Search in sources :

Example 1 with MethodMirror

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

the class ClassDiscovery method getMethodsWithAnnotation.

/**
 * Returns all methods, including constructors, with the specified annotations
 *
 * @param annotation
 * @return
 */
public Set<MethodMirror> getMethodsWithAnnotation(Class<? extends Annotation> annotation) {
    if (methodAnnotationCache.containsKey(annotation)) {
        return new HashSet<>(methodAnnotationCache.get(annotation));
    }
    doDiscovery();
    Set<MethodMirror> mirrors = new HashSet<>();
    for (ClassMirror<?> m : getKnownClasses()) {
        for (MethodMirror mm : m.getMethods()) {
            if (mm.hasAnnotation(annotation)) {
                mirrors.add(mm);
            }
        }
    }
    methodAnnotationCache.put(annotation, mirrors);
    return mirrors;
}
Also used : AbstractMethodMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.AbstractMethodMirror) MethodMirror(com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.MethodMirror) HashSet(java.util.HashSet)

Example 2 with MethodMirror

use of com.laytonsmith.PureUtilities.ClassLoading.ClassMirror.MethodMirror 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

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