8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
authorjfranck
Wed, 13 Feb 2013 10:36:36 +0100
changeset 15659 e575dab44ff5
parent 15658 55b829ca2334
child 15660 1649006b5598
child 15661 282a9cfb26ca
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class) Reviewed-by: darcy, abuckley
jdk/src/share/classes/java/lang/Class.java
jdk/src/share/classes/java/lang/Package.java
jdk/src/share/classes/java/lang/reflect/AccessibleObject.java
jdk/src/share/classes/java/lang/reflect/AnnotatedElement.java
jdk/src/share/classes/java/lang/reflect/Executable.java
jdk/src/share/classes/java/lang/reflect/Field.java
jdk/src/share/classes/java/lang/reflect/Parameter.java
jdk/src/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
jdk/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
jdk/test/java/lang/annotation/TypeParamAnnotation.java
jdk/test/java/lang/annotation/repeatingAnnotations/RepeatedUnitTest.java
--- a/jdk/src/share/classes/java/lang/Class.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Class.java	Wed Feb 13 10:36:36 2013 +0100
@@ -3087,7 +3087,8 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <A extends Annotation> A[] getAnnotations(Class<A> annotationClass) {
+    @Override
+    public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
         Objects.requireNonNull(annotationClass);
 
         initAnnotationsIfNecessary();
@@ -3106,6 +3107,7 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
+    @Override
     @SuppressWarnings("unchecked")
     public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
         Objects.requireNonNull(annotationClass);
@@ -3118,7 +3120,8 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <A extends Annotation> A[] getDeclaredAnnotations(Class<A> annotationClass) {
+    @Override
+    public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) {
         Objects.requireNonNull(annotationClass);
 
         initAnnotationsIfNecessary();
--- a/jdk/src/share/classes/java/lang/Package.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/Package.java	Wed Feb 13 10:36:36 2013 +0100
@@ -389,8 +389,9 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public  <A extends Annotation> A[] getAnnotations(Class<A> annotationClass) {
-        return getPackageInfo().getAnnotations(annotationClass);
+    @Override
+    public  <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
+        return getPackageInfo().getAnnotationsByType(annotationClass);
     }
 
     /**
@@ -404,6 +405,7 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
+    @Override
     public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
         return getPackageInfo().getDeclaredAnnotation(annotationClass);
     }
@@ -412,8 +414,9 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <A extends Annotation> A[] getDeclaredAnnotations(Class<A> annotationClass) {
-        return getPackageInfo().getDeclaredAnnotations(annotationClass);
+    @Override
+    public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) {
+        return getPackageInfo().getDeclaredAnnotationsByType(annotationClass);
     }
 
     /**
--- a/jdk/src/share/classes/java/lang/reflect/AccessibleObject.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/reflect/AccessibleObject.java	Wed Feb 13 10:36:36 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -184,7 +184,8 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
         throw new AssertionError("All subclasses should override this method");
     }
 
@@ -199,6 +200,7 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
+    @Override
     public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
         // Only annotations on classes are inherited, for all other
         // objects getDeclaredAnnotation is the same as
@@ -210,11 +212,12 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
         // Only annotations on classes are inherited, for all other
-        // objects getDeclaredAnnotations is the same as
-        // getAnnotations.
-        return getAnnotations(annotationClass);
+        // objects getDeclaredAnnotationsByType is the same as
+        // getAnnotationsByType.
+        return getAnnotationsByType(annotationClass);
     }
 
     /**
--- a/jdk/src/share/classes/java/lang/reflect/AnnotatedElement.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/reflect/AnnotatedElement.java	Wed Feb 13 10:36:36 2013 +0100
@@ -35,22 +35,43 @@
  * arrays returned by accessors for array-valued enum members; it will
  * have no affect on the arrays returned to other callers.
  *
- * <p>An annotation A is <em>directly present</em> on an element E if the
- * RuntimeVisibleAnnotations or RuntimeVisibleParameterAnnotations attribute
- * associated with E either:
+ * <p>The {@link #getAnnotationsByType(Class)} and {@link
+ * #getDeclaredAnnotationsByType(Class)} methods support multiple
+ * annotations of the same type on an element. If the argument to either method
+ * is a repeatable annotation type (JLS 9.6), then the method will "look
+ * through" a container annotation (JLS 9.7) which was generated at
+ * compile-time to wrap multiple annotations of the argument type.
+ *
+ * <p>The terms <em>directly present</em> and <em>present</em> are used
+ * throughout this interface to describe precisely which annotations are
+ * returned by methods:
+ *
  * <ul>
- * <li>contains A; or
- * <li>for invocations of get[Declared]Annotations(Class<T>),
- * contains A or exactly one annotation C whose type is the containing
- * annotation type of A's type (JLS 9.6) and whose value element contains A
+ * <li>An annotation A is <em>directly present</em> on an element E if E is
+ * associated with a RuntimeVisibleAnnotations or
+ * RuntimeVisibleParameterAnnotations attribute, and:
+ *
+ * <ul>
+ * <li>for an invocation of {@code get[Declared]Annotation(Class<T>)} or
+ * {@code get[Declared]Annotations()}, the attribute contains A.
+ *
+ * <li>for an invocation of {@code get[Declared]AnnotationsByType(Class<T>)}, the
+ * attribute either contains A or, if the type of A is repeatable, contains
+ * exactly one annotation whose value element contains A and whose type is the
+ * containing annotation type of A's type (JLS 9.6).
  * </ul>
  *
- * <p>An annotation A is <em>present</em> on an element E if either:
+ * <p>
+ * <li>An annotation A is <em>present</em> on an element E if either:
+ *
  * <ul>
  * <li>A is <em>directly present</em> on E; or
- * <li>There are no annotations of A's type which are <em>directly present</em>
- * on E, and E is a class, and A's type is inheritable (JLS 9.6.3.3), and A is
- * present on the superclass of E
+ *
+ * <li>A is not <em>directly present</em> on E, and E is a class, and A's type
+ * is inheritable (JLS 9.6.3.3), and A is <em>present</em> on the superclass of
+ * E.
+ * </ul>
+ *
  * </ul>
  *
  * <p>If an annotation returned by a method in this interface contains
@@ -119,12 +140,19 @@
     <T extends Annotation> T getAnnotation(Class<T> annotationClass);
 
     /**
-     * Returns an array of all this element's annotations for the
-     * specified type if one or more of such annotation is present,
-     * else an array of length zero.
+     * Returns annotations that are <em>present</em> on this element.
+     *
+     * If there are no annotations <em>present</em> on this element, the return
+     * value is an array of length 0.
      *
-     * The caller of this method is free to modify the returned array;
-     * it will have no effect on the arrays returned to other callers.
+     * The difference between this method and {@link #getAnnotation(Class)}
+     * is that this method detects if its argument is a <em>repeatable
+     * annotation type</em> (JLS 9.6), and if so, attempts to find one or
+     * more annotations of that type by "looking through" a container
+     * annotation.
+     *
+     * The caller of this method is free to modify the returned array; it will
+     * have no effect on the arrays returned to other callers.
      *
      * @param annotationClass the Class object corresponding to the
      *        annotation type
@@ -133,7 +161,7 @@
      * @throws NullPointerException if the given annotation class is null
      * @since 1.8
      */
-    <T extends Annotation> T[] getAnnotations(Class<T> annotationClass);
+    <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass);
 
     /**
      * Returns annotations that are <em>present</em> on this element.
@@ -165,16 +193,21 @@
      */
     <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass);
 
-   /**
-     * Returns an array of all this element's annotations for the
-     * specified type if one or more of such annotation is directly
-     * present, else an array of length zero.
+    /**
+     * Returns annotations that are <em>directly present</em> on this element.
+     * This method ignores inherited annotations.
+     *
+     * If there are no annotations <em>directly present</em> on this element,
+     * the return value is an array of length 0.
      *
-     * This method ignores inherited annotations. (Returns
-     * an array of length zero if no annotations are directly present
-     * on this element.)  The caller of this method is free to modify
-     * the returned array; it will have no effect on the arrays
-     * returned to other callers.
+     * The difference between this method and {@link
+     * #getDeclaredAnnotation(Class)} is that this method detects if its
+     * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so,
+     * attempts to find one or more annotations of that type by "looking
+     * through" a container annotation.
+     *
+     * The caller of this method is free to modify the returned array; it will
+     * have no effect on the arrays returned to other callers.
      *
      * @param annotationClass the Class object corresponding to the
      *        annotation type
@@ -183,7 +216,7 @@
      * @throws NullPointerException if the given annotation class is null
      * @since 1.8
      */
-    <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass);
+    <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass);
 
     /**
      * Returns annotations that are <em>directly present</em> on this element.
--- a/jdk/src/share/classes/java/lang/reflect/Executable.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/reflect/Executable.java	Wed Feb 13 10:36:36 2013 +0100
@@ -449,7 +449,8 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
         Objects.requireNonNull(annotationClass);
 
         return AnnotationSupport.getMultipleAnnotations(declaredAnnotations(), annotationClass);
--- a/jdk/src/share/classes/java/lang/reflect/Field.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/reflect/Field.java	Wed Feb 13 10:36:36 2013 +0100
@@ -1029,7 +1029,8 @@
      * @throws NullPointerException {@inheritDoc}
      * @since 1.8
      */
-    public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
         Objects.requireNonNull(annotationClass);
 
         return AnnotationSupport.getMultipleAnnotations(declaredAnnotations(), annotationClass);
--- a/jdk/src/share/classes/java/lang/reflect/Parameter.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/java/lang/reflect/Parameter.java	Wed Feb 13 10:36:36 2013 +0100
@@ -258,7 +258,8 @@
      * {@inheritDoc}
      * @throws NullPointerException {@inheritDoc}
      */
-    public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
         Objects.requireNonNull(annotationClass);
 
         return AnnotationSupport.getMultipleAnnotations(declaredAnnotations(), annotationClass);
@@ -284,11 +285,12 @@
     /**
      * @throws NullPointerException {@inheritDoc}
      */
-    public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
         // Only annotations on classes are inherited, for all other
         // objects getDeclaredAnnotations is the same as
         // getAnnotations.
-        return getAnnotations(annotationClass);
+        return getAnnotationsByType(annotationClass);
     }
 
     /**
--- a/jdk/src/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java	Wed Feb 13 10:36:36 2013 +0100
@@ -148,8 +148,8 @@
         }
 
         @Override
-        public final <T extends Annotation> T[] getAnnotations(Class<T> annotation) {
-            return getDeclaredAnnotations(annotation);
+        public final <T extends Annotation> T[] getAnnotationsByType(Class<T> annotation) {
+            return getDeclaredAnnotationsByType(annotation);
         }
 
         @Override
@@ -164,7 +164,7 @@
         }
 
         @Override
-        public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotation) {
+        public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotation) {
             return AnnotationSupport.getMultipleAnnotations(annotations, annotation);
         }
 
--- a/jdk/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java	Wed Feb 13 10:36:36 2013 +0100
@@ -200,14 +200,16 @@
         return getAnnotation(annotationClass);
     }
 
-    public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
         Objects.requireNonNull(annotationClass);
         return AnnotationSupport.getMultipleAnnotations(mapAnnotations(getAnnotations()), annotationClass);
     }
 
-    public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) {
+    @Override
+    public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
         Objects.requireNonNull(annotationClass);
-        return getAnnotations(annotationClass);
+        return getAnnotationsByType(annotationClass);
     }
 
     public Annotation[] getAnnotations() {
--- a/jdk/test/java/lang/annotation/TypeParamAnnotation.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/test/java/lang/annotation/TypeParamAnnotation.java	Wed Feb 13 10:36:36 2013 +0100
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8004698
+ * @bug 8004698 8007278
  * @summary Unit test for annotations on TypeVariables
  */
 
@@ -93,7 +93,7 @@
     private static void testGetAnnos() throws Exception {
         TypeVariable<?>[] ts = TypeParam.class.getDeclaredMethod("foo").getTypeParameters();
         ParamAnno2[] as;
-        as = ts[0].getAnnotations(ParamAnno2.class);
+        as = ts[0].getAnnotationsByType(ParamAnno2.class);
         check(as.length == 1);
         check(as[0].value() == 3);
     }
--- a/jdk/test/java/lang/annotation/repeatingAnnotations/RepeatedUnitTest.java	Tue Feb 12 09:25:43 2013 -0800
+++ b/jdk/test/java/lang/annotation/repeatingAnnotations/RepeatedUnitTest.java	Wed Feb 13 10:36:36 2013 +0100
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug     7154390 8005712
+ * @bug     7154390 8005712 8007278
  * @summary Unit test for repeated annotation reflection
  *
  * @compile RepeatedUnitTest.java subpackage/package-info.java subpackage/Container.java subpackage/Containee.java subpackage/NonRepeated.java subpackage/InheritedContainee.java subpackage/InheritedContainer.java subpackage/InheritedNonRepeated.java
@@ -76,7 +76,7 @@
 
         check(1 == countAnnotation(e, NonRepeated.class));
 
-        nr = e.getAnnotations(NonRepeated.class)[0];
+        nr = e.getAnnotationsByType(NonRepeated.class)[0];
         check(nr.value() == 10);
 
         check(1 == containsAnnotationOfType(e.getAnnotations(), NonRepeated.class));
@@ -87,9 +87,9 @@
         check(c == null);
         check(2 == countAnnotation(e, Containee.class));
 
-        c = e.getAnnotations(Containee.class)[0];
+        c = e.getAnnotationsByType(Containee.class)[0];
         check(c.value() == 1);
-        c = e.getAnnotations(Containee.class)[1];
+        c = e.getAnnotationsByType(Containee.class)[1];
         check(c.value() == 2);
 
         check(0 == containsAnnotationOfType(e.getAnnotations(), Containee.class));
@@ -98,7 +98,7 @@
     static void packageContainer(AnnotatedElement e) {
         Container cr = e.getAnnotation(Container.class);
         check(null != cr);
-        check(1 == containsAnnotationOfType(e.getAnnotations(Container.class), Container.class));
+        check(1 == containsAnnotationOfType(e.getAnnotationsByType(Container.class), Container.class));
         check(1 == countAnnotation(e, Container.class));
     }
 
@@ -123,10 +123,10 @@
         check(1 == countAnnotation(e, NonRepeated.class));
         check(1 == countAnnotation(e, InheritedNonRepeated.class));
 
-        check(e.getAnnotations(Containee.class)[2].value() == 300);
-        check(e.getAnnotations(InheritedContainee.class)[2].value() == 300);
-        check(e.getAnnotations(InheritedNonRepeated.class)[0].value() == 200);
-        check(e.getAnnotations(NonRepeated.class)[0].value() == 100);
+        check(e.getAnnotationsByType(Containee.class)[2].value() == 300);
+        check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 300);
+        check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 200);
+        check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 100);
     }
 
     static void inheritedMe3() {
@@ -138,8 +138,8 @@
         check(0 == countAnnotation(e, Container.class));
         check(1 == countAnnotation(e, InheritedContainer.class));
 
-        check(e.getAnnotations(InheritedContainee.class)[2].value() == 350);
-        check(e.getAnnotations(InheritedNonRepeated.class)[0].value() == 15);
+        check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 350);
+        check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 15);
     }
 
     static void inheritedMe4() {
@@ -153,24 +153,24 @@
         check(1 == countAnnotation(e, NonRepeated.class));
         check(1 == countAnnotation(e, InheritedNonRepeated.class));
 
-        check(e.getAnnotations(Containee.class)[2].value() == 3000);
-        check(e.getAnnotations(InheritedContainee.class)[2].value() == 3000);
-        check(e.getAnnotations(InheritedNonRepeated.class)[0].value() == 2000);
-        check(e.getAnnotations(NonRepeated.class)[0].value() == 1000);
+        check(e.getAnnotationsByType(Containee.class)[2].value() == 3000);
+        check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 3000);
+        check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 2000);
+        check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 1000);
     }
 
     static void checkMultiplier(AnnotatedElement e, int m) {
         // Basic sanity of non-repeating getAnnotation(Class)
         check(e.getAnnotation(NonRepeated.class).value() == 5 * m);
 
-        // Check count of annotations returned from getAnnotations(Class)
+        // Check count of annotations returned from getAnnotationsByType(Class)
         check(4 == countAnnotation(e, Containee.class));
         check(1 == countAnnotation(e, Container.class));
         check(1 == countAnnotation(e, NonRepeated.class));
 
-        // Check contents of array returned from getAnnotations(Class)
-        check(e.getAnnotations(Containee.class)[2].value() == 3 * m);
-        check(e.getAnnotations(NonRepeated.class)[0].value() == 5 * m);
+        // Check contents of array returned from getAnnotationsByType(Class)
+        check(e.getAnnotationsByType(Containee.class)[2].value() == 3 * m);
+        check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 5 * m);
 
         // Check getAnnotation(Class)
         check(e.getAnnotation(Containee.class) == null);
@@ -187,7 +187,7 @@
     }
 
     static int countAnnotation(AnnotatedElement e, Class<? extends Annotation> c) {
-        return containsAnnotationOfType(e.getAnnotations(c), c);
+        return containsAnnotationOfType(e.getAnnotationsByType(c), c);
     }
 
     static <A extends Annotation> int containsAnnotationOfType(A[] l, Class<? extends Annotation> a) {