Close

Java Reflection - Method.getAnnotationsByType() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)

Returns annotations that are associated with this element. If there are no annotations associated with this element, the return value is an array of length 0. The difference between this method and getAnnotation(java.lang.Class) is that this method detects if its argument is a repeatable annotation type, and if so, attempts to find one or more annotations of that type by "looking through" a container annotation.


Examples


package com.logicbig.example.method;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Arrays;

public class GetAnnotationsByTypeExample {
private static class Test {
@Deprecated
@Async
public void process() {
}
}

public static void main(String... args) throws NoSuchMethodException {
Method m = Test.class.getMethod("process");
Async[] a = m.getAnnotationsByType(Async.class);
System.out.println(a.length);
Arrays.stream(a).forEach(System.out::println);
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
private @interface Async {}

}

Output

1
@com.logicbig.example.method.GetAnnotationsByTypeExample$Async()




package com.logicbig.example.method;

import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.Arrays;

public class GetAnnotationsByTypeExample2 {

private static class Test {
@Access("Admin")
@Access("Super User")
private void process() {}
}

public static void main(String... args) throws NoSuchMethodException {
Method m = Test.class.getDeclaredMethod("process");
Access[] a = m.getAnnotationsByType(Access.class);
System.out.println(Arrays.toString(a));

Access[] a2 = m.getDeclaredAnnotationsByType(Access.class);//no difference
System.out.println(Arrays.toString(a2));

Access a3 = m.getAnnotation(Access.class);
System.out.println(a3);
}

@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MultiAccess.class)
public @interface Access {
String value() default "ADMIN";
}

@Retention(RetentionPolicy.RUNTIME)
public @interface MultiAccess {
Access[] value() default @Access;
}

}

Output

[@com.logicbig.example.method.GetAnnotationsByTypeExample2$Access(value="Admin"), @com.logicbig.example.method.GetAnnotationsByTypeExample2$Access(value="Super User")]
[@com.logicbig.example.method.GetAnnotationsByTypeExample2$Access(value="Admin"), @com.logicbig.example.method.GetAnnotationsByTypeExample2$Access(value="Super User")]
null




See Also