Java Utililty Methods Reflection Annotation

List of utility methods to do Reflection Annotation

  1. HOME
  2. Java
  3. R
  4. Reflection Annotation

Description

The list of methods to do Reflection Annotation are organized into topic(s).

Method

T getAnnotation(final AnnotatedElement anElement, final Class annotationClass)
get Annotation
return anElement != null ? (T) anElement.getAnnotation(annotationClass) : null;
T getAnnotation(final Method method, final Class annotationClass)
get Annotation
final T annotation = method.getAnnotation(annotationClass);
if (annotation != null) {
 return annotation;
final Class<?> declaringClass = method.getDeclaringClass();
Class<?> currentSuperClass = declaringClass.getSuperclass();
while (currentSuperClass != null) {
 try {
...
T getAnnotation(@Nonnull Annotation[] annotations, @Nonnull Class annotation)
Retrieve the annotation of given type from array of annotations
if (annotations == null || annotations.length == 0) {
 return null;
for (Annotation a : annotations) {
 if (annotation.isAssignableFrom(a.getClass())) {
 return (T) a;
return null;
A getAnnotation(@Nonnull Class cls, @Nonnull Class annotation)
get Annotation
return annotation.cast(cls.getAnnotation(annotation));
T getAnnotation(AnnotatedElement ae, Class annotationType)
Get a single Annotation of annotationType from the supplied Method, Constructor or Field.
T ann = ae.getAnnotation(annotationType);
if (ann == null) {
 for (Annotation metaAnn : ae.getAnnotations()) {
 ann = metaAnn.annotationType().getAnnotation(annotationType);
 if (ann != null) {
 break;
return ann;
Object getAnnotation(AnnotatedElement aobj, Class aClass)
Get annotation of an object via reflection
for (Object a : aobj.getAnnotations()) {
 if (isAnnotationInstance(aClass, a))
 return a;
return null;
Annotation getAnnotation(AnnotatedElement element, Class annotation)
get Annotation
return element.getAnnotation(annotation);
Annotation getAnnotation(AnnotatedElement element, String annotationTypeName)
get Annotation
Class<?> annotationType = null; 
try {
 annotationType = Class.forName(annotationTypeName);
} catch (Exception ex) {
 throw new IllegalArgumentException(ex);
return element.getAnnotation(annotationType.asSubclass(Annotation.class));
Annotation getAnnotation(AnnotatedElement target, String annotationType)
get Annotation
for (Annotation annotation : target.getAnnotations()) {
 if (annotationType.equals(annotation.annotationType().getName())) {
 return annotation;
return null;
T getAnnotation(Annotation ann, Class annotationType)
get Annotation
if (annotationType.isInstance(ann)) {
 return (T) ann;
return ann.annotationType().getAnnotation(annotationType);


AltStyle によって変換されたページ (->オリジナル) /