changeset 930:07d2550f5c84

6799230: Lazily load java.lang.annotation.Annotation class Summary: Remove the static EMPTY_ANNOTATION_ARRAY field; add AnnotationParser.toArray method Reviewed-by: darcy
author mchung
date Tue, 03 Mar 2009 19:26:43 -0800
parents 7546743f4cc0
children a8d9e8cb38bb
files src/share/classes/java/lang/Class.java src/share/classes/java/lang/reflect/Constructor.java src/share/classes/java/lang/reflect/Field.java src/share/classes/java/lang/reflect/Method.java src/share/classes/sun/reflect/annotation/AnnotationParser.java
diffstat 5 files changed, 17 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/Class.java	Mon Mar 02 15:10:55 2009 -0800
+++ b/src/share/classes/java/lang/Class.java	Tue Mar 03 19:26:43 2009 -0800
@@ -3059,14 +3059,12 @@
     }
 
 
-    private static Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0];
-
     /**
      * @since 1.5
      */
     public Annotation[] getAnnotations() {
         initAnnotationsIfNecessary();
-        return annotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY);
+        return AnnotationParser.toArray(annotations);
     }
 
     /**
@@ -3074,7 +3072,7 @@
      */
     public Annotation[] getDeclaredAnnotations()  {
         initAnnotationsIfNecessary();
-        return declaredAnnotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY);
+        return AnnotationParser.toArray(declaredAnnotations);
     }
 
     // Annotations cache
--- a/src/share/classes/java/lang/reflect/Constructor.java	Mon Mar 02 15:10:55 2009 -0800
+++ b/src/share/classes/java/lang/reflect/Constructor.java	Tue Mar 03 19:26:43 2009 -0800
@@ -626,13 +626,11 @@
         return (T) declaredAnnotations().get(annotationClass);
     }
 
-    private static final Annotation[] EMPTY_ANNOTATION_ARRAY=new Annotation[0];
-
     /**
      * @since 1.5
      */
     public Annotation[] getDeclaredAnnotations()  {
-        return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY);
+        return AnnotationParser.toArray(declaredAnnotations());
     }
 
     private transient Map<Class, Annotation> declaredAnnotations;
--- a/src/share/classes/java/lang/reflect/Field.java	Mon Mar 02 15:10:55 2009 -0800
+++ b/src/share/classes/java/lang/reflect/Field.java	Tue Mar 03 19:26:43 2009 -0800
@@ -1018,13 +1018,11 @@
         return (T) declaredAnnotations().get(annotationClass);
     }
 
-    private static final Annotation[] EMPTY_ANNOTATION_ARRAY=new Annotation[0];
-
     /**
      * @since 1.5
      */
     public Annotation[] getDeclaredAnnotations()  {
-        return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY);
+        return AnnotationParser.toArray(declaredAnnotations());
     }
 
     private transient Map<Class, Annotation> declaredAnnotations;
--- a/src/share/classes/java/lang/reflect/Method.java	Mon Mar 02 15:10:55 2009 -0800
+++ b/src/share/classes/java/lang/reflect/Method.java	Tue Mar 03 19:26:43 2009 -0800
@@ -705,13 +705,11 @@
         return (T) declaredAnnotations().get(annotationClass);
     }
 
-    private static final Annotation[] EMPTY_ANNOTATION_ARRAY=new Annotation[0];
-
     /**
      * @since 1.5
      */
     public Annotation[] getDeclaredAnnotations()  {
-        return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY);
+        return AnnotationParser.toArray(declaredAnnotations());
     }
 
     private transient Map<Class, Annotation> declaredAnnotations;
--- a/src/share/classes/sun/reflect/annotation/AnnotationParser.java	Mon Mar 02 15:10:55 2009 -0800
+++ b/src/share/classes/sun/reflect/annotation/AnnotationParser.java	Tue Mar 03 19:26:43 2009 -0800
@@ -788,4 +788,16 @@
         for (int i = 0; i < length; i++)
             skipMemberValue(buf);
     }
+
+    /*
+     * This method converts the annotation map returned by the parseAnnotations()
+     * method to an array.  It is called by Field.getDeclaredAnnotations(),
+     * Method.getDeclaredAnnotations(), and Constructor.getDeclaredAnnotations().
+     * This avoids the reflection classes to load the Annotation class until
+     * it is needed.
+     */
+    private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
+    public static Annotation[] toArray(Map<Class, Annotation> annotations) {
+        return annotations.values().toArray(EMPTY_ANNOTATION_ARRAY);
+    }
 }