List of utility methods to do Reflection Annotation
Class extends Annotation> | getAnnotationClass(Annotation annotation) Resolve the impl's class. return annotation.annotationType();
|
Class | getAnnotationClass(Class> clazz, Class Loads annotation class, using class loader of the specified class. ClassLoader clazzClassLoader = clazz.getClassLoader(); ClassLoader annotationClassLoader = annotation.getClassLoader(); if (null != clazzClassLoader && !clazzClassLoader.equals(annotationClassLoader)) { try { return (Class<T>) clazzClassLoader.loadClass(annotation.getName()); } catch (ClassNotFoundException e) { return annotation; |
T | getAnnotationClass(Class> entityClass, Class Return a specific annotation class if exist in class, otherwise return false return hasAnnotationClass(entityClass, annotationClass) ? entityClass.getAnnotation(annotationClass) : null;
|
Class extends Annotation> | getAnnotationClass(String name) get Annotation Class try { return (Class<? extends Annotation>) Class.forName(name); } catch (ClassNotFoundException e) { return null; |
Annotation | getAnnotationDeep(Annotation from, Class extends Annotation> toFind) This method is looking for the annotation class toFind from the annoFrom annotation instance. if (from.annotationType().equals(toFind)) { return from; } else { for (Annotation anno : from.annotationType().getAnnotations()) { if (!anno.annotationType().getPackage().getName().startsWith(JAVA_LANG)) { return getAnnotationDeep(anno, toFind); return null; |
T | getAnnotationDefault(Class extends Annotation> annotationClass, String element) get Annotation Default Method method = null; try { method = annotationClass.getMethod(element, (Class[]) null); } catch (NoSuchMethodException e) { return null; return ((T) method.getDefaultValue()); |
Map | getAnnotationDefaultMap(Class get Annotation Default Map return Stream.of(annotationClass.getDeclaredMethods()).filter(m -> m.getDefaultValue() != null)
.collect(Collectors.toMap(m -> m.getName(), m -> m.getDefaultValue()));
|
T | getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName, String annotationElementName, Class get Annotation Element Value final Annotation annotation = getAnnotation(annotatedElement, annotationClassName); return getAnnotationElementValue(annotation, annotationElementName, annotationElementType); |
Object | getAnnotationField(Annotation annotation, String field) Access to the value of a field in an annotation using reflection. Class<?> annotClass = annotation.getClass(); try { Method getValue = annotClass.getMethod(field); return getValue.invoke(annotation); } catch (Exception e) { e.printStackTrace(); return null; |
List | getAnnotationFields(Class claz, Class annotationType) get fields list of a type, and all the fields returned contains the given annotation List<Field> result = new ArrayList<Field>(); if (claz != null) { Field[] fields = claz.getDeclaredFields(); for (Field field : fields) { if (field.getAnnotation(annotationType) != null) { result.add(field); result.addAll(getAnnotationFields(claz.getSuperclass(), annotationType)); return result; |