Java SE API

Class.getAnnotationsByType()

Return associated annotations, expanding repeatable containers. See official API for exceptions and overloads; Java 21-compatible example.

Formal Syntax & Structure

java syntaxjava
<A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass)

Parameters & Attributes

NameTypeDescription
annotationClassClass<A>Annotation interface

Return Value

<A extends Annotation> A[]

Return associated annotations, expanding repeatable containers.

Working Example

Example Snippetjava
import java.lang.annotation.*;import java.lang.reflect.*;import java.lang.invoke.*;import java.util.*;import java.nio.file.*;import java.nio.charset.StandardCharsets;import java.net.URLClassLoader;import java.net.URL;public class Main {    @Retention(RetentionPolicy.RUNTIME)    @Target(ElementType.TYPE)    @Repeatable(Tags.class)    @interface Tag { String value(); }    @Retention(RetentionPolicy.RUNTIME)    @Target(ElementType.TYPE)    @interface Tags { Tag[] value(); }    @Tag("billing") @Tag("reviewed") static class Report {}    public static void main(String[] args) throws Throwable {        for (Tag tag : Report.class.getAnnotationsByType(Tag.class)) System.out.println(tag.value());    }}
Java Reference