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;
}
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");
}
Aggregations