Java Method .getDeclaredAnnotations ()
Syntax
Method.getDeclaredAnnotations() has the following syntax.
public Annotation [] getDeclaredAnnotations()
Example
In the following code shows how to use Method.getDeclaredAnnotations() method.
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
//w ww . j ava2s. c o m
class X {
@Deprecated
public void objectMethod(String arg) {
System.out.println("Instance method: " + arg);
}
public static void classMethod() {
System.out.println("Class method");
}
}
public class Main {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("X");
X x = (X) clazz.newInstance();
Class[] argTypes = { String.class };
Method method = clazz.getMethod("objectMethod", argTypes);
Annotation[] annos = method.getAnnotations();
for(Annotation anno:annos){
System.out.println(anno);
}
System.out.println();
} catch (Exception e) {
System.err.println(e);
}
}
}
The code above generates the following result.