Search in sources :

Example 1 with FieldMirror

use of com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.FieldMirror in project CommandHelper by EngineHub.

the class ClassDiscovery method getFieldsWithAnnotation.

/**
 * Returns a list of fields that have been annotated with the specified annotation. This will work with annotations
 * that have been declared with the {@link RetentionPolicy#CLASS} property.
 *
 * @param annotation
 * @return
 */
public Set<FieldMirror> getFieldsWithAnnotation(Class<? extends Annotation> annotation) {
    if (fieldAnnotationCache.containsKey(annotation)) {
        return new HashSet<>(fieldAnnotationCache.get(annotation));
    }
    doDiscovery();
    Set<FieldMirror> mirrors = new HashSet<>();
    for (ClassMirror<?> m : getKnownClasses()) {
        for (FieldMirror f : m.getFields()) {
            if (f.hasAnnotation(annotation)) {
                mirrors.add(f);
            }
        }
    }
    fieldAnnotationCache.put(annotation, mirrors);
    return mirrors;
}
Also used : FieldMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.FieldMirror) HashSet(java.util.HashSet)

Example 2 with FieldMirror

use of com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.FieldMirror in project CommandHelper by EngineHub.

the class GeneralTest method testAnnotationValue.

@Test
public void testAnnotationValue() throws Exception {
    ClassMirror<?> c1 = ClassDiscovery.getDefaultInstance().forName(GeneralTest.class.getName());
    ClassMirror<GeneralTest> c2 = new ClassMirror<>(GeneralTest.class);
    FieldMirror f1 = c1.getField("field");
    FieldMirror f2 = c1.getField("field");
    MethodMirror m1 = c1.getMethod("method", new Class[] {});
    MethodMirror m2 = c2.getMethod("method", new Class[] {});
    assertEquals(c1.loadAnnotation(TestAnnotation.class).value(), "value");
    assertEquals(c2.loadAnnotation(TestAnnotation.class).value(), "value");
    assertEquals(f1.loadAnnotation(TestAnnotation.class).value(), "field");
    assertEquals(f2.loadAnnotation(TestAnnotation.class).value(), "field");
    assertEquals(m1.loadAnnotation(TestAnnotation.class).value(), "method");
    assertEquals(m2.loadAnnotation(TestAnnotation.class).value(), "method");
}
Also used : FieldMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.FieldMirror) MethodMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.MethodMirror) ClassMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.ClassMirror) Test(org.junit.Test)

Aggregations

FieldMirror (com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.FieldMirror)2 ClassMirror (com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.ClassMirror)1 MethodMirror (com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.MethodMirror)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1