List of utility methods to do Reflection Annotation
List | getAnnotations(Object instance) get Annotations return new ArrayList<Annotation>(Arrays.asList(instance.getClass().getAnnotations())); |
Annotation[] | getAnnotations(Set get annotations by given fields and methods List<Annotation> annotations = new LinkedList<Annotation>(); if (fields != null) { for (Field field : fields) { Annotation[] annos = field.getAnnotations(); if (annos != null) { for (Annotation annotation : annos) { annotations.add(annotation); if (methods != null) { for (Method method : methods) { Annotation[] annos = method.getAnnotations(); if (annos != null) { for (Annotation annotation : annos) { annotations.add(annotation); return annotations.toArray(new Annotation[annotations.size()]); |
Annotation[] | getAnnotationsOf(Class annoClass) get Annotations Of return annoClass.getAnnotations();
|
Collection | getAnnotationsRecursive(Class> clazz) get Annotations Recursive Collection<Annotation> allAnnotations = new HashSet<>(); getAllAnnotations(clazz, allAnnotations); return allAnnotations; |
List | getAnnotationsRecursive(final Class> type, final Class get Annotations Recursive List<T> annotations = new ArrayList<T>(); Class<?> current = type; while (!Object.class.equals(current)) { T annotation = current.getAnnotation(annotationType); if (annotation != null) { annotations.add(annotation); current = current.getSuperclass(); ... |
V | getAnnotationValue(@Nonnull Class> cls, @Nonnull Class annotation, V def) get Annotation Value final A a = getAnnotation(cls, annotation); return a != null ? get(a, "value") : def; |
String | getAnnotationValue(Annotation annotation, String attributeName) get Annotation Value String value = null; if (annotation != null) { try { value = (String) annotation.annotationType().getMethod(attributeName).invoke(annotation); } catch (Exception ignore) { ignore.printStackTrace(); return value; |
T | getAnnotationValue(Annotation annotation, String value, Class Return a value from an annotation. Class<? extends Annotation> annotationType = annotation.annotationType(); Method valueMethod; try { valueMethod = annotationType.getDeclaredMethod(value); } catch (NoSuchMethodException e) { throw new IllegalStateException( "Cannot resolve required method '" + value + "()' for '" + annotationType + "'."); Object elementValue; try { elementValue = valueMethod.invoke(annotation); } catch (ReflectiveOperationException e) { throw new IllegalStateException("Cannot invoke method value() for " + annotationType); return elementValue != null ? expectedType.cast(elementValue) : null; |
Object | getAnnotationValue(Annotation annotation, String valueName) get Annotation Value try { return annotation.annotationType().getMethod(valueName).invoke(annotation); } catch (Exception e) { throw new RuntimeException(e); |
Object | getAnnotationValue(Class extends Annotation> anno, Field field, String paramName) get Annotation Value Object value = null; if (hasAnnotation(anno, field) && (paramName != null && !paramName.isEmpty())) { value = callMethodNamed(paramName, field.getAnnotation(anno)); return value; |