List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:demo.config.PropertyConverter.java
/** * Converts the specified value to the target class. If the class is a * primitive type (Integer.TYPE, Boolean.TYPE, etc) the value returned will * use the wrapper type (Integer.class, Boolean.class, etc). * //from w ww . jav a 2 s . com * @param cls * the target class of the converted value * @param value * the value to convert * @param params * optional parameters used for the conversion * @return the converted value * @throws ConversionException * if the value is not compatible with the requested type * * @since 1.5 */ static Object to(Class<?> cls, Object value, Object[] params) throws ConversionException { if (cls.isInstance(value)) { return value; // no conversion needed } if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) { return toBoolean(value); } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) { return toCharacter(value); } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) { if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) { return toInteger(value); } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) { return toLong(value); } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) { return toByte(value); } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) { return toShort(value); } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) { return toFloat(value); } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) { return toDouble(value); } else if (BigInteger.class.equals(cls)) { return toBigInteger(value); } else if (BigDecimal.class.equals(cls)) { return toBigDecimal(value); } } else if (Date.class.equals(cls)) { return toDate(value, (String) params[0]); } else if (Calendar.class.equals(cls)) { return toCalendar(value, (String) params[0]); } else if (URL.class.equals(cls)) { return toURL(value); } else if (Locale.class.equals(cls)) { return toLocale(value); } else if (isEnum(cls)) { return convertToEnum(cls, value); } else if (Color.class.equals(cls)) { return toColor(value); } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) { return toInternetAddress(value); } else if (InetAddress.class.isAssignableFrom(cls)) { return toInetAddress(value); } throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")" + " can't be converted to a " + cls.getName() + " object"); }
From source file:com.jim.im.utils.Assert.java
/** * Assert that the provided object is an instance of the provided class. * <p/>/*w ww .j a va2s . c o m*/ * * <pre class="code"> * Assert.isInstanceOf(Foo.class, foo, "The object must be instance of * the type"); * </pre> * * @param type the type to check against * @param obj the object to check * @param message a message which will be prepended to the message produced by the function * itself, and which may be used to provide context. It should normally end in a ": " or * ". " so that the function generate message looks ok when prepended to it. * @throws IllegalStateException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class type, Object obj, String message) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { throwException(message); } }
From source file:io.fabric8.maven.core.util.KubernetesResourceUtil.java
/** * Returns the resource of the given kind and name from the collection or null *//*from w ww. j a v a 2 s .c om*/ public static <T> T findResourceByName(Iterable<HasMetadata> entities, Class<T> clazz, String name) { if (entities != null) { for (HasMetadata entity : entities) { if (clazz.isInstance(entity) && Objects.equals(name, getName(entity))) { return clazz.cast(entity); } } } return null; }
From source file:com.miravtech.SBGNUtils.SBGNUtils.java
public static Map<String, SBGNNodeType> getInnerNodesOfType(SBGNNodeType n, Class<?> c) { Map<String, SBGNNodeType> ret = new HashMap<String, SBGNNodeType>(); for (SBGNNodeType n1 : n.getInnerNodes()) { if (c.isInstance(c)) { ret.put(n1.getID(), n1);/* www . ja va 2s .com*/ } } return ret; }
From source file:com.ironiacorp.persistence.datasource.HibernateConfigurationUtil.java
/** * Retrieve the data sources registered at JNDI in the given context. * // w w w . ja v a 2 s .c om * @param namingContext * Start point context. * * @return A collection of the data sources found within the context. */ private static Collection<String> getAvailableDataSources(Context namingContext) { Collection<String> datasources = new ArrayList<String>(); Class<?> type = null; // Acquire global JNDI resources if available try { type = Class.forName("xyz"); } catch (ClassNotFoundException e) { } try { NamingEnumeration<Binding> items = namingContext.listBindings(""); while (items.hasMore()) { Binding item = (Binding) items.next(); if (item.getObject() instanceof Context) { datasources.addAll(getAvailableDataSources((Context) item.getObject())); } else { if (type.isInstance(item.getObject())) { datasources.add(item.getName()); } } } } catch (Throwable t) { } return datasources; }
From source file:jfix.util.Reflections.java
/** * Returns all instanceable (sub-)classes of given type in given package. *//*from www . j ava2 s . c om*/ public static <E> E[] find(Class<E> classType, Package pckage) { File directory; try { String name = "/" + pckage.getName().replace('.', '/'); directory = new File(classType.getResource(name).toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e.getMessage(), e); } List<E> result = new ArrayList<>(); if (directory.exists()) { String[] files = directory.list(); for (int i = 0; i < files.length; i++) { if (files[i].endsWith(".class")) { String classname = files[i].substring(0, files[i].length() - 6); try { Object o = Class.forName(pckage.getName() + "." + classname).newInstance(); if (classType.isInstance(o)) { result.add((E) o); } } catch (ClassNotFoundException cnfex) { System.err.println(cnfex); } catch (InstantiationException iex) { } catch (IllegalAccessException iaex) { } } } } result.sort(new Comparator<Object>() { public int compare(Object o1, Object o2) { return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); } }); return result.toArray((E[]) Array.newInstance(classType, result.size())); }
From source file:org.web4thejob.web.util.ZkUtil.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static <T extends CommandDecorator> List<T> getCommandDecorators(Command command, Class<T> decoratorType) { if (command == null) return Collections.emptyList(); List<T> decorators = new ArrayList<T>(); for (MessageAware listener : command.getListeners()) { if (decoratorType.isInstance(listener)) { decorators.add((T) listener); }/*w ww .j a va2 s . com*/ } return decorators; }
From source file:hudson.model.Items.java
private static <T extends Item> void getAllItems(final ItemGroup root, Class<T> type, List<T> r) { List<Item> items = new ArrayList<Item>(((ItemGroup<?>) root).getItems()); Collections.sort(items, new Comparator<Item>() { @Override/* w w w . java2 s. com*/ public int compare(Item i1, Item i2) { return name(i1).compareToIgnoreCase(name(i2)); } String name(Item i) { String n = i.getName(); if (i instanceof ItemGroup) { n += '/'; } return n; } }); for (Item i : items) { if (type.isInstance(i)) { if (i.hasPermission(Item.READ)) { r.add(type.cast(i)); } } if (i instanceof ItemGroup) { getAllItems((ItemGroup) i, type, r); } } }
From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java
/** * Returns the leftmost annotation of type <tt>cls</tt> that completely includes <tt>focusAnnotation</tt>. That is, * if multiple annotations of type <tt>cls</tt> include <tt>focusAnnotation</tt>, the one with the lowest begin * offset will be chosen./*from w w w. j a va2 s . co m*/ * <p> * This algorithm has <tt>O(n)</tt> runtime with <tt>n</tt> being the number of annotations in the annotation index. * </p> * <p> * TODO: A start offset parameter could be introduced from where to start looking. This way, when iterating over a * number of different focusAnnotations in ascending order, one would have only to check from focusAnnotation to * focusAnnotation and not always from the very beginning of the annotation index. Same thing for * getPartiallyOverlappingAnnotation(). * </p> * * @param aJCas * @param focusAnnotation * @param cls * @return the leftmost annotation of type <tt>cls</tt> that completely includes <tt>focusAnnotation</tt>. */ @SuppressWarnings("unchecked") public static <T extends Annotation> T getIncludingAnnotation(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); // Annotations are sorted by begin offset and may be arbitrarily long. Thus we just have to start from the // beginning. cursor.moveToFirst(); // Now go to the right as long as we don't yet overlap with the focus annotation, then stop. Annotation currentAnnotation = null; while (cursor.isValid() && ((currentAnnotation = cursor.get()).getEnd() < focusAnnotation.getEnd() || !cls.isInstance(currentAnnotation))) { cursor.moveToNext(); } // Check whether we have found an overlapping annotation. Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && currentRange.containsRange(focusRange)) return (T) cursor.get(); return null; }
From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java
/** * Returns the leftmost annotation of type <tt>cls</tt> that overlaps <tt>focusAnnotation</tt>. That is, if multiple * annotations of type <tt>cls</tt> overlap with <tt>focusAnnotation</tt>, the one with the lowest begin offset will * be chosen.//from ww w . j ava 2 s.c o m * <p> * The two annotations may overlap in any way (partial, nested, inclusion, exact match). This algorithm has * <tt>O(n)</tt> runtime with <tt>n</tt> being the number of annotations in the annotation index. * </p> * * * <p> * TODO: A start offset parameter could be introduced from where to start looking. This way, when iterating over a * number of different focusAnnotations in ascending order, one would have only to check from focusAnnotation to * focusAnnotation and not always from the very beginning of the annotation index. Same thing for * getIncludingAnnotation(). * </p> * * @param aJCas * @param focusAnnotation * @param cls * @return the leftmost annotation of type <tt>cls</tt> that overlaps <tt>focusAnnotation</tt>. */ @SuppressWarnings("unchecked") public static <T extends Annotation> T getPartiallyOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); // Annotations are sorted by begin offset and may be arbitrarily long. Thus we just have to start from the // beginning. cursor.moveToFirst(); // Now go to the right as long as we don't yet overlap with the focus annotation, then stop. Annotation currentAnnotation = null; while (cursor.isValid() && ((currentAnnotation = cursor.get()).getEnd() <= focusAnnotation.getBegin() || !cls.isInstance(currentAnnotation))) { cursor.moveToNext(); } // Check whether we have found an overlapping annotation. Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && currentRange.isOverlappedBy(focusRange)) return (T) cursor.get(); return null; }