JDK-6799230 : Lazily load java.lang.annotation.Annotation class
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-01-29
  • Updated: 2010-05-21
  • Resolved: 2009-03-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u14Fixed 7 b51Fixed
Related Reports
Relates :  
Description
java.lang.annotation.Annotation class is always loaded when loading java.lang.Class, java.lang.reflect.Constructor, java.lang.reflect.Method, and java.lang.reflect.Field due to a static field EMPTY_ANNOTATION_ARRAY.

This dependency is only needed when the getAnnotations or getDeclaredAnnotation method is called.  So java.lang.annotation.Annotation can be made lazily loaded.

Comments
SUGGESTED FIX --- a/src/share/classes/sun/reflect/annotation/AnnotationParser.java +++ b/src/share/classes/sun/reflect/annotation/AnnotationParser.java @@ -788,4 +788,16 @@ public class AnnotationParser { 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); + } } --- a/src/share/classes/java/lang/Class.java +++ b/src/share/classes/java/lang/Class.java @@ -3059,14 +3059,12 @@ public final } - 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 final */ public Annotation[] getDeclaredAnnotations() { initAnnotationsIfNecessary(); - return declaredAnnotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY); + return AnnotationParser.toArray(declaredAnnotations); } --- a/src/share/classes/java/lang/reflect/Field.java +++ b/src/share/classes/java/lang/reflect/Field.java @@ -1018,13 +1018,11 @@ class Field extends AccessibleObject imp 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()); } --- a/src/share/classes/java/lang/reflect/Method.java +++ b/src/share/classes/java/lang/reflect/Method.java @@ -705,13 +705,11 @@ public final 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()); } --- a/src/share/classes/java/lang/reflect/Constructor.java +++ b/src/share/classes/java/lang/reflect/Constructor.java @@ -626,13 +626,11 @@ public final 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()); }
04-03-2009

EVALUATION Instantiates the static field EMPTY_ANNOTATION_ARRAY only when the getAnnotations or getDeclaredAnnotation method is called in Class, Method, Field, and Constructor classes.
29-01-2009