Search in sources :

Example 1 with Inherited

use of java.lang.annotation.Inherited in project libgdx by libgdx.

the class ClassReflection method getAnnotations.

/** Returns an array of {@link Annotation} objects reflecting all annotations declared by the supplied class, and inherited
	 * from its superclass. Returns an empty array if there are none. */
public static Annotation[] getAnnotations(Class c) {
    Type declType = ReflectionCache.getType(c);
    java.lang.annotation.Annotation[] annotations = declType.getDeclaredAnnotations();
    // annotations of supplied class
    Annotation[] result = new Annotation[annotations.length];
    for (int i = 0; i < annotations.length; i++) {
        result[i] = new Annotation(annotations[i]);
    }
    // search super classes, until Object.class is reached
    Type superType = declType.getSuperclass();
    java.lang.annotation.Annotation[] superAnnotations;
    while (!superType.getClassOfType().equals(Object.class)) {
        superAnnotations = superType.getDeclaredAnnotations();
        for (int i = 0; i < superAnnotations.length; i++) {
            // check for annotation types marked as Inherited
            Type annotationType = ReflectionCache.getType(superAnnotations[i].annotationType());
            if (annotationType.getDeclaredAnnotation(Inherited.class) != null) {
                // ignore duplicates
                boolean duplicate = false;
                for (Annotation annotation : result) {
                    if (annotation.getAnnotationType().equals(annotationType)) {
                        duplicate = true;
                        break;
                    }
                }
                // append to result set
                if (!duplicate) {
                    Annotation[] copy = new Annotation[result.length + 1];
                    for (int j = 0; j < result.length; j++) {
                        copy[j] = result[j];
                    }
                    copy[result.length] = new Annotation(superAnnotations[i]);
                    result = copy;
                }
            }
        }
        superType = superType.getSuperclass();
    }
    return result;
}
Also used : Type(com.badlogic.gwtref.client.Type) Inherited(java.lang.annotation.Inherited)

Example 2 with Inherited

use of java.lang.annotation.Inherited in project guava by google.

the class FeatureEnumTest method assertGoodTesterAnnotation.

private static void assertGoodTesterAnnotation(Class<? extends Annotation> annotationClass) {
    assertNotNull(rootLocaleFormat("%s must be annotated with @TesterAnnotation.", annotationClass), annotationClass.getAnnotation(TesterAnnotation.class));
    final Retention retentionPolicy = annotationClass.getAnnotation(Retention.class);
    assertNotNull(rootLocaleFormat("%s must have a @Retention annotation.", annotationClass), retentionPolicy);
    assertEquals(rootLocaleFormat("%s must have RUNTIME RetentionPolicy.", annotationClass), RetentionPolicy.RUNTIME, retentionPolicy.value());
    assertNotNull(rootLocaleFormat("%s must be inherited.", annotationClass), annotationClass.getAnnotation(Inherited.class));
    for (String propertyName : new String[] { "value", "absent" }) {
        Method method = null;
        try {
            method = annotationClass.getMethod(propertyName);
        } catch (NoSuchMethodException e) {
            fail(rootLocaleFormat("%s must have a property named '%s'.", annotationClass, propertyName));
        }
        final Class<?> returnType = method.getReturnType();
        assertTrue(rootLocaleFormat("%s.%s() must return an array.", annotationClass, propertyName), returnType.isArray());
        assertSame(rootLocaleFormat("%s.%s() must return an array of %s.", annotationClass, propertyName, annotationClass.getDeclaringClass()), annotationClass.getDeclaringClass(), returnType.getComponentType());
    }
}
Also used : Inherited(java.lang.annotation.Inherited) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention)

Aggregations

Inherited (java.lang.annotation.Inherited)2 Type (com.badlogic.gwtref.client.Type)1 Retention (java.lang.annotation.Retention)1 Method (java.lang.reflect.Method)1