List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:annis.visualizers.component.grid.EventExtractor.java
/** * Get the qualified name of all annotations belonging to spans having a * specific namespace.//from ww w. java 2 s . c o m * * @param graph The graph. * @param namespace The namespace of the node (not the annotation) to search * for. * @param type Which type of nodes to include * @return * */ private static Set<String> getAnnotationLevelSet(SDocumentGraph graph, String namespace, Class<? extends SNode> type) { Set<String> result = new TreeSet<String>(); if (graph != null) { EList<? extends SNode> nodes; // catch most common cases directly if (SSpan.class == type) { nodes = graph.getSSpans(); } else if (SToken.class == type) { nodes = graph.getSTokens(); } else { nodes = graph.getSNodes(); } if (nodes != null) { for (SNode n : nodes) { if (type.isAssignableFrom(n.getClass())) { for (SLayer layer : n.getSLayers()) { if (namespace == null || namespace.equals(layer.getSName())) { for (SAnnotation anno : n.getSAnnotations()) { result.add(anno.getQName()); } // we got all annotations of this node, jump to next node break; } // end if namespace equals layer name } // end for each layer } } // end for each node } } return result; }
From source file:com.jim.im.utils.Assert.java
/** * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>. * <p/>//from ww w .j av a2 s .c om * * <pre class="code"> * Assert.isAssignableFrom(Number.class, myClass, "The subType must be * instance of the superType"); * </pre> * * @param superType the super type to check against * @param subType the sub type 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 classes are not assignable */ @SuppressWarnings("unchecked") public static void isAssignableFrom(Class superType, Class subType, String message) { notNull(superType, "Type to check against must not be null"); if (subType == null || !superType.isAssignableFrom(subType)) { throwException(message); } }
From source file:com.bstek.dorado.data.JsonUtils.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private static Object internalToJavaCollection(ArrayNode arrayNode, AggregationDataType dataType, Class<Collection<?>> targetType, boolean proxy, JsonConvertContext context) throws Exception { Class<Collection> creationType = null; if (dataType != null) { creationType = (Class<Collection>) dataType.getCreationType(); }//from ww w . j av a 2 s . com boolean shouldConvertToArray = false; Collection collection = null; if (creationType != null) { if (targetType != null && !targetType.isAssignableFrom(creationType)) { throw new IllegalArgumentException("Java type mismatch. expect [" + targetType + "]."); } if (!Collection.class.isAssignableFrom(creationType)) { if (creationType.isArray()) { shouldConvertToArray = true; } else { throw new IllegalArgumentException( "Java type mismatch. expect [java.util.Collection] or [Array]."); } } else { if (!Modifier.isAbstract(creationType.getModifiers())) { collection = creationType.newInstance(); } } } else if (targetType != null) { if (targetType.isArray()) { shouldConvertToArray = true; } else if (!Modifier.isAbstract(targetType.getModifiers())) { collection = targetType.newInstance(); } else if (Set.class.isAssignableFrom(targetType)) { collection = new HashSet<Object>(); } } if (collection == null) { collection = new ArrayList<Object>(); } boolean isFirstElement = true; DataType elementDataType = ((dataType != null) ? dataType.getElementDataType() : null); Iterator<JsonNode> it = arrayNode.iterator(); while (it.hasNext()) { JsonNode jsonNode = it.next(); if (jsonNode instanceof ObjectNode) { ObjectNode objectNode = (ObjectNode) jsonNode; if (isFirstElement && elementDataType == null) { String dataTypeName = JsonUtils.getString(objectNode, DATATYPE_PROPERTY); if (StringUtils.isNotEmpty(dataTypeName)) { elementDataType = getDataType(dataTypeName, context); } } collection.add( internalToJavaEntity(objectNode, (EntityDataType) elementDataType, null, proxy, context)); } else if (jsonNode instanceof ValueNode) { collection.add(toJavaValue(((ValueNode) jsonNode), elementDataType, context)); } else { throw new IllegalArgumentException("Value type mismatch. expect [JSON Value]."); } isFirstElement = false; } if (context != null && context.getEntityListCollection() != null) { context.getEntityListCollection().add(collection); } if (shouldConvertToArray) { return collection.toArray(); } else { if (dataType != null) { collection = EntityUtils.toEntity(collection, dataType); } return collection; } }
From source file:com.evolveum.midpoint.util.ReflectionUtil.java
/** * Try to get java property from the object by reflection */// ww w . j a va2 s.c o m public static <T> T getJavaProperty(Object object, String propertyName, Class<T> propetyClass) { String getterName = getterName(propertyName); Method method; try { method = object.getClass().getMethod(getterName); } catch (SecurityException e) { throw new IllegalArgumentException( "Security error getting getter for property " + propertyName + ": " + e.getMessage(), e); } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "No getter for property " + propertyName + " in " + object + " (" + object.getClass() + ")"); } if (method == null) { throw new IllegalArgumentException( "No getter for property " + propertyName + " in " + object + " (" + object.getClass() + ")"); } if (!propetyClass.isAssignableFrom(method.getReturnType())) { throw new IllegalArgumentException( "The getter for property " + propertyName + " returns " + method.getReturnType() + ", expected " + propetyClass + " in " + object + " (" + object.getClass() + ")"); } try { return (T) method.invoke(object); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Error invoking getter for property " + propertyName + " in " + object + " (" + object.getClass() + "): " + e.getClass().getSimpleName() + ": " + e.getMessage(), e); } catch (IllegalAccessException e) { throw new IllegalArgumentException( "Error invoking getter for property " + propertyName + " in " + object + " (" + object.getClass() + "): " + e.getClass().getSimpleName() + ": " + e.getMessage(), e); } catch (InvocationTargetException e) { throw new IllegalArgumentException( "Error invoking getter for property " + propertyName + " in " + object + " (" + object.getClass() + "): " + e.getClass().getSimpleName() + ": " + e.getMessage(), e); } }
From source file:com.fatminds.cmis.AlfrescoCmisHelper.java
public static Object convert(Object inVal, Class<?> outClass) { if (null == inVal || null == outClass) { //log.info("inVal " + inVal + " or outClass " + outClass + " are null"); return null; }/*from ww w.j a va 2 s . com*/ //log.info("Converting " + inVal.getClass() + " to " + outClass); if (outClass.isAssignableFrom(inVal.getClass())) return inVal; Object outVal; try { Constructor<?> c = outClass.getConstructor(inVal.getClass()); outVal = c.newInstance(inVal); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { //log.info("Failed to find constructor of " + outClass.toString() // + " with input parameter " + inVal.getClass().toString() // + ", returning unconverted value"); outVal = inVal; } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } //log.info("Returning an instance of " + outVal.getClass()); return outVal; }
From source file:com.github.helenusdriver.driver.impl.RootClassInfoImpl.java
/** * Finds the class info for all type classes for this root element class. * * @author paouelle// w ww .jav a 2 s . co m * * @param <T> The type of POJO represented by this root element class * * @param mgr the non-<code>null</code> statement manager * @param clazz the root POJO class * @return the non-<code>null</code> map of class info for all types * @throws NullPointerException if <code>clazz</code> is <code>null</code> * @throws IllegalArgumentException if any of the type classes are invalid */ private static <T> Map<Class<? extends T>, TypeClassInfoImpl<? extends T>> findTypeInfos( StatementManagerImpl mgr, Class<T> clazz) { org.apache.commons.lang3.Validate.notNull(clazz, "invalid null root POJO class"); final RootEntity re = clazz.getAnnotation(RootEntity.class); final int hsize = re.types().length * 3 / 2; final Map<Class<? extends T>, TypeClassInfoImpl<? extends T>> types = new HashMap<>(hsize); final Set<String> names = new HashSet<>(hsize); for (final Class<?> type : re.types()) { org.apache.commons.lang3.Validate.isTrue(clazz.isAssignableFrom(type), "type class '%s' must extends root element class: %s", type.getName(), clazz.getName()); @SuppressWarnings("unchecked") // tested above! final TypeClassInfoImpl<? extends T> tcinfo = new TypeClassInfoImpl<>(mgr, clazz, (Class<? extends T>) type); org.apache.commons.lang3.Validate.isTrue(types.put(tcinfo.getObjectClass(), tcinfo) == null, "duplicate type element class '%s' defined for root element class '%s'", type.getSimpleName(), clazz.getSimpleName()); org.apache.commons.lang3.Validate.isTrue(names.add(tcinfo.getType()), "duplicate type name '%s' defined by class '%s' for root element class '%s'", tcinfo.getType(), type.getSimpleName(), clazz.getSimpleName()); } return types; }
From source file:com.dianping.squirrel.client.util.ClassUtils.java
/** * getInstance from className by using reflection * @param <T>/*from w w w . ja v a2 s . co m*/ * @param className * @param interfaceClazz * @return instance */ @SuppressWarnings("unchecked") public static <T> T newInstance(String className, Class<?> interfaceClazz) { if (StringUtils.isEmpty(className)) { throw new IllegalArgumentException("\"className\" parameter must not be empty!"); } if (interfaceClazz == null) { throw new IllegalArgumentException("\"interfaceClazz\" parameter must not be null!"); } T result = null; Class<?> cz = null; try { cz = Thread.currentThread().getContextClassLoader().loadClass(className); } catch (ClassNotFoundException e) { } if (cz == null) { try { cz = ClassUtils.class.getClassLoader().loadClass(className); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("The implementation [" + className + "] class is not found."); } } if (!interfaceClazz.isAssignableFrom(cz)) { throw new IllegalArgumentException( "The implementation[" + className + "] is not drived from " + interfaceClazz.getName()); } try { result = (T) cz.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException( "Cann't instantiate " + interfaceClazz.getName() + " implementation[" + className + "]", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } return result; }
From source file:de.perdian.commons.lang.conversion.impl.resolvers.InputToOutputResolver.java
@Override public Converter<?, ?> resolveConverter(Class<?> sourceClass, Class<?> targetClass) { if (targetClass.isAssignableFrom(sourceClass)) { return new InputToOutputConverter<>(); } else {/*from ww w. j a v a 2 s . c om*/ return null; } }
From source file:org.hellojavaer.testcase.generator.TestCaseGenerator.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private static <T> void checkBeanValidity(Class<T> clazz, List<String> excludeFieldList, int recursiveCycleLimit) { PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(clazz); boolean validBean = false; for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null && pd.getReadMethod() != null && // (excludeFieldList != null && !excludeFieldList.contains(pd.getName()) || excludeFieldList == null) // ) {//from w w w .j a v a2 s . c o m validBean = true; // just set write ,read for user control if (!Modifier.isPublic(pd.getWriteMethod().getDeclaringClass().getModifiers())) { pd.getWriteMethod().setAccessible(true); } Class fieldClazz = pd.getPropertyType(); if (!TypeUtil.isBaseType(fieldClazz)) { try { // ???? if (recursiveCycleLimit == 0 && fieldClazz == clazz) { throw new RuntimeException("recursive cycle limit is 0! field[" + pd.getName() + "] may cause recursive, please add this field[" + pd.getName() + "] to exclude list or set recursiveCycleLimit more than 0 ."); } else if (!fieldClazz.isAssignableFrom(clazz)) { checkBeanValidity(fieldClazz, null, 999999999); } } catch (Exception e) { throw new RuntimeException("Unknown Class " + fieldClazz.getName() + " for field " + pd.getName() + " ! please add this field[" + pd.getName() + "] to exclude list.", e); } } } } if (!validBean) { throw new RuntimeException( "Invalid Bean Class[" + clazz.getName() + "], for it has not getter setter methods!"); } }
From source file:org.mashupmedia.validator.EditGroupPageValidator.java
@Override public boolean supports(Class<?> clazz) { return clazz.isAssignableFrom(EditGroupPage.class); }