org.springframework.core.annotation.MergedAnnotations Maven / Gradle / Ivy
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Provides access to a collection of merged annotations, usually obtained
* from a source such as a {@link Class} or {@link Method}.
*
* Each merged annotation represents a view where the attribute values may be
* "merged" from different source values, typically:
*
*
* - Explicit and Implicit {@link AliasFor @AliasFor} declarations on one or
* more attributes within the annotation
* - Explicit {@link AliasFor @AliasFor} declarations for a meta-annotation
* - Convention based attribute aliases for a meta-annotation
* - From a meta-annotation declaration
*
*
* For example, a {@code @PostMapping} annotation might be defined as follows:
*
*
* @Retention(RetentionPolicy.RUNTIME)
* @RequestMapping(method = RequestMethod.POST)
* public @interface PostMapping {
*
* @AliasFor(attribute = "path")
* String[] value() default {};
*
* @AliasFor(attribute = "value")
* String[] path() default {};
* }
*
*
* If a method is annotated with {@code @PostMapping("/home")} it will contain
* merged annotations for both {@code @PostMapping} and the meta-annotation
* {@code @RequestMapping}. The merged view of the {@code @RequestMapping}
* annotation will contain the following attributes:
*
*
*
* Name
* Value
* Source
*
*
* value
* "/home"
* Declared in {@code @PostMapping}
*
*
* path
* "/home"
* Explicit {@code @AliasFor}
*
*
* method
* RequestMethod.POST
* Declared in meta-annotation
*
*
*
* {@code MergedAnnotations} can be obtained {@linkplain #from(AnnotatedElement)
* from} any Java {@link AnnotatedElement}. They may also be used for sources that
* don't use reflection (such as those that directly parse bytecode).
*
*
Different {@linkplain SearchStrategy search strategies} can be used to locate
* related source elements that contain the annotations to be aggregated. For
* example, the following code uses {@link SearchStrategy#TYPE_HIERARCHY} to
* search for annotations on {@code MyClass} as well as in superclasses and implemented
* interfaces.
*
*
* MergedAnnotations mergedAnnotations =
* MergedAnnotations.search(TYPE_HIERARCHY).from(MyClass.class);
*
*
* From a {@code MergedAnnotations} instance you can either
* {@linkplain #get(String) get} a single annotation, or {@linkplain #stream()
* stream all annotations} or just those that match {@linkplain #stream(String)
* a specific type}. You can also quickly tell if an annotation
* {@linkplain #isPresent(String) is present}.
*
*
Here are some typical examples:
*
*
* // is an annotation present or meta-present?
* mergedAnnotations.isPresent(ExampleAnnotation.class);
*
* // get the merged "value" attribute of ExampleAnnotation (either directly or
* // meta-present)
* mergedAnnotations.get(ExampleAnnotation.class).getString("value");
*
* // get all meta-annotations but no directly present annotations
* mergedAnnotations.stream().filter(MergedAnnotation::isMetaPresent);
*
* // get all ExampleAnnotation declarations (including any meta-annotations) and
* // print the merged "value" attributes
* mergedAnnotations.stream(ExampleAnnotation.class)
* .map(mergedAnnotation -> mergedAnnotation.getString("value"))
* .forEach(System.out::println);
*
*
* NOTE: The {@code MergedAnnotations} API and its underlying model have
* been designed for composable annotations in Spring's common component model,
* with a focus on attribute aliasing and meta-annotation relationships.
* There is no support for retrieving plain Java annotations with this API;
* please use standard Java reflection or Spring's {@link AnnotationUtils}
* for simple annotation retrieval purposes.
*
* @author Phillip Webb
* @author Sam Brannen
* @since 5.2
* @see MergedAnnotation
* @see MergedAnnotationCollectors
* @see MergedAnnotationPredicates
* @see MergedAnnotationSelectors
*/
public interface MergedAnnotations extends Iterable> {
/**
* Determine if the specified annotation type is either directly present or
* meta-present.
* Equivalent to calling {@code get(annotationType).isPresent()}.
* @param annotationType the annotation type to check
* @return {@code true} if the annotation is present
*/
boolean isPresent(Class annotationType);
/**
* Determine if the specified annotation type is either directly present or
* meta-present.
* Equivalent to calling {@code get(annotationType).isPresent()}.
* @param annotationType the fully qualified class name of the annotation type
* to check
* @return {@code true} if the annotation is present
*/
boolean isPresent(String annotationType);
/**
* Determine if the specified annotation type is directly present.
*
Equivalent to calling {@code get(annotationType).isDirectlyPresent()}.
* @param annotationType the annotation type to check
* @return {@code true} if the annotation is directly present
*/
boolean isDirectlyPresent(Class annotationType);
/**
* Determine if the specified annotation type is directly present.
* Equivalent to calling {@code get(annotationType).isDirectlyPresent()}.
* @param annotationType the fully qualified class name of the annotation type
* to check
* @return {@code true} if the annotation is directly present
*/
boolean isDirectlyPresent(String annotationType);
/**
* Get the {@linkplain MergedAnnotationSelectors#nearest() nearest} matching
* annotation or meta-annotation of the specified type, or
* {@link MergedAnnotation#missing()} if none is present.
* @param annotationType the annotation type to get
* @return a {@link MergedAnnotation} instance
*/
MergedAnnotation get(Class annotationType);
/**
* Get the {@linkplain MergedAnnotationSelectors#nearest() nearest} matching
* annotation or meta-annotation of the specified type, or
* {@link MergedAnnotation#missing()} if none is present.
* @param annotationType the annotation type to get
* @param predicate a predicate that must match, or {@code null} if only
* type matching is required
* @return a {@link MergedAnnotation} instance
* @see MergedAnnotationPredicates
*/
MergedAnnotation get(Class annotationType,
@Nullable Predicate> predicate);
/**
* Get a matching annotation or meta-annotation of the specified type, or
* {@link MergedAnnotation#missing()} if none is present.
* @param annotationType the annotation type to get
* @param predicate a predicate that must match, or {@code null} if only
* type matching is required
* @param selector a selector used to choose the most appropriate annotation
* within an aggregate, or {@code null} to select the
* {@linkplain MergedAnnotationSelectors#nearest() nearest}
* @return a {@link MergedAnnotation} instance
* @see MergedAnnotationPredicates
* @see MergedAnnotationSelectors
*/
MergedAnnotation get(Class annotationType,
@Nullable Predicate> predicate,
@Nullable MergedAnnotationSelector selector);
/**
* Get the {@linkplain MergedAnnotationSelectors#nearest() nearest} matching
* annotation or meta-annotation of the specified type, or
* {@link MergedAnnotation#missing()} if none is present.
* @param annotationType the fully qualified class name of the annotation type
* to get
* @return a {@link MergedAnnotation} instance
*/
MergedAnnotation get(String annotationType);
/**
* Get the {@linkplain MergedAnnotationSelectors#nearest() nearest} matching
* annotation or meta-annotation of the specified type, or
* {@link MergedAnnotation#missing()} if none is present.
* @param annotationType the fully qualified class name of the annotation type
* to get
* @param predicate a predicate that must match, or {@code null} if only
* type matching is required
* @return a {@link MergedAnnotation} instance
* @see MergedAnnotationPredicates
*/
MergedAnnotation get(String annotationType,
@Nullable Predicate> predicate);
/**
* Get a matching annotation or meta-annotation of the specified type, or
* {@link MergedAnnotation#missing()} if none is present.
* @param annotationType the fully qualified class name of the annotation type
* to get
* @param predicate a predicate that must match, or {@code null} if only
* type matching is required
* @param selector a selector used to choose the most appropriate annotation
* within an aggregate, or {@code null} to select the
* {@linkplain MergedAnnotationSelectors#nearest() nearest}
* @return a {@link MergedAnnotation} instance
* @see MergedAnnotationPredicates
* @see MergedAnnotationSelectors
*/
MergedAnnotation get(String annotationType,
@Nullable Predicate> predicate,
@Nullable MergedAnnotationSelector selector);
/**
* Stream all annotations and meta-annotations that match the specified
* type.
* The resulting stream follows the same ordering rules as {@link #stream()}.
* @param annotationType the annotation type to match
* @return a stream of matching annotations
*/
Stream> stream(Class annotationType);
/**
* Stream all annotations and meta-annotations that match the specified
* type.
*