Proposal to make new-in-Java-SE-8 methods on AnnotatedElement default methods

Joe Darcy joe.darcy at oracle.com
Thu Oct 24 00:16:26 PDT 2013


Hello,

Previously, an issue was filed to consider making the new-in-Java-SE-8 
methods on AnnotatedElement default methods:

         JDK-8005294 Consider default methods for additions to 
AnnotatedElement
         https://bugs.openjdk.java.net/browse/JDK-8005294

I've looked into the issue and I've concluded this is a reasonable 
suggestion. The proposed specification change is small the 
implementation manageable.

The diffs of the proposed specification changes are below. 
(Implementations changes will be reviewed by core-libs.)

Note that in the reference implementation, none of the concrete 
implementations of AnnotatedElement would call the default methods. 
Instead, those methods are instead implemented to take advantage of 
lower-level implementation data structures. Changing the new methods to 
be default methods would allow any implementations of AnnotatedElement 
outside of the reference implementation to behave reasonably under Java 
SE 8.

Thanks,

-Joe

+     * @implSpec The default implementation first calls {@link
+     * #getAnnotation(Class)} on the argument type. If the annotation
+     * is directly or indirectly present, it is returned in an
+     * one-element array. Otherwise, if the argument annotation type
+     * is repeatable and an annotation of the container type is
+     * directly or indirectly present, then the returned result is
+     * equal to the result of calling the {@code value} method on the
+     * container annotation.
+     *
       * @param <T> the type of the annotation to query for and return 
if present
       * @param annotationClass the Class object corresponding to the
       *        annotation type
@@ -230,8 +241,10 @@
       * @throws NullPointerException if the given annotation class is null
       * @since 1.8
       */
-    <T extends Annotation> T[] getAnnotationsByType(Class<T> 
annotationClass);
+    default <T extends Annotation> T[] getAnnotationsByType(Class<T> 
annotationClass) {


+     * @implSpec The default implementation first calls {@link
+     * #getDeclaredAnnotation(Class)} on the argument type. If the
+     * annotation is directly present, it is returned in an
+     * one-element array. Otherwise, if the argument annotation type
+     * is repeatable and an annotation of the container type is
+     * directly present, then the returned result is equal to the
+     * result of calling the {@code value} method on the container
+     * annotation.
+     *
       * @param <T> the type of the annotation to query for and return
       * if directly or indirectly present
       * @param annotationClass the Class object corresponding to the
@@ -277,7 +315,9 @@
       * @throws NullPointerException if the given annotation class is null
       * @since 1.8
       */
-    <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> 
annotationClass);
+    default <T extends Annotation> T[] 
getDeclaredAnnotationsByType(Class<T> annotationClass) {



+     * @implSpec The default implementation first performs a null check
+     * and then loops over the results of {@link
+     * getDeclaredAnnotations} returning the first annotation whose
+     * annotation type matches the argument type.
+     *
       * @param <T> the type of the annotation to query for and return 
if directly present
       * @param annotationClass the Class object corresponding to the
       *        annotation type
@@ -247,7 +265,18 @@
       * @throws NullPointerException if the given annotation class is null
       * @since 1.8
       */
-    <T extends Annotation> T getDeclaredAnnotation(Class<T> 
annotationClass);
+    default <T extends Annotation> T getDeclaredAnnotation(Class<T> 
annotationClass) {





More information about the enhanced-metadata-spec-discuss mailing list