List of utility methods to do Reflection Annotation
T | getAnnotation(ProceedingJoinPoint pjp, Class get Annotation final MethodSignature methodSignature = ((MethodSignature) pjp.getSignature()); final Annotation[] annotations = methodSignature.getMethod().getAnnotations(); T lookupAnnotation = null; for (Annotation annotation : annotations) { if (annotation.annotationType().equals(annotationClass)) { lookupAnnotation = (T) annotation; break; return lookupAnnotation; |
Object | getAnnotationAttribute(Annotation a, String attrName) Returns attribute value of given annotation with attrName attribute name.
try { Method method = a.annotationType().getMethod(attrName); return method.invoke(a); } catch (NoSuchMethodException e) { throw new AssertionError(e); } catch (SecurityException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { ... |
Optional | getAnnotationAttribute(final Annotation anno, final String attrName, final Class get Annotation Attribute try { final Method method = anno.annotationType().getMethod(attrName); method.setAccessible(true); if (!attrType.equals(method.getReturnType())) { return Optional.empty(); final Object value = method.invoke(anno); return Optional.of((T) value); ... |
Map | getAnnotationAttributes(Annotation annotation) get Annotation Attributes Map attrs = new HashMap(); Method methods[] = annotation.annotationType().getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; if (method.getParameterTypes().length != 0 || method.getReturnType() == Void.TYPE) continue; try { attrs.put(method.getName(), method.invoke(annotation, new Object[0])); ... |
Map | getAnnotationAttributes(Annotation annotation) get Annotation Attributes return getAnnotationAttributes(annotation, false);
|
Map | getAnnotationAttributes(final Annotation annotation) Retrieve the given annotation's attributes as a Map. final Map<String, Object> attrs = new HashMap<String, Object>(); final Method[] methods = annotation.annotationType().getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { final Method method = methods[j]; if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) { try { attrs.put(method.getName(), method.invoke(annotation)); } catch (final Exception ex) { ... |
String | getAnnotationAttributeValue(final Annotation annotation, final String attributeName) Retrieve the value of an Annotation's attribute String value = null; if (annotation != null) { try { value = annotation.annotationType().getMethod(attributeName).invoke(annotation).toString(); } catch (Exception ex) { return value; ... |
Annotation | getAnnotationByType(AnnotatedElement element, Class annotationType) get Annotation By Type Annotation[] annotations = element.getAnnotations(); for (Annotation annotation : annotations) { if (annotation.annotationType().equals(annotationType)) { return annotation; return null; |
Annotation | getAnnotationByType(List get Annotation By Type if (annotations == null) { return null; return getAnnotationByType(annotations.toArray(NO_ANNOTATIONS), annotationType); |
Class extends Annotation> | getAnnotationClass(Annotation a) get Annotation Class Class<?>[] interfaces = a.getClass().getInterfaces(); assert interfaces.length == 1 : "Customize annotations with multiple interfaces are not supported: " + a.getClass(); return (Class<? extends Annotation>) interfaces[0]; |