Java Reflection - Class.getAnnotations() Examples

Code Snippets Java Reflection Java 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Annotation[] getAnnotations()

Description copied from interface: <java_se_link>java.lang.reflect.AnnotatedElement#getAnnotations--,AnnotatedElement</java_se_link>Returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Specified by:
<java_se_link>java.lang.reflect.AnnotatedElement#getAnnotations--,getAnnotations</java_se_link> in interface <java_se_link>java.lang.reflect.AnnotatedElement,AnnotatedElement</java_se_link>
Returns:
annotations present on this element
Since:
1.5


Examples


package com.logicbig.example.clazz;

import java.lang.annotation.Annotation;
import java.util.Arrays;

public class GetAnnotationsExample {

public static void main(String... args) {
Annotation[] annotations = MyClass.class.getAnnotations();
String s = Arrays.toString(annotations);
System.out.println(s);
}

@Deprecated
@FunctionalInterface
private static interface MyClass {
void doSomething();
}
}

Output

[@java.lang.Deprecated(forRemoval=false, since=""), @java.lang.FunctionalInterface()]




See Also