@Test public void getFieldsAnnotatedWith() { Set<Field> found = reflectionsToTest.getFieldsAnnotatedWith(ReflectionsExampleUsageAnnotation.class); assertNotNull(found); assertEquals(4, found.size()); Set<String> sortedSet = new TreeSet<>(); for (Field field : found) { sortedSet.add(field.toString()); } assertFound("private java.lang.String com.daimler.sechub.docgen.reflections.ReflectionsExampleClass1.field1",sortedSet); assertFound("private java.lang.String com.daimler.sechub.docgen.reflections.ReflectionsExampleClass1.field2",sortedSet); assertFound("private java.lang.String com.daimler.sechub.docgen.reflections.ReflectionsExampleClass2.field1",sortedSet); assertFound("private java.lang.String com.daimler.sechub.docgen.reflections.ReflectionsExampleClass2.field2",sortedSet); }
public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation) { ensureScan(); synchronized (MONITOR) { Set<Field> result = annotationClassToField.get(annotation); if (result != null) { return result; } Set<Field> newResult = new LinkedHashSet<>(); visitEveryClass(new Visitor() { @Override public void visit(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(annotation) != null) { newResult.add(field); } } } }); annotationClassToField.put(annotation, newResult); return newResult; } }
Reflections { public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation) { ensureScan(); synchronized (MONITOR) { Set<Field> result = annotationClassToField.get(annotation); if (result != null) { return result; } Set<Field> newResult = new LinkedHashSet<>(); visitEveryClass(new Visitor() { @Override public void visit(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(annotation) != null) { newResult.add(field); } } } }); annotationClassToField.put(annotation, newResult); return newResult; } } }
Reflections { public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation) { ensureScan(); synchronized (MONITOR) { Set<Field> result = annotationClassToField.get(annotation); if (result != null) { return result; } Set<Field> newResult = new LinkedHashSet<>(); visitEveryClass(new Visitor() { @Override public void visit(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(annotation) != null) { newResult.add(field); } } } }); annotationClassToField.put(annotation, newResult); return newResult; } } Reflections(); }
Reflections { public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation) { ensureScan(); synchronized (MONITOR) { Set<Field> result = annotationClassToField.get(annotation); if (result != null) { return result; } Set<Field> newResult = new LinkedHashSet<>(); visitEveryClass(new Visitor() { @Override public void visit(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(annotation) != null) { newResult.add(field); } } } }); annotationClassToField.put(annotation, newResult); return newResult; } } Reflections(); Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation); Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation); Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation); Set<Class<? extends T>> getSubTypesOf(Class<T> clazz); boolean isInspecting(Class<?> clazz); int getAmountOfInspectedClasses(); }
Reflections { public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation) { ensureScan(); synchronized (MONITOR) { Set<Field> result = annotationClassToField.get(annotation); if (result != null) { return result; } Set<Field> newResult = new LinkedHashSet<>(); visitEveryClass(new Visitor() { @Override public void visit(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(annotation) != null) { newResult.add(field); } } } }); annotationClassToField.put(annotation, newResult); return newResult; } } Reflections(); Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation); Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation); Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation); Set<Class<? extends T>> getSubTypesOf(Class<T> clazz); boolean isInspecting(Class<?> clazz); int getAmountOfInspectedClasses();  }