View Javadoc
1   package net.florianschoppmann.java.type;
2   
3   import javax.annotation.Nullable;
4   import javax.lang.model.element.AnnotationMirror;
5   import java.lang.annotation.Annotation;
6   import java.util.List;
7   
8   /**
9    * Represents a construct that can be annotated, equivalent to {@code javax.lang.model.AnnotatedConstruct} in JDK 8.
10   *
11   * <p>This interface's sole purpose is to ensure source compatibility with Java 7 and 8, despite the fact that
12   * {@code javax.lang.model.AnnotatedConstruct} was retrofitted into the Java language model hierarchy in JDK 8. See the
13   * <a href="http://docs.oracle.com/javase/8/docs/api/javax/lang/model/AnnotatedConstruct.html">JDK 8 JavaDoc for
14   * {@code javax.lang.model.AnnotatedConstruct}</a> for more information.
15   *
16   * <p>Implementations may implement both {@code javax.lang.model.AnnotatedConstruct} and this interface. At some time
17   * in the future, when Java 7 compatibility is no longer required, this interface will be deprecated and eventually
18   * removed in favor of {@code javax.lang.model.AnnotatedConstruct}.
19   */
20  public interface AnnotatedConstruct {
21      /**
22       * Returns the annotations that are <em>directly present</em> on this construct.
23       *
24       * @return the annotations <em>directly present</em> on this construct; an empty list if there are none
25       */
26      List<? extends AnnotationMirror> getAnnotationMirrors();
27  
28      /**
29       * Returns this construct's annotation of the specified type if such an annotation is <em>present</em>, else
30       * {@code null}.
31       *
32       * @param <A> the annotation type
33       * @param annotationType the {@code Class} object corresponding to the annotation type
34       * @return this construct's annotation for the specified annotation type if present, else {@code null}
35       */
36      @Nullable
37      <A extends Annotation> A getAnnotation(Class<A> annotationType);
38  
39      /**
40       * Returns annotations that are <em>associated</em> with this construct.
41       *
42       * @param <A> the annotation type
43       * @param annotationType the {@code Class} object corresponding to the annotation type
44       * @return this construct's annotations for the specified annotation type if present on this construct, else an
45       *     empty array
46       */
47      <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);
48  }