Skip to content

io microsphere util AnnotationUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

AnnotationUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/AnnotationUtils.java

Overview

Annotation Utilities class

Declaration

public abstract class AnnotationUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isType

Class<?> clazz = String.class;
boolean result = AnnotationUtils.isType(clazz); // true

isSameType

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

class B extends A {
}

DataAccess dataAccessOfA = A.class.getAnnotation(DataAccess.class);
DataAccess dataAccessOfB = B.class.getAnnotation(DataAccess.class);

System.out.println(isSameType(dataAccessOfA, DataAccess.class)); // true
System.out.println(isSameType(dataAccessOfB, DataAccess.class)); // true; @DataAccess is an @Inherited annotation

findAnnotation

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

class B extends A {
}

DataAccess dataAccessOfA = A.class.getAnnotation(DataAccess.class);
DataAccess dataAccessOfB = B.class.getAnnotation(DataAccess.class);

System.out.println(findAnnotation(A.class, DataAccess.class)); // DataAccess
System.out.println(findAnnotation(B.class, DataAccess.class)); // DataAccess; @DataAccess is an @Inherited annotation

findAnnotation

// Example 1: Find an annotation by type using a predicate
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {}

@CustomAnnotation
class MyClass {}

Annotation result = AnnotationUtils.findAnnotation(
    MyClass.class,
    a -> a.annotationType() == CustomAnnotation.class
);
System.out.println(result); // prints @CustomAnnotation

// Example 2: Using multiple filters (e.g., filter by annotation type and additional logic)
Annotation filteredResult = AnnotationUtils.findAnnotation(
    MyClass.class,
    a -> a.annotationType() == CustomAnnotation.class,
    a -> someAdditionalCheck(a) // hypothetical additional check
);

isMetaAnnotation

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

DataAccess dataAccess = findAnnotation(A.class, DataAccess.class);
assertTrue(isMetaAnnotation(dataAccess, Monitored.class));        // true
assertTrue(isMetaAnnotation(dataAccess, ServiceMode.class));      // true
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

DataAccess dataAccess = A.class.getAnnotation(DataAccess.class);
System.out.println(AnnotationUtils.isMetaAnnotation(dataAccess, Monitored.class));   // true
System.out.println(AnnotationUtils.isMetaAnnotation(dataAccess, ServiceMode.class)); // true

isMetaAnnotation

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

DataAccess dataAccess = findAnnotation(A.class, DataAccess.class);
assertTrue(isMetaAnnotation(dataAccess, Monitored.class));        // true
assertTrue(isMetaAnnotation(dataAccess, ServiceMode.class));      // true

isMetaAnnotation

// Example 1: Checking if @Target is a meta-annotation
boolean result = AnnotationUtils.isMetaAnnotation(Target.class);
System.out.println(result); // false, because Target is not annotated with any meta-annotation

// Example 2: Custom annotation scenario
// Assume we have the following annotations:
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

DataAccess dataAccess = findAnnotation(A.class, DataAccess.class);
System.out.println(isMetaAnnotation(dataAccess, Arrays.asList(Monitored.class))); // true
System.out.println(isMetaAnnotation(dataAccess, Arrays.asList(ServiceMode.class))); // true

isMetaAnnotation

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

DataAccess dataAccess = findAnnotation(A.class, DataAccess.class);
assertTrue(isMetaAnnotation(dataAccess, Monitored.class));        // true
assertTrue(isMetaAnnotation(dataAccess, ServiceMode.class));      // true

isMetaAnnotation

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

System.out.println(AnnotationUtils.isMetaAnnotation(DataAccess.class, Monitored.class, ServiceMode.class)); // true
System.out.println(AnnotationUtils.isMetaAnnotation(DataAccess.class, ServiceMode.class, Monitored.class)); // true

isMetaAnnotation

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

System.out.println(AnnotationUtils.isMetaAnnotation(DataAccess.class, Arrays.asList(Monitored.class, ServiceMode.class)));   // true
System.out.println(AnnotationUtils.isMetaAnnotation(DataAccess.class, Arrays.asList(ServiceMode.class, Monitored.class)));   // true

getAllDeclaredAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

System.out.println(getAllDeclaredAnnotations(A.class)); // Outputs: [ @DataAccess ]
System.out.println(getAllDeclaredAnnotations(DataAccess.class)); // Outputs: [ @Inherited , @Target , @Retention , @Monitored ]

getDeclaredAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

@Since(module = "microsphere-java-core", value = "1.0.0")
class B extends A {
}

System.out.println(getDeclaredAnnotations(A.class)); // Outputs: [ @DataAccess ]
System.out.println(getDeclaredAnnotations(B.class)); // Outputs: [ @DataAccess , @Since ]

findAllDeclaredAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

@Since(module = "microsphere-java-core", value = "1.0.0")
class B extends A {
}

System.out.println(findAllDeclaredAnnotations(A.class, annotation -> true)); // Outputs: [ @DataAccess ]
System.out.println(findAllDeclaredAnnotations(A.class)); // Outputs: [ @DataAccess ]
System.out.println(findAllDeclaredAnnotations(B.class, annotation -> true)); // Outputs: [ @DataAccess , @Since , @DataAccess ]
System.out.println(findAllDeclaredAnnotations(B.class)); // Outputs: [ @DataAccess , @Since , @DataAccess ]

findAllDeclaredAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@DataAccess
class A {
}

@Monitored
class B extends A {
}

System.out.println(findAllDeclaredAnnotations(A.class, annotation -> true)); // Outputs: [ @DataAccess ]
System.out.println(findAllDeclaredAnnotations(A.class)); // Outputs: [ @DataAccess ]
System.out.println(findAllDeclaredAnnotations(B.class, annotation -> true)); // Outputs: [ @DataAccess , @Since , @DataAccess ]
System.out.println(findAllDeclaredAnnotations(B.class)); // Outputs: [ @DataAccess , @Since , @DataAccess ]
System.out.println(findAllDeclaredAnnotations(null)); // Outputs: [ ]
System.out.println(findAllDeclaredAnnotations(A.class, annotation -> false)); // Outputs: [ ]

findDeclaredAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

@Since(module = "microsphere-java-core", value = "1.0.0")
class B extends A {
}

System.out.println(findDeclaredAnnotations(A.class, annotation -> true)); // Outputs: [ @DataAccess ]
System.out.println(findDeclaredAnnotations(A.class)); // Outputs: [ @DataAccess ]
System.out.println(findDeclaredAnnotations(B.class, annotation -> true)); // Outputs: [ @DataAccess , @Since ]
System.out.println(findDeclaredAnnotations(B.class)); // Outputs: [ @DataAccess , @Since ]
System.out.println(findDeclaredAnnotations(null, annotation -> true))); // Outputs: [ ]
System.out.println(findDeclaredAnnotations(A.class, annotation -> false))); // Outputs: [ ]

filterAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

@Since(module = "microsphere-java-core", value = "1.0.0")
class B extends A {
}

Annotation[] annotationsOfA = A.class.getAnnotations();
System.out.println(filterAnnotations(annotationsOfA, annotation -> true)); // Outputs: [ @DataAccess ]
System.out.println(filterAnnotations(annotationsOfA, annotation -> false)); // Outputs: [  ]
System.out.println(filterAnnotations((Annotation[]) null, annotation -> false)); // Outputs: [  ]
System.out.println(filterAnnotations(new Annotation[0], annotation -> false)); // Outputs: [  ]

Annotation[] annotationsOfB = B.class.getAnnotations();
System.out.println(filterAnnotations(annotationsOfB, annotation -> true)); // Outputs: [ @Since, @DataAccess ]
System.out.println(filterAnnotations(annotationsOfB, annotation -> false)); // Outputs: [  ]

filterAnnotations

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@ServiceMode
@interface Monitored {
}

@Inherited
@Target(TYPE)
@Retention(RUNTIME)
@Monitored
@interface DataAccess {
}

@DataAccess
class A {
}

@Since(module = "microsphere-java-core", value = "1.0.0")
class B extends A {
}

Annotation[] annotationsOfA = A.class.getAnnotations();
System.out.println(filterAnnotations(ofList(annotationsOfA), annotation -> true)); // Outputs: [ @DataAccess ]
System.out.println(filterAnnotations(ofList(annotationsOfA), annotation -> false)); // Outputs: [  ]
System.out.println(filterAnnotations((List) null, annotation -> false)); // Outputs: [  ]
System.out.println(filterAnnotations(emptyList(), annotation -> false)); // Outputs: [  ]

Annotation[] annotationsOfB = B.class.getAnnotations();
System.out.println(filterAnnotations(ofList(annotationsOfB), annotation -> true)); // Outputs: [ @Since, @DataAccess ]
System.out.println(filterAnnotations(ofList(annotationsOfB), annotation -> false)); // Outputs: [  ]

findAttributeValue

@Target(TYPE)
@Retention(RUNTIME)
@Inherited
@Documented
public @interface ServiceMode {
    String name() default "default";
}

@ServiceMode(name = "custom")
class A {
}

Annotation[] annotations = A.class.getAnnotations();
String name = AnnotationUtils.findAttributeValue(annotations, "name");
System.out.println(name); // Outputs: custom

getAttributeValue

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {
    String value();
    int count() default 1;
}

@CustomAnnotation(value = "example", count = 5)
class ExampleClass {}

Annotation annotation = ExampleClass.class.getAnnotation(CustomAnnotation.class);
String value = AnnotationUtils.getAttributeValue(annotation, "value"); // returns "example"
Integer count = AnnotationUtils.getAttributeValue(annotation, "count"); // returns 5

exists

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {
    String value() default "default";
    int count() default 1;
}

@CustomAnnotation(value = "example", count = 5)
class ExampleClass {}

Annotation annotation = ExampleClass.class.getAnnotation(CustomAnnotation.class);
Map<String, Object> attributesMap = AnnotationUtils.getAttributesMap(annotation);

System.out.println(attributesMap.get("value"));  // Outputs: example
System.out.println(attributesMap.get("count"));   // Outputs: 5
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {
    String value() default "default";
    int count() default 1;
}

@CustomAnnotation(value = "example", count = 5)
class ExampleClass {}

Annotation annotation = ExampleClass.class.getAnnotation(CustomAnnotation.class);
Map<String, Object> filteredAttributes = AnnotationUtils.findAttributesMap(annotation, name -> "value".equals(name));

System.out.println(filteredAttributes.get("value"));  // Outputs: example
System.out.println(filteredAttributes.containsKey("count"));  // Outputs: false

System.out.println(AnnotationUtils.findAttributesMap(annotation, name -> false)); // Outputs : {}
System.out.println(AnnotationUtils.findAttributesMap(null, name -> true)); // Outputs : {}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {
}

@CustomAnnotation
class ExampleClass {}

Annotation[] annotations = ExampleClass.class.getAnnotations();
boolean result = AnnotationUtils.exists(annotations, CustomAnnotation.class);
System.out.println(result); // true

// When annotations are null or empty
System.out.println(AnnotationUtils.exists((Annotation[]) null, CustomAnnotation.class)); // false
System.out.println(AnnotationUtils.exists(new Annotation[0], CustomAnnotation.class)); // false

// When annotation type is null
System.out.println(AnnotationUtils.exists(annotations, null)); // false

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.AnnotationUtils;

API Reference

Public Methods

Method Description
isType A list of annotation types that are considered native to the Java language.
isSameType Checks whether the specified annotation is of the given annotation type.
findAnnotation Finds the annotation of the specified type that is directly or indirectly
findAnnotation Finds the first annotation of the specified AnnotatedElement that matches all the given filters.
isMetaAnnotation Checks whether the specified annotation is a meta-annotation, which means it's used to annotate other annotations.
isMetaAnnotation Checks whether the specified annotation is a meta-annotation, which means it's used to annotate other annotations.
isMetaAnnotation Checks whether the specified annotation is a meta-annotation, which means it's used to annotate other annotations.
isMetaAnnotation Checks whether the specified annotation type is a meta-annotation.
isMetaAnnotation Checks whether the specified annotation type is a meta-annotation, which means it's used to annotate other annotations.
isMetaAnnotation Checks whether the specified annotation type is a meta-annotation, which means it's used to annotate other annotations.
getAllDeclaredAnnotations Retrieves all declared annotations from the specified AnnotatedElement, including those from its hierarchy,
getDeclaredAnnotations Retrieves the annotations that are directly declared on the specified AnnotatedElement.
findAllDeclaredAnnotations Find all directly declared annotations of the annotated element with filters, not including
findAllDeclaredAnnotations Retrieves all declared annotations from the specified Class, including those from its hierarchy,
findDeclaredAnnotations Retrieves the annotations that are directly declared on the specified AnnotatedElement.
filterAnnotations Filters the given array of annotations based on the provided predicates.
filterAnnotations Filters the given list of annotations based on the provided predicates.
findAttributeValue Finds the value of the specified attribute from the given array of annotations.
getAttributeValue Retrieves the value of the specified attribute from the given annotation.
exists Retrieves a map of attribute names to their corresponding values from the specified annotation.

Method Details

isType

public static boolean isType(AnnotatedElement annotatedElement)

A list of annotation types that are considered native to the Java language.

These annotations are defined by the Java platform and are commonly used for structural or metadata purposes. They include:

  • Target
  • Retention
  • Documented
  • Inherited
  • Native
  • Repeatable

/

isSameType

public static boolean isSameType(Annotation annotation, Class<? extends Annotation> annotationType)

Checks whether the specified annotation is of the given annotation type.

This method compares the type of the provided annotation with the expected annotation type.

Example Usage

{@code

#### `findAnnotation`

```java
public static  A findAnnotation(AnnotatedElement annotatedElement, Class annotationType)
```

Finds the annotation of the specified type that is directly or indirectly
present on the given `AnnotatedElement`.



This method searches for an annotation of the specified type, considering both
direct annotations and meta-annotations (annotations on annotations).

### Example Usage
{@code

#### `findAnnotation`

```java
public static  A findAnnotation(AnnotatedElement annotatedElement,
                                                          Predicate super Annotation>... annotationFilters)
```

Finds the first annotation of the specified `AnnotatedElement` that matches all the given filters.



This method is useful when searching for specific annotations based on custom filtering logic.
The search includes directly declared annotations but does not include meta-annotations.

### Example Usage
{@code
// Example 1: Find an annotation by type using a predicate

#### `isMetaAnnotation`

```java
public static boolean isMetaAnnotation(Annotation annotation,
                                           Class extends Annotation> metaAnnotationType)
```

Checks whether the specified annotation is a meta-annotation, which means it's used to annotate other annotations.



A meta-annotation is an annotation that applies to another annotation type rather than directly to Java elements like classes or methods.

### Example Usage
{@code

#### `isMetaAnnotation`

```java
public static boolean isMetaAnnotation(Annotation annotation,
                                           Class extends Annotation>... metaAnnotationTypes)
```

Checks whether the specified annotation is a meta-annotation, which means it's used to annotate other annotations.



A meta-annotation is an annotation that applies to another annotation type rather than directly to Java elements like classes or methods.

### Example Usage
{@code

#### `isMetaAnnotation`

```java
public static boolean isMetaAnnotation(Annotation annotation,
                                           Iterable> metaAnnotationTypes)
```

Checks whether the specified annotation is a meta-annotation, which means it's used to annotate other annotations.



A meta-annotation is an annotation that applies to another annotation type rather than directly to Java elements like classes or methods.

### Example Usage
{@code
// Example 1: Checking if @Target is a meta-annotation
boolean result = AnnotationUtils.isMetaAnnotation(Target.class);
System.out.println(result); // false, because Target is not annotated with any meta-annotation

// Example 2: Custom annotation scenario
// Assume we have the following annotations:

#### `isMetaAnnotation`

```java
public static boolean isMetaAnnotation(Class extends Annotation> annotationType,
                                           Class extends Annotation> metaAnnotationType)
```

Checks whether the specified annotation type is a meta-annotation.



A meta-annotation is an annotation that applies to another annotation type,
typically used to define composed annotations or custom structured annotations.

### Example Usage
{@code

#### `isMetaAnnotation`

```java
public static boolean isMetaAnnotation(Class extends Annotation> annotationType,
                                           Class extends Annotation>... metaAnnotationTypes)
```

Checks whether the specified annotation type is a meta-annotation, which means it's used to annotate other annotations.



A meta-annotation is an annotation that applies to another annotation type rather than directly to Java elements like classes or methods.

### Example Usage
{@code

#### `isMetaAnnotation`

```java
public static boolean isMetaAnnotation(Class extends Annotation> annotationType,
                                           Iterable> metaAnnotationTypes)
```

Checks whether the specified annotation type is a meta-annotation, which means it's used to annotate other annotations.



A meta-annotation is an annotation that applies to another annotation type rather than directly to Java elements like classes or methods.

### Example Usage
{@code

#### `getAllDeclaredAnnotations`

```java
public static List getAllDeclaredAnnotations(AnnotatedElement annotatedElement)
```

Retrieves all declared annotations from the specified `AnnotatedElement`, including those from its hierarchy,
but excluding meta-annotations (annotations on annotations).



This method is particularly useful when you need to inspect all annotations directly applied
to a class, method, or field, including those inherited from superclasses.

### Example Usage
{@code

#### `getDeclaredAnnotations`

```java
public static List getDeclaredAnnotations(AnnotatedElement annotatedElement)
```

Retrieves the annotations that are *directly declared* on the specified `AnnotatedElement`.



This method returns only the annotations directly present on the element itself, excluding any inherited annotations or meta-annotations.

### Example Usage
{@code

#### `findAllDeclaredAnnotations`

```java
public static List findAllDeclaredAnnotations(AnnotatedElement annotatedElement,
                                                              Predicate super Annotation>... annotationsToFilter)
```

Find all directly declared annotations of the annotated element with filters, not including
meta annotations.

#### `findAllDeclaredAnnotations`

```java
public static List findAllDeclaredAnnotations(Class> type, Predicate super Annotation>... annotationsToFilter)
```

Retrieves all declared annotations from the specified `Class`, including those from its hierarchy,
but excluding meta-annotations (annotations on annotations).



This method is particularly useful when you need to inspect all annotations directly applied
to a class, including those inherited from superclasses. It ensures that each annotation is only included once,
even if it appears in multiple levels of the class hierarchy.

### Example Usage
{@code

#### `findDeclaredAnnotations`

```java
public static List findDeclaredAnnotations(AnnotatedElement annotatedElement,
                                                           Predicate super Annotation>... annotationsToFilter)
```

Retrieves the annotations that are *directly declared* on the specified `AnnotatedElement`.



This method returns only the annotations directly present on the element itself, excluding any inherited annotations or meta-annotations.

### Example Usage
{@code

#### `filterAnnotations`

```java
public static List filterAnnotations(Annotation[] annotations,
                                                     Predicate super Annotation>... annotationsToFilter)
```

Filters the given array of annotations based on the provided predicates.



This method converts the input array into a list and applies the filter using the
`#filterAnnotations(List, Predicate[])` method. If the input array is empty or null,
it returns an empty list.

### Example Usage
{@code

#### `filterAnnotations`

```java
public static List filterAnnotations(List annotations,
                                                     Predicate super Annotation>... annotationsToFilter)
```

Filters the given list of annotations based on the provided predicates.



This method applies each predicate in the array to filter the input list of annotations.
If no filters are provided, it returns an unmodifiable view of the original list.
If any of the filters reject an annotation, it is excluded from the result.

{@code

#### `findAttributeValue`

```java
public static  T findAttributeValue(Annotation[] annotations, String attributeName)
```

Finds the value of the specified attribute from the given array of annotations.



This method iterates through the provided annotations, attempting to retrieve the value of the
named attribute from each. It returns the first non-null value found.

### Example Usage
{@code

#### `getAttributeValue`

```java
public static  T getAttributeValue(Annotation annotation, String attributeName)
```

Retrieves the value of the specified attribute from the given annotation.



This method uses reflection to find the method with the matching name in the annotation's type,
and then invokes it on the provided annotation instance to get the attribute value.

### Example Usage
{@code

#### `exists`

```java
public static boolean exists(Annotation[] annotations, Class extends Annotation> annotationType)
```

Retrieves a map of attribute names to their corresponding values from the specified annotation.



This method uses reflection to extract all declared methods in the annotation's type that are not defined
in the `Object` or `Annotation` interfaces. These methods represent the attributes of the annotation,
and their return values are obtained by invoking them on the provided annotation instance.

### Example Usage
{@code

---

*This documentation was auto-generated from the source code of [microsphere-java](https://github.com/microsphere-projects/microsphere-java).*

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally