/** * Returns the list of fields on this class or any of its ancestors annotated with the * passed {@link Annotation}. * @param klass checks the {@link Field}s on this class and its ancestors * @param annotation looks for this {@link Annotation} * @return list of all {@link Field}s that are annotated with the specified {@link Annotation} */ public static List<Field> getAnnotatedFieldsForClassHierarchy(Class<?> klass, final Class<? extends Annotation> annotation) { final List<Field> annotatedFields = Lists.newArrayList(); visitClassHierarchy(klass, new Visitor<Class<?>>() { public void visit(Class<?> c) { annotatedFields.addAll(getAnnotatedFields(c, annotation)); } }); return annotatedFields; }
/** * Returns all methods in the passed object's class hierarchy that do no not take parameters * @param object object to query for parameterless methods * @return a list {@link ObjectFieldMethod} tuples mapping the parameterless methods to * the passed object. */ public static List<ObjectFieldMethod> getParameterlessMethodsForClassHierarchy(final Object object) { final List<ObjectFieldMethod> methods = Lists.newArrayList(); visitClassHierarchy(object.getClass(), new Visitor<Class<?>>() { public void visit(Class<?> c) { methods.addAll(getParameterlessMethods(object, c)); } }); return methods; }
/** * Checks whether or not the specified {@link Annotation} exists in the passed {@link Object}'s * class hierarchy. * @param object object to check * @param annotation annotation to look for * @return true is a class in this passed object's type hierarchy is annotated with the * passed {@link Annotation} */ public static boolean isClassAnnotatedForClassHierarchy(Object object, final Class<? extends Annotation> annotation) { final boolean[] bool = new boolean[1]; visitClassHierarchy(object.getClass(), new Visitor<Class<?>>() { public void visit(Class<?> klass) { if (klass.isAnnotationPresent(annotation)) { bool[0] = true; } } }); return bool[0]; }
/** * Gets all fields assignable from <code>targetClass</code> in the passed class's type * hierarchy. * * @param klass starting point in the type stack to query for fields of the specified type. * @param targetClass looks for fields that are assignable from this type. * @return all fields declared by classes in <code>klass</code>'s type hierarchy assignable from * <code>targetclass</code> * @see Class#isAssignableFrom(Class) * @see Class#getDeclaredClasses() * @see #getTypesOfType(Class, Class) */ public static List<Field> getFieldsOfTypeForClassHierarchy(Class<?> klass, final Class<?> targetClass) { final List<Field> fields = Lists.newArrayList(); visitClassHierarchy(klass, new Visitor<Class<?>>() { public void visit(Class<?> c) { fields.addAll(getFieldsOfType(c, targetClass)); } }); return fields; }
/** * Gets all inner classes assignable from <code>targetClass</code> in the passed class's type * hierarchy. * * @param klass starting point in the type stack to query for inner classes. * @param targetClass looks for inner classes that are assignable from this type. * @return all inner classes in <code>klass</code>'s type hierarchy assignable from * <code>targetclass</code> * @see Class#isAssignableFrom(Class) * @see Class#getDeclaredClasses() * @see #getTypesOfType(Class, Class) */ public static List<Class<?>> getTypesOfTypeForClassHierarchy(Class<?> klass, final Class<?> targetClass) { final List<Class<?>> classes = Lists.newArrayList(); visitClassHierarchy(klass, new Visitor<Class<?>>() { public void visit(Class<?> c) { classes.addAll(getTypesOfType(c, targetClass)); } }); return classes; }
private static Collection<Binding> wire(final Bound bound, final BindingContext context, final Field field) { final boolean[] didBind = new boolean[1]; final List<Binding> bindings = Lists.newArrayList(); Reflections.visitClassHierarchy(field.getType(), new Visitor<Class<?>>() { public void visit(Class<?> klass){ if (didBind[0]) { return; } WiringHarness<Bound, Field> wiringHarness = wiringHarnesses.get(klass); if (wiringHarness == null) { return; } try { bindings.addAll(wiringHarness.wire(bound, context, field)); didBind[0] = true; } catch (Exception e) { throw Throwables.throwUncheckedException(e); } } }); if (!didBind[0]) { throw new BindingException("don't know how to wire up @Bound field: " + field.getName()); } return bindings; } }