List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:Main.java
/** * Get the index'th child node of parent that implements/subclasses the * given type./* ww w .j a v a 2 s. co m*/ * * @param index * Child node index * @param parent * Parent to search * @param nodeType * Type of child to search for * @return index'th child of parent that conforms to the given nodeType */ public static <N extends Node> N getNthChildImplementing(int index, Node parent, Class<N> nodeType) { if (parent == null) { return null; } NodeList children = parent.getChildNodes(); if (children == null) { return null; } int count = 0; for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (nodeType.isAssignableFrom(node.getClass())) { if (count++ == index) { @SuppressWarnings("unchecked") N n = (N) node; return n; } } } return null; }
From source file:Utils.java
public static boolean isTypeCompatible(Class formalType, Class actualType, boolean strict) { if (formalType == null && actualType != null) return false; if (formalType != null && actualType == null) return false; if (formalType == null && actualType == null) return true; if (strict)/*from ww w .j a v a 2 s .com*/ return formalType.equals(actualType); else return formalType.isAssignableFrom(actualType); }
From source file:fr.landel.utils.assertor.utils.AssertorClass.java
/** * Prepare the next step to validate if the {@link Class} is assignable from * the specified super type/* w w w .j a va 2 s.co m*/ * * <p> * precondition: neither classes can be null * </p> * * @param step * the current step * @param superType * the super type * @param message * the message if invalid * @param <T> * the class type * @return the next step */ public static <T> StepAssertor<Class<T>> isAssignableFrom(final StepAssertor<Class<T>> step, final Class<?> superType, final MessageAssertor message) { final Predicate<Class<T>> preChecker = (type) -> type != null && superType != null; final BiPredicate<Class<T>, Boolean> checker = (type, not) -> superType.isAssignableFrom(type); return new StepAssertor<>(step, preChecker, checker, false, message, MSG.CLASS.ASSIGNABLE, false, new ParameterAssertor<>(superType, EnumType.CLASS)); }
From source file:net.sf.morph.util.TransformerUtils.java
/** * Get the mapped destination type from the specified typemap. * @param typeMapping Map/*from w w w . ja v a2 s . c o m*/ * @param requestedType Class * @return Class */ public static Class getMappedDestinationType(Map typeMapping, Class requestedType) { if (typeMapping == null) { return null; } // see if the requested type has been directly mapped to some other type Class mappedDestinationType = (Class) typeMapping.get(requestedType); // see if the requested type has been indirectly mapped to some other type if (mappedDestinationType == null) { Set keys = typeMapping.keySet(); for (Iterator i = keys.iterator(); i.hasNext();) { Class type = (Class) i.next(); if (type.isAssignableFrom(requestedType)) { mappedDestinationType = (Class) typeMapping.get(type); break; } } } return mappedDestinationType; }
From source file:com.ery.estorm.util.ReflectionUtils.java
/** * This code is to support backward compatibility and break the compile time dependency of core on mapred. This should be made * deprecated along with the mapred package HADOOP-1230. Should be removed when mapred package is removed. *///from www .j av a2s . com private static void setJobConf(Object theObject, Configuration conf) { // If JobConf and JobConfigurable are in classpath, AND // theObject is of type JobConfigurable AND // conf is of type JobConf then // invoke configure on theObject try { Class<?> jobConfClass = conf.getClassByName("org.apache.hadoop.mapred.JobConf"); Class<?> jobConfigurableClass = conf.getClassByName("org.apache.hadoop.mapred.JobConfigurable"); if (jobConfClass.isAssignableFrom(conf.getClass()) && jobConfigurableClass.isAssignableFrom(theObject.getClass())) { Method configureMethod = jobConfigurableClass.getMethod("configure", jobConfClass); configureMethod.invoke(theObject, conf); } } catch (ClassNotFoundException e) { // JobConf/JobConfigurable not in classpath. no need to configure } catch (Exception e) { throw new RuntimeException("Error in configuring object", e); } }
From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.java
/** * Checks if given class is one of the primitive types or more importantly, * one of the classes associated with a primitive type. eg. Integer, Double * etc./*w w w. j a v a 2s .com*/ * * @param cls * @return */ private static boolean isPrimitive(Class<?> cls) { return cls.isPrimitive() || cls.isAssignableFrom(Byte.class) || cls.isAssignableFrom(Short.class) || cls.isAssignableFrom(Integer.class) || cls.isAssignableFrom(Long.class) || cls.isAssignableFrom(Float.class) || cls.isAssignableFrom(Double.class) || cls.isAssignableFrom(Boolean.class) || cls.isAssignableFrom(Character.class); }
From source file:com.addthis.codec.config.Configs.java
private static boolean isCodableType(CodableFieldInfo fieldInfo) { Class<?> expectedType = elementType(fieldInfo); if (expectedType.isAssignableFrom(String.class)) { return false; } else if ((expectedType == boolean.class) || (expectedType == Boolean.class)) { return false; } else if (expectedType == AtomicBoolean.class) { return false; } else if (Number.class.isAssignableFrom(expectedType) || expectedType.isPrimitive()) { // primitive numeric types are not subclasses of Number, so just catch all non-booleans return false; } else if (expectedType.isEnum()) { return false; } else {/* w w w .ja v a2 s. c o m*/ return true; } }
From source file:io.neba.core.util.ReflectionUtil.java
/** * Provides an implementation instance for the desired collection type. * Example: If the collection type is {@link List}, a suitable list implementation is instantiated. * * @param collectionType must not be <code>null</code>. * @return never <code>null</code>. Throws an {@link IllegalStateException} * if the collection type is not supported. */// www . jav a 2s. co m @SuppressWarnings("unchecked") public static <K, T extends Collection<K>> Collection<K> instantiateCollectionType(Class<T> collectionType, int length) { if (collectionType == null) { throw new IllegalArgumentException("Method parameter collectionType must not be null."); } T collection; // This includes java.util.Collection if (collectionType.isAssignableFrom(List.class)) { collection = (T) new ArrayList<K>(length); } else if (collectionType.isAssignableFrom(Set.class)) { collection = (T) new LinkedHashSet<K>(length); } else { throw new IllegalArgumentException("Unable to instantiate a collection compatible to " + collectionType + ". Only " + List.class + ", " + Set.class + " or " + Collection.class + " are supported."); } return collection; }
From source file:net.sf.morph2.util.TransformerUtils.java
/** * Get the mapped destination type from the specified typemap. * @param typeMapping Map/* w w w. j av a2s . c o m*/ * @param requestedType Class * @return Class */ public static Class getMappedDestinationType(Map typeMapping, Class requestedType) { if (typeMapping == null) { return null; } // see if the requested type has been directly mapped to some other type Class mappedDestinationType = (Class) typeMapping.get(requestedType); // see if the requested type has been indirectly mapped to some other // type if (mappedDestinationType == null) { Set keys = typeMapping.keySet(); for (Iterator i = keys.iterator(); i.hasNext();) { Class type = (Class) i.next(); if (type.isAssignableFrom(requestedType)) { mappedDestinationType = (Class) typeMapping.get(type); break; } } } return mappedDestinationType; }
From source file:ClassFinder.java
/** * //from w w w . j av a 2s .com * @param parentClasses * list of classes to check for * @param strClassName * name of class to be checked * @param contextClassLoader * the classloader to use * @return */ private static boolean isChildOf(Class[] parentClasses, String strClassName, ClassLoader contextClassLoader) { // might throw an exception, assume this is ignorable try { Class c = Class.forName(strClassName, false, contextClassLoader); if (!c.isInterface() && !Modifier.isAbstract(c.getModifiers())) { for (Class parentClass : parentClasses) { if (parentClass.isAssignableFrom(c)) { return true; } } } } catch (Throwable ignored) { } return false; }