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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 

  28 import sun.reflect.CallerSensitive;
  29 import sun.reflect.FieldAccessor;
  30 import sun.reflect.Reflection;
  31 import sun.reflect.generics.repository.FieldRepository;
  32 import sun.reflect.generics.factory.CoreReflectionFactory;
  33 import sun.reflect.generics.factory.GenericsFactory;
  34 import sun.reflect.generics.scope.ClassScope;
  35 import java.lang.annotation.Annotation;
  36 import java.util.Map;
  37 import java.util.Objects;
  38 import sun.reflect.annotation.AnnotationParser;
  39 import sun.reflect.annotation.AnnotationSupport;
  40 import sun.reflect.annotation.TypeAnnotation;
  41 import sun.reflect.annotation.TypeAnnotationParser;
  42 
  43 /**
  44  * A {@code Field} provides information about, and dynamic access to, a
  45  * single field of a class or an interface.  The reflected field may
  46  * be a class (static) field or an instance field.
  47  *


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);
1184 }
1185 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import jdk.internal.misc.SharedSecrets;
  29 import sun.reflect.CallerSensitive;
  30 import sun.reflect.FieldAccessor;
  31 import sun.reflect.Reflection;
  32 import sun.reflect.generics.repository.FieldRepository;
  33 import sun.reflect.generics.factory.CoreReflectionFactory;
  34 import sun.reflect.generics.factory.GenericsFactory;
  35 import sun.reflect.generics.scope.ClassScope;
  36 import java.lang.annotation.Annotation;
  37 import java.util.Map;
  38 import java.util.Objects;
  39 import sun.reflect.annotation.AnnotationParser;
  40 import sun.reflect.annotation.AnnotationSupport;
  41 import sun.reflect.annotation.TypeAnnotation;
  42 import sun.reflect.annotation.TypeAnnotationParser;
  43 
  44 /**
  45  * A {@code Field} provides information about, and dynamic access to, a
  46  * single field of a class or an interface.  The reflected field may
  47  * be a class (static) field or an instance field.
  48  *


1136     /**
1137      * {@inheritDoc}
1138      */
1139     public Annotation[] getDeclaredAnnotations()  {
1140         return AnnotationParser.toArray(declaredAnnotations());
1141     }
1142 
1143     private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
1144 
1145     private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
1146         Map<Class<? extends Annotation>, Annotation> declAnnos;
1147         if ((declAnnos = declaredAnnotations) == null) {
1148             synchronized (this) {
1149                 if ((declAnnos = declaredAnnotations) == null) {
1150                     Field root = this.root;
1151                     if (root != null) {
1152                         declAnnos = root.declaredAnnotations();
1153                     } else {
1154                         declAnnos = AnnotationParser.parseAnnotations(
1155                                 annotations,
1156                                 SharedSecrets.getJavaLangAccess()
1157                                         .getConstantPool(getDeclaringClass()),
1158                                 getDeclaringClass());
1159                     }
1160                     declaredAnnotations = declAnnos;
1161                 }
1162             }
1163         }
1164         return declAnnos;
1165     }
1166 
1167     private native byte[] getTypeAnnotationBytes0();
1168 
1169     /**
1170      * Returns an AnnotatedType object that represents the use of a type to specify
1171      * the declared type of the field represented by this Field.
1172      * @return an object representing the declared type of the field
1173      * represented by this Field
1174      *
1175      * @since 1.8
1176      */
1177     public AnnotatedType getAnnotatedType() {
1178         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
1179                                                        SharedSecrets.getJavaLangAccess().
1180                                                            getConstantPool(getDeclaringClass()),
1181                                                        this,
1182                                                        getDeclaringClass(),
1183                                                        getGenericType(),
1184                                                        TypeAnnotation.TypeAnnotationTarget.FIELD);
1185 }
1186 }