src/share/classes/java/lang/reflect/AnnotatedElement.java

Print this page




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.annotation.AnnotationFormatError;









  30 
  31 /**
  32  * Represents an annotated element of the program currently running in this
  33  * VM.  This interface allows annotations to be read reflectively.  All
  34  * annotations returned by methods in this interface are immutable and
  35  * serializable. The arrays returned by methods of this interface may be modified
  36  * by callers without affecting the arrays returned to other callers.
  37  *
  38  * <p>The {@link #getAnnotationsByType(Class)} and {@link
  39  * #getDeclaredAnnotationsByType(Class)} methods support multiple
  40  * annotations of the same type on an element. If the argument to
  41  * either method is a repeatable annotation type (JLS 9.6), then the
  42  * method will "look through" a container annotation (JLS 9.7), if
  43  * present, and return any annotations inside the container. Container
  44  * annotations may be generated at compile-time to wrap multiple
  45  * annotations of the argument type.
  46  *
  47  * <p>The terms <em>directly present</em>, <em>indirectly present</em>,
  48  * <em>present</em>, and <em>associated</em> are used throughout this
  49  * interface to describe precisely which annotations are returned by


 205      * @return annotations present on this element
 206      * @since 1.5
 207      */
 208     Annotation[] getAnnotations();
 209 
 210     /**
 211      * Returns annotations that are <em>associated</em> with this element.
 212      *
 213      * If there are no annotations <em>associated</em> with this element, the return
 214      * value is an array of length 0.
 215      *
 216      * The difference between this method and {@link #getAnnotation(Class)}
 217      * is that this method detects if its argument is a <em>repeatable
 218      * annotation type</em> (JLS 9.6), and if so, attempts to find one or
 219      * more annotations of that type by "looking through" a container
 220      * annotation.
 221      *
 222      * The caller of this method is free to modify the returned array; it will
 223      * have no effect on the arrays returned to other callers.
 224      *












 225      * @param <T> the type of the annotation to query for and return if present
 226      * @param annotationClass the Class object corresponding to the
 227      *        annotation type
 228      * @return all this element's annotations for the specified annotation type if
 229      *     associated with this element, else an array of length zero
 230      * @throws NullPointerException if the given annotation class is null
 231      * @since 1.8
 232      */
 233     <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass);






















 234 
 235     /**
 236      * Returns this element's annotation for the specified type if
 237      * such an annotation is <em>directly present</em>, else null.
 238      *
 239      * This method ignores inherited annotations. (Returns null if no
 240      * annotations are directly present on this element.)
 241      *





 242      * @param <T> the type of the annotation to query for and return if directly present
 243      * @param annotationClass the Class object corresponding to the
 244      *        annotation type
 245      * @return this element's annotation for the specified annotation type if
 246      *     directly present on this element, else null
 247      * @throws NullPointerException if the given annotation class is null
 248      * @since 1.8
 249      */
 250     <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass);











 251 
 252     /**
 253      * Returns this element's annotation(s) for the specified type if
 254      * such annotations are either <em>directly present</em> or
 255      * <em>indirectly present</em>. This method ignores inherited
 256      * annotations.
 257      *
 258      * If there are no specified annotations directly or indirectly
 259      * present on this element, the return value is an array of length
 260      * 0.
 261      *
 262      * The difference between this method and {@link
 263      * #getDeclaredAnnotation(Class)} is that this method detects if its
 264      * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so,
 265      * attempts to find one or more annotations of that type by "looking
 266      * through" a container annotation if one is present.
 267      *
 268      * The caller of this method is free to modify the returned array; it will
 269      * have no effect on the arrays returned to other callers.
 270      *
















 271      * @param <T> the type of the annotation to query for and return
 272      * if directly or indirectly present
 273      * @param annotationClass the Class object corresponding to the
 274      *        annotation type
 275      * @return all this element's annotations for the specified annotation type if
 276      *     directly or indirectly present on this element, else an array of length zero
 277      * @throws NullPointerException if the given annotation class is null
 278      * @since 1.8
 279      */
 280     <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass);










 281 
 282     /**
 283      * Returns annotations that are <em>directly present</em> on this element.
 284      * This method ignores inherited annotations.
 285      *
 286      * If there are no annotations <em>directly present</em> on this element,
 287      * the return value is an array of length 0.
 288      *
 289      * The caller of this method is free to modify the returned array; it will
 290      * have no effect on the arrays returned to other callers.
 291      *
 292      * @return annotations directly present on this element
 293      * @since 1.5
 294      */
 295     Annotation[] getDeclaredAnnotations();
 296 }


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.annotation.AnnotationFormatError;
  30 import java.lang.annotation.Repeatable;
  31 import java.util.Arrays;
  32 import java.util.LinkedHashMap;
  33 import java.util.Map;
  34 import java.util.Objects;
  35 import java.util.function.Function;
  36 import java.util.stream.Collectors;
  37 import sun.reflect.annotation.AnnotationSupport;
  38 import sun.reflect.annotation.AnnotationType;
  39 
  40 /**
  41  * Represents an annotated element of the program currently running in this
  42  * VM.  This interface allows annotations to be read reflectively.  All
  43  * annotations returned by methods in this interface are immutable and
  44  * serializable. The arrays returned by methods of this interface may be modified
  45  * by callers without affecting the arrays returned to other callers.
  46  *
  47  * <p>The {@link #getAnnotationsByType(Class)} and {@link
  48  * #getDeclaredAnnotationsByType(Class)} methods support multiple
  49  * annotations of the same type on an element. If the argument to
  50  * either method is a repeatable annotation type (JLS 9.6), then the
  51  * method will "look through" a container annotation (JLS 9.7), if
  52  * present, and return any annotations inside the container. Container
  53  * annotations may be generated at compile-time to wrap multiple
  54  * annotations of the argument type.
  55  *
  56  * <p>The terms <em>directly present</em>, <em>indirectly present</em>,
  57  * <em>present</em>, and <em>associated</em> are used throughout this
  58  * interface to describe precisely which annotations are returned by


 214      * @return annotations present on this element
 215      * @since 1.5
 216      */
 217     Annotation[] getAnnotations();
 218 
 219     /**
 220      * Returns annotations that are <em>associated</em> with this element.
 221      *
 222      * If there are no annotations <em>associated</em> with this element, the return
 223      * value is an array of length 0.
 224      *
 225      * The difference between this method and {@link #getAnnotation(Class)}
 226      * is that this method detects if its argument is a <em>repeatable
 227      * annotation type</em> (JLS 9.6), and if so, attempts to find one or
 228      * more annotations of that type by "looking through" a container
 229      * annotation.
 230      *
 231      * The caller of this method is free to modify the returned array; it will
 232      * have no effect on the arrays returned to other callers.
 233      *
 234      * @implSpec The default implementation first calls {@link
 235      * #getDeclaredAnnotationsByType(Class)} passing {@code
 236      * annotationClass} as the argument. If the returned array has
 237      * size greater than zero, the array is returned. If the returned
 238      * array has size zero and this {@code AnnotatedElement} is a
 239      * class and the argument type is an inheritable annotation type,
 240      * and the superclass of this {@code AnnoatedElement} is non-null,
 241      * then the returned result is the result of calling {@code
 242      * getAnnotationsByType} on the superclass with {@code
 243      * annotationClass} as the argument. Otherwise, a zero-length
 244      * array is returned.
 245      *
 246      * @param <T> the type of the annotation to query for and return if present
 247      * @param annotationClass the Class object corresponding to the
 248      *        annotation type
 249      * @return all this element's annotations for the specified annotation type if
 250      *     associated with this element, else an array of length zero
 251      * @throws NullPointerException if the given annotation class is null
 252      * @since 1.8
 253      */
 254     default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
 255          /*
 256           * Definition of associated: directly or indirectly present OR
 257           * neither directly nor indirectly present AND the element is
 258           * a Class, the annotation type is inheritable, and the
 259           * annotation type is associated with the superclass of the
 260           * element.
 261           */
 262          T[] result = getDeclaredAnnotationsByType(annotationClass);
 263 
 264          if (result.length == 0 && // Neither directly nor indirectly present
 265              this instanceof Class && // the element is a class
 266              AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
 267              Class<?> superClass = ((Class<?>) this).getSuperclass();
 268              if (superClass != null) {
 269                  // Determine if the annotation is associated with the
 270                  // superclass
 271                  result = superClass.getAnnotationsByType(annotationClass);
 272              }
 273          }
 274 
 275          return result;
 276      }
 277     
 278     /**
 279      * Returns this element's annotation for the specified type if
 280      * such an annotation is <em>directly present</em>, else null.
 281      *
 282      * This method ignores inherited annotations. (Returns null if no
 283      * annotations are directly present on this element.)
 284      *
 285      * @implSpec The default implementation first performs a null check
 286      * and then loops over the results of {@link
 287      * #getDeclaredAnnotations} returning the first annotation whose
 288      * annotation type matches the argument type.
 289      *
 290      * @param <T> the type of the annotation to query for and return if directly present
 291      * @param annotationClass the Class object corresponding to the
 292      *        annotation type
 293      * @return this element's annotation for the specified annotation type if
 294      *     directly present on this element, else null
 295      * @throws NullPointerException if the given annotation class is null
 296      * @since 1.8
 297      */
 298     default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
 299          Objects.requireNonNull(annotationClass);
 300          // Loop over all directly-present annotations looking for a matching one
 301          for (Annotation annotation : getDeclaredAnnotations()) {
 302              if (annotationClass.equals(annotation.annotationType())) {
 303                  // More robust to do a dynamic cast at runtime instead
 304                  // of compile-time only.
 305                  return annotationClass.cast(annotation);
 306              }
 307          }
 308          return null; 
 309      }
 310 
 311     /**
 312      * Returns this element's annotation(s) for the specified type if
 313      * such annotations are either <em>directly present</em> or
 314      * <em>indirectly present</em>. This method ignores inherited
 315      * annotations.
 316      *
 317      * If there are no specified annotations directly or indirectly
 318      * present on this element, the return value is an array of length
 319      * 0.
 320      *
 321      * The difference between this method and {@link
 322      * #getDeclaredAnnotation(Class)} is that this method detects if its
 323      * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so,
 324      * attempts to find one or more annotations of that type by "looking
 325      * through" a container annotation if one is present.
 326      *
 327      * The caller of this method is free to modify the returned array; it will
 328      * have no effect on the arrays returned to other callers.
 329      *
 330      * @implSpec The default implementation may call {@link
 331      * #getDeclaredAnnotation(Class)} one or more times to find a
 332      * directly present annotation and, if the annotation type is
 333      * repeatable, to find a container annotation. If annotations of
 334      * the annotation type {@code annotationClass} are found to be both
 335      * directly and indirectly present, then {@link
 336      * #getDeclaredAnnotations()} will get called to determine the
 337      * order of the elements in the returned array.
 338      *
 339      * <p>Alternatively, the default implementation may call {@link
 340      * #getDeclaredAnnotations()} a single time and the returned array
 341      * examined for both directly and indirectly present
 342      * annotations. The results of calling {@link
 343      * #getDeclaredAnnotations()} are assumed to be consistent with the
 344      * results of calling {@link #getDeclaredAnnotation(Class)}.
 345      *
 346      * @param <T> the type of the annotation to query for and return
 347      * if directly or indirectly present
 348      * @param annotationClass the Class object corresponding to the
 349      *        annotation type
 350      * @return all this element's annotations for the specified annotation type if
 351      *     directly or indirectly present on this element, else an array of length zero
 352      * @throws NullPointerException if the given annotation class is null
 353      * @since 1.8
 354      */
 355     default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
 356         Objects.requireNonNull(annotationClass);
 357         return AnnotationSupport.
 358             getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()).
 359                                             collect(Collectors.toMap(Annotation::annotationType,
 360                                                                      Function.identity(),
 361                                                                      ((first,second) -> first),
 362                                                                      LinkedHashMap::new)),
 363                                             annotationClass);
 364     }
 365 
 366 
 367     /**
 368      * Returns annotations that are <em>directly present</em> on this element.
 369      * This method ignores inherited annotations.
 370      *
 371      * If there are no annotations <em>directly present</em> on this element,
 372      * the return value is an array of length 0.
 373      *
 374      * The caller of this method is free to modify the returned array; it will
 375      * have no effect on the arrays returned to other callers.
 376      *
 377      * @return annotations directly present on this element
 378      * @since 1.5
 379      */
 380     Annotation[] getDeclaredAnnotations();
 381 }