提交 4b29ed40 编写于 作者: M mchung

6799230: Lazily load java.lang.annotation.Annotation class

Summary: Remove the static EMPTY_ANNOTATION_ARRAY field; add AnnotationParser.toArray method
Reviewed-by: darcy
上级 8d22cdf3
...@@ -3059,14 +3059,12 @@ public final ...@@ -3059,14 +3059,12 @@ public final
} }
private static Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0];
/** /**
* @since 1.5 * @since 1.5
*/ */
public Annotation[] getAnnotations() { public Annotation[] getAnnotations() {
initAnnotationsIfNecessary(); initAnnotationsIfNecessary();
return annotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY); return AnnotationParser.toArray(annotations);
} }
/** /**
...@@ -3074,7 +3072,7 @@ public final ...@@ -3074,7 +3072,7 @@ public final
*/ */
public Annotation[] getDeclaredAnnotations() { public Annotation[] getDeclaredAnnotations() {
initAnnotationsIfNecessary(); initAnnotationsIfNecessary();
return declaredAnnotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY); return AnnotationParser.toArray(declaredAnnotations);
} }
// Annotations cache // Annotations cache
......
...@@ -626,13 +626,11 @@ public final ...@@ -626,13 +626,11 @@ public final
return (T) declaredAnnotations().get(annotationClass); return (T) declaredAnnotations().get(annotationClass);
} }
private static final Annotation[] EMPTY_ANNOTATION_ARRAY=new Annotation[0];
/** /**
* @since 1.5 * @since 1.5
*/ */
public Annotation[] getDeclaredAnnotations() { public Annotation[] getDeclaredAnnotations() {
return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY); return AnnotationParser.toArray(declaredAnnotations());
} }
private transient Map<Class, Annotation> declaredAnnotations; private transient Map<Class, Annotation> declaredAnnotations;
......
...@@ -1018,13 +1018,11 @@ class Field extends AccessibleObject implements Member { ...@@ -1018,13 +1018,11 @@ class Field extends AccessibleObject implements Member {
return (T) declaredAnnotations().get(annotationClass); return (T) declaredAnnotations().get(annotationClass);
} }
private static final Annotation[] EMPTY_ANNOTATION_ARRAY=new Annotation[0];
/** /**
* @since 1.5 * @since 1.5
*/ */
public Annotation[] getDeclaredAnnotations() { public Annotation[] getDeclaredAnnotations() {
return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY); return AnnotationParser.toArray(declaredAnnotations());
} }
private transient Map<Class, Annotation> declaredAnnotations; private transient Map<Class, Annotation> declaredAnnotations;
......
...@@ -705,13 +705,11 @@ public final ...@@ -705,13 +705,11 @@ public final
return (T) declaredAnnotations().get(annotationClass); return (T) declaredAnnotations().get(annotationClass);
} }
private static final Annotation[] EMPTY_ANNOTATION_ARRAY=new Annotation[0];
/** /**
* @since 1.5 * @since 1.5
*/ */
public Annotation[] getDeclaredAnnotations() { public Annotation[] getDeclaredAnnotations() {
return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY); return AnnotationParser.toArray(declaredAnnotations());
} }
private transient Map<Class, Annotation> declaredAnnotations; private transient Map<Class, Annotation> declaredAnnotations;
......
...@@ -788,4 +788,16 @@ public class AnnotationParser { ...@@ -788,4 +788,16 @@ public class AnnotationParser {
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
skipMemberValue(buf); 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);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册