List of usage examples for java.lang Class getInterfaces
public Class<?>[] getInterfaces()
From source file:br.gov.frameworkdemoiselle.ldap.internal.ClazzUtils.java
public static boolean isCollection(Class<?> clazz) { for (Class<?> claz : clazz.getInterfaces()) if (claz == Collection.class) return true; return false; }
From source file:nl.nn.adapterframework.util.ClassUtils.java
public static String debugObject(Object o) { if (o == null) { return null; }//from w w w . j a v a2 s. c o m StringBuffer result = new StringBuffer(nameOf(o) + "\n"); Class c = o.getClass(); Class interfaces[] = c.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { appendFieldsAndMethods(result, o, "Interface", interfaces[i]); } while (c != Object.class) { appendFieldsAndMethods(result, o, "Class", c); c = c.getSuperclass(); } result.append("toString=[" + o.toString() + "]\n"); result.append("reflectionToString=[" + reflectionToString(o, null) + "]\n"); return result.toString(); }
From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java
/** * Get a single {@link Annotation} of <code>annotationType</code> from the supplied {@link Method}, traversing its * super methods if no annotation can be found on the given method itself. * <p>//from w w w .jav a2 s .co m * Annotations on methods are not inherited by default, so we need to handle this explicitly. * * @param method * the method to look for annotations on * @param annotationType * the annotation class to look for * @return the annotation found, or <code>null</code> if none found */ public static <A extends Annotation> A findAnnotation(final Method method, final Class<A> annotationType) { A annotation = getAnnotation(method, annotationType); Class<?> cl = method.getDeclaringClass(); if (annotation == null) { annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces()); } while (annotation == null) { cl = cl.getSuperclass(); if ((cl == null) || (cl == Object.class)) { break; } try { final Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes()); annotation = getAnnotation(equivalentMethod, annotationType); if (annotation == null) { annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces()); } } catch (final NoSuchMethodException ex) { // We're done... } } return annotation; }
From source file:Main.java
private final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> declaringClass, Class<?> inClass) { if (inClass == null) return null; Class<?> superClass = null; Type resolvedType = null;/*from ww w . j a va 2s . c om*/ Type genericSuperClass = null; if (!declaringClass.equals(inClass)) { if (declaringClass.isInterface()) { // the declaringClass is an interface Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length && resolvedType == null; i++) { superClass = interfaces[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericInterfaces()[i]; } } if (resolvedType == null) { superClass = inClass.getSuperclass(); resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericSuperclass(); } } else { resolvedType = type; genericSuperClass = superClass = inClass; } if (resolvedType != null) { // if its another type this means we have finished if (resolvedType instanceof TypeVariable<?>) { type = (TypeVariable<?>) resolvedType; TypeVariable<?>[] parameters = superClass.getTypeParameters(); int positionInClass = 0; for (; positionInClass < parameters.length && !type.equals(parameters[positionInClass]); positionInClass++) { } // we located the position of the typevariable in the superclass if (positionInClass < parameters.length) { // let's look if we have type specialization information in the current class if (genericSuperClass instanceof ParameterizedType) { ParameterizedType pGenericType = (ParameterizedType) genericSuperClass; Type[] args = pGenericType.getActualTypeArguments(); return positionInClass < args.length ? args[positionInClass] : null; } } // we didnt find typevariable specialization in the class, so it's the best we can // do, lets return the resolvedType... } } return resolvedType; }
From source file:GenericsUtil.java
/** * Returns all of the {@link TypeVariables}s implemented * by the given class. If none are implemented then an array * of zero length is returned./*w w w . jav a 2 s . c o m*/ * @param clazz the class * @param genericClazz the generic class or interface to return * the TypeVariables from * @return an array of TypeVariable */ public static TypeVariable<?>[] getGenericTypeParameters(Class<?> clazz, Type genericClazz) { List<TypeVariable<?>> vars = new ArrayList<TypeVariable<?>>(); // add superclass for (TypeVariable<?> var : clazz.getSuperclass().getTypeParameters()) { if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) { vars.add(var); } } // add interfaces for (Class<?> iface : clazz.getInterfaces()) { for (TypeVariable<?> var : iface.getTypeParameters()) { if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) { vars.add(var); } } } // return list return vars.toArray(new TypeVariable<?>[0]); }
From source file:org.seedstack.seed.core.utils.BaseClassSpecifications.java
/** * This method walks up the inheritance hierarchy to make sure we get every class/interface/superclass/interface's superclass that could * possibly contain the declaration of the annotated method we're looking for. * * @param classes array of class//from w w w . j av a2 s .c om * @return array of class */ @SuppressWarnings("unchecked") static Class<?>[] getAllInterfacesAndClasses(Class<?>[] classes) { if (0 == classes.length) { return classes; } else { List<Class<?>> extendedClasses = new ArrayList<Class<?>>(); // all interfaces hierarchy for (Class<?> clazz : classes) { if (clazz != null) { Class<?>[] interfaces = clazz.getInterfaces(); if (interfaces != null) { extendedClasses.addAll(Arrays.asList(interfaces)); } Class<?> superclass = clazz.getSuperclass(); if (superclass != null && superclass != Object.class) { extendedClasses.addAll(Arrays.asList(superclass)); } } } // Class::getInterfaces() gets only interfaces/classes // implemented/extended directly by a given class. // We need to walk the whole way up the tree. return (Class[]) ArrayUtils.addAll(classes, getAllInterfacesAndClasses(extendedClasses.toArray(new Class[extendedClasses.size()]))); } }
From source file:org.seedstack.seed.core.utils.BaseClassSpecifications.java
public static Specification<Class<?>> classImplements(final Class<?> klass) { return new AbstractSpecification<Class<?>>() { @Override// w w w . jav a 2 s. c om public boolean isSatisfiedBy(Class<?> candidate) { if (candidate != null && klass.isInterface()) { for (Class<?> i : candidate.getInterfaces()) { if (i.equals(klass)) { return true; } } } return false; } }; }
From source file:org.jspresso.framework.util.bean.PropertyHelper.java
/** * Retrieves all property names declared by a bean class. * * @param beanClass/* ww w.j a va 2 s . com*/ * the class to introspect. * @return the collection of property names. */ public static Collection<String> getPropertyNames(Class<?> beanClass) { Collection<String> propertyNames = new HashSet<>(); PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass); for (PropertyDescriptor descriptor : descriptors) { propertyNames.add(descriptor.getName()); } if (beanClass.isInterface()) { for (Class<?> superInterface : beanClass.getInterfaces()) { propertyNames.addAll(getPropertyNames(superInterface)); } } return propertyNames; }
From source file:Main.java
public static void print_class(Class c) { if (c.isInterface()) { System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c)); } else if (c.getSuperclass() != null) { System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends " + typename(c.getSuperclass())); } else {/* w ww . j a v a 2 s .c o m*/ System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c)); } Class[] interfaces = c.getInterfaces(); if ((interfaces != null) && (interfaces.length > 0)) { if (c.isInterface()) System.out.print(" extends "); else System.out.print(" implements "); for (int i = 0; i < interfaces.length; i++) { if (i > 0) System.out.print(", "); System.out.print(typename(interfaces[i])); } } System.out.println(" {"); Constructor[] constructors = c.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) print_method_or_constructor(constructors[i]); Field[] fields = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { print_field(fields[i]); } Method[] methods = c.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) print_method_or_constructor(methods[i]); System.out.println("}"); }
From source file:org.apache.marmotta.commons.sesame.facading.util.FacadeUtils.java
/** * Check whether a type is a {@link Facade}, i.e. the type or one of its superinterfaces has the * {@link Facade} annotation./* w w w.ja v a 2 s . co m*/ * * @param <C> * @param clazz * @return */ public static <C> boolean isFacadeAnnotationPresent(Class<C> clazz, Class<? extends Annotation> annotation) { if (clazz.isAnnotationPresent(annotation)) { return true; } else { for (final Class<?> iface : clazz.getInterfaces()) { if (iface.isAnnotationPresent(annotation)) { return true; } } if (clazz.getSuperclass() != null) { return isFacadeAnnotationPresent(clazz.getSuperclass(), annotation); } return false; } }