< prev index next >

src/java.base/share/classes/java/lang/reflect/Field.java

Print this page
rev 11212 : 8068736: Avoid synchronization on Executable/Field.declaredAnnotations
Reviewed-by: jfranck


1122 
1123     /**
1124      * {@inheritDoc}
1125      * @throws NullPointerException {@inheritDoc}
1126      * @since 1.8
1127      */
1128     @Override
1129     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1130         Objects.requireNonNull(annotationClass);
1131 
1132         return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
1133     }
1134 
1135     /**
1136      * {@inheritDoc}
1137      */
1138     public Annotation[] getDeclaredAnnotations()  {
1139         return AnnotationParser.toArray(declaredAnnotations());
1140     }
1141 
1142     private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
1143 
1144     private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
1145         if (declaredAnnotations == null) {



1146             Field root = this.root;
1147             if (root != null) {
1148                 declaredAnnotations = root.declaredAnnotations();
1149             } else {
1150                 declaredAnnotations = AnnotationParser.parseAnnotations(
1151                         annotations,
1152                         sun.misc.SharedSecrets.getJavaLangAccess().getConstantPool(getDeclaringClass()),

1153                         getDeclaringClass());
1154             }

1155         }
1156         return declaredAnnotations;


1157     }
1158 
1159     private native byte[] getTypeAnnotationBytes0();
1160 
1161     /**
1162      * Returns an AnnotatedType object that represents the use of a type to specify
1163      * the declared type of the field represented by this Field.
1164      * @return an object representing the declared type of the field
1165      * represented by this Field
1166      *
1167      * @since 1.8
1168      */
1169     public AnnotatedType getAnnotatedType() {
1170         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
1171                                                        sun.misc.SharedSecrets.getJavaLangAccess().
1172                                                            getConstantPool(getDeclaringClass()),
1173                                                        this,
1174                                                        getDeclaringClass(),
1175                                                        getGenericType(),
1176                                                        TypeAnnotation.TypeAnnotationTarget.FIELD);


1122 
1123     /**
1124      * {@inheritDoc}
1125      * @throws NullPointerException {@inheritDoc}
1126      * @since 1.8
1127      */
1128     @Override
1129     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1130         Objects.requireNonNull(annotationClass);
1131 
1132         return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
1133     }
1134 
1135     /**
1136      * {@inheritDoc}
1137      */
1138     public Annotation[] getDeclaredAnnotations()  {
1139         return AnnotationParser.toArray(declaredAnnotations());
1140     }
1141 
1142     private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
1143 
1144     private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
1145         Map<Class<? extends Annotation>, Annotation> declAnnos;
1146         if ((declAnnos = declaredAnnotations) == null) {
1147             synchronized (this) {
1148                 if ((declAnnos = declaredAnnotations) == null) {
1149                     Field root = this.root;
1150                     if (root != null) {
1151                         declAnnos = root.declaredAnnotations();
1152                     } else {
1153                         declAnnos = AnnotationParser.parseAnnotations(
1154                                 annotations,
1155                                 sun.misc.SharedSecrets.getJavaLangAccess()
1156                                         .getConstantPool(getDeclaringClass()),
1157                                 getDeclaringClass());
1158                     }
1159                     declaredAnnotations = declAnnos;
1160                 }
1161             }
1162         }
1163         return declAnnos;
1164     }
1165 
1166     private native byte[] getTypeAnnotationBytes0();
1167 
1168     /**
1169      * Returns an AnnotatedType object that represents the use of a type to specify
1170      * the declared type of the field represented by this Field.
1171      * @return an object representing the declared type of the field
1172      * represented by this Field
1173      *
1174      * @since 1.8
1175      */
1176     public AnnotatedType getAnnotatedType() {
1177         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
1178                                                        sun.misc.SharedSecrets.getJavaLangAccess().
1179                                                            getConstantPool(getDeclaringClass()),
1180                                                        this,
1181                                                        getDeclaringClass(),
1182                                                        getGenericType(),
1183                                                        TypeAnnotation.TypeAnnotationTarget.FIELD);
< prev index next >