diff --git a/src/share/classes/java/lang/Class.java b/src/share/classes/java/lang/Class.java index 77df0d146fdc097fddec5916b571a3400dabcdc7..63f56764d450150ae5e72f0aba6a1ddfdf1f15be 100644 --- 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); } // Annotations cache diff --git a/src/share/classes/java/lang/reflect/Constructor.java b/src/share/classes/java/lang/reflect/Constructor.java index 49d2478bd90b16a89faf83a12eab5d17cac59ede..0c101ccacb8596bad591d09fd5fda9a4beaa540f 100644 --- 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()); } private transient Map declaredAnnotations; diff --git a/src/share/classes/java/lang/reflect/Field.java b/src/share/classes/java/lang/reflect/Field.java index 33b6fc61ad86db712d51f2df9c2ea5991a5aa1ce..5a3e9e9ab7dccd6b58483641af5b3f63375857ce 100644 --- 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 implements Member { 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 declaredAnnotations; diff --git a/src/share/classes/java/lang/reflect/Method.java b/src/share/classes/java/lang/reflect/Method.java index 97947468f086f3f57e0161c5728f5170d9570595..c638869cd38e81b8eeafde35d60d1cfa955e9349 100644 --- 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()); } private transient Map declaredAnnotations; diff --git a/src/share/classes/sun/reflect/annotation/AnnotationParser.java b/src/share/classes/sun/reflect/annotation/AnnotationParser.java index eac123dc6053360dc6e2c002c01bcec3d2298d68..4bcb0d305426cdc2b4a4f7533dd6c28f875c94a1 100644 --- 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 annotations) { + return annotations.values().toArray(EMPTY_ANNOTATION_ARRAY); + } }