Java : AnnotatedElement with Examples

AnnotatedElement (Java SE 20 & JDK 20) API Examples.
You will find code examples on most AnnotatedElement methods.


Summary

Represents an annotated construct of the program currently running in this VM. A construct is either an element or a type. Annotations on an element are on a declaration, whereas annotations on a type are on a specific use of a type name.

Class diagram

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Foo {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Bar {
}

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

@Foo
class A {
    @Bar
    int num;

    @Baz
    void aaa() {
    }
}
final AnnotatedElement element = A.class;

final var foo = element.getDeclaredAnnotation(Foo.class);
System.out.println(foo); // @Foo()
final AnnotatedElement element = A.class.getDeclaredField("num");

final var bar = element.getDeclaredAnnotation(Bar.class);
System.out.println(bar); // @Bar()
final AnnotatedElement element = A.class.getDeclaredMethod("aaa");

final var bar = element.getDeclaredAnnotation(Baz.class);
System.out.println(bar); // @Baz()

Methods

<T extends Annotation> T getAnnotation (Class<T> annotationClass)

Returns this element's annotation for the specified type if such an annotation is present, else null.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Foo {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Bar {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Baz {
}

@Foo
class A {
}

@Bar
class B extends A {
    @Baz
    int num;
}
final AnnotatedElement element = B.class;

final var ret1 = element.getAnnotation(Foo.class);
System.out.println(ret1); // @Foo()

final var ret2 = element.getDeclaredAnnotation(Foo.class);
System.out.println(ret2); // null
final AnnotatedElement element = B.class;

final var ret1 = element.getAnnotation(Bar.class);
System.out.println(ret1); // @Bar()

final var ret2 = element.getDeclaredAnnotation(Bar.class);
System.out.println(ret2); // @Bar()
final AnnotatedElement element = B.class.getDeclaredField("num");

final var ret1 = element.getAnnotation(Baz.class);
System.out.println(ret1); // @Baz()

final var ret2 = element.getDeclaredAnnotation(Baz.class);
System.out.println(ret2); // @Baz()

Annotation[] getAnnotations ()

Returns annotations that are present on this element.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Foo {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Bar {
}

@Foo
class A {
}

@Bar
class B extends A {
}
final AnnotatedElement element = B.class;

for (final var a : element.getAnnotations()) {
    System.out.println(a);
}

// Result
// ↓
//@Foo()
//@Bar()
final AnnotatedElement element = B.class;

for (final var a : element.getDeclaredAnnotations()) {
    System.out.println(a);
}

// Result
// ↓
//@Bar()

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

Returns annotations that are associated with this element.

@Repeatable(FooArray.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Foo {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface FooArray {
    Foo[] value();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Bar {
}

@Foo("a1")
@Foo("a2")
class A {
}

@Bar
class B extends A {
}
final AnnotatedElement element = B.class;

System.out.println("-- Foo --");
for (final var a : element.getAnnotationsByType(Foo.class)) {
    System.out.println(a.annotationType().getSimpleName() + " : " + a.value());
}

System.out.println("-- Bar --");
for (final var a : element.getAnnotationsByType(Bar.class)) {
    System.out.println(a.annotationType().getSimpleName());
}

System.out.println("-- end --");

// Result
// ↓
//-- Foo --
//Foo : a1
//Foo : a2
//-- Bar --
//Bar
//-- end --
final AnnotatedElement element = B.class;

System.out.println("-- Foo --");
for (final var a : element.getDeclaredAnnotationsByType(Foo.class)) {
    System.out.println(a.annotationType().getSimpleName() + " : " + a.value());
}

System.out.println("-- Bar --");
for (final var a : element.getDeclaredAnnotationsByType(Bar.class)) {
    System.out.println(a.annotationType().getSimpleName());
}

System.out.println("-- end --");

// Result
// ↓
//-- Foo --
//-- Bar --
//Bar
//-- end --

default <T extends Annotation> T getDeclaredAnnotation (Class<T> annotationClass)

Returns this element's annotation for the specified type if such an annotation is directly present, else null.

Please see getAnnotation(Class<T> annotationClass).

Annotation[] getDeclaredAnnotations ()

Returns annotations that are directly present on this element.

Please see getAnnotations().

default <T extends Annotation> T[] getDeclaredAnnotationsByType (Class<T> annotationClass)

Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

Please see getAnnotationsByType(Class<T> annotationClass).

default boolean isAnnotationPresent (Class<? extends Annotation> annotationClass)

Returns true if an annotation for the specified type is present on this element, else false.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Foo {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Bar {
}

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

@Foo
class A {
    @Bar
    int num;

    @Baz
    void aaa() {
    }
}
final AnnotatedElement element = A.class;

System.out.println(element.isAnnotationPresent(Foo.class)); // true
System.out.println(element.isAnnotationPresent(Bar.class)); // false
System.out.println(element.isAnnotationPresent(Baz.class)); // false
final AnnotatedElement element = A.class.getDeclaredField("num");

System.out.println(element.isAnnotationPresent(Foo.class)); // false
System.out.println(element.isAnnotationPresent(Bar.class)); // true
System.out.println(element.isAnnotationPresent(Baz.class)); // false
final AnnotatedElement element = A.class.getDeclaredMethod("aaa");

System.out.println(element.isAnnotationPresent(Foo.class)); // false
System.out.println(element.isAnnotationPresent(Bar.class)); // false
System.out.println(element.isAnnotationPresent(Baz.class)); // true

Related posts

To top of page