1 /*
   2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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 jdk.internal.reflect.CallerSensitive;
  30 import jdk.internal.reflect.FieldAccessor;
  31 import jdk.internal.reflect.Reflection;
  32 import jdk.internal.vm.annotation.ForceInline;
  33 import sun.reflect.generics.repository.FieldRepository;
  34 import sun.reflect.generics.factory.CoreReflectionFactory;
  35 import sun.reflect.generics.factory.GenericsFactory;
  36 import sun.reflect.generics.scope.ClassScope;
  37 import java.lang.annotation.Annotation;
  38 import java.util.Map;
  39 import java.util.Objects;
  40 import sun.reflect.annotation.AnnotationParser;
  41 import sun.reflect.annotation.AnnotationSupport;
  42 import sun.reflect.annotation.TypeAnnotation;
  43 import sun.reflect.annotation.TypeAnnotationParser;
  44 
  45 /**
  46  * A {@code Field} provides information about, and dynamic access to, a
  47  * single field of a class or an interface.  The reflected field may
  48  * be a class (static) field or an instance field.
  49  *
  50  * <p>A {@code Field} permits widening conversions to occur during a get or
  51  * set access operation, but throws an {@code IllegalArgumentException} if a
  52  * narrowing conversion would occur.
  53  *
  54  * @see Member
  55  * @see java.lang.Class
  56  * @see java.lang.Class#getFields()
  57  * @see java.lang.Class#getField(String)
  58  * @see java.lang.Class#getDeclaredFields()
  59  * @see java.lang.Class#getDeclaredField(String)
  60  *
  61  * @author Kenneth Russell
  62  * @author Nakul Saraiya
  63  * @since 1.1
  64  */
  65 public final
  66 class Field extends AccessibleObject implements Member {
  67 
  68     private Class<?>            clazz;
  69     private int                 slot;
  70     // This is guaranteed to be interned by the VM in the 1.4
  71     // reflection implementation
  72     private String              name;
  73     private Class<?>            type;
  74     private int                 modifiers;
  75     // Generics and annotations support
  76     private transient String    signature;
  77     // generic info repository; lazily initialized
  78     private transient FieldRepository genericInfo;
  79     private byte[]              annotations;
  80     // Cached field accessor created without override
  81     private FieldAccessor fieldAccessor;
  82     // Cached field accessor created with override
  83     private FieldAccessor overrideFieldAccessor;
  84     // For sharing of FieldAccessors. This branching structure is
  85     // currently only two levels deep (i.e., one root Field and
  86     // potentially many Field objects pointing to it.)
  87     //
  88     // If this branching structure would ever contain cycles, deadlocks can
  89     // occur in annotation code.
  90     private Field               root;
  91 
  92     // Generics infrastructure
  93 
  94     private String getGenericSignature() {return signature;}
  95 
  96     // Accessor for factory
  97     private GenericsFactory getFactory() {
  98         Class<?> c = getDeclaringClass();
  99         // create scope and factory
 100         return CoreReflectionFactory.make(c, ClassScope.make(c));
 101     }
 102 
 103     // Accessor for generic info repository
 104     private FieldRepository getGenericInfo() {
 105         // lazily initialize repository if necessary
 106         if (genericInfo == null) {
 107             // create and cache generic info repository
 108             genericInfo = FieldRepository.make(getGenericSignature(),
 109                                                getFactory());
 110         }
 111         return genericInfo; //return cached repository
 112     }
 113 
 114 
 115     /**
 116      * Package-private constructor used by ReflectAccess to enable
 117      * instantiation of these objects in Java code from the java.lang
 118      * package via sun.reflect.LangReflectAccess.
 119      */
 120     Field(Class<?> declaringClass,
 121           String name,
 122           Class<?> type,
 123           int modifiers,
 124           int slot,
 125           String signature,
 126           byte[] annotations)
 127     {
 128         this.clazz = declaringClass;
 129         this.name = name;
 130         this.type = type;
 131         this.modifiers = modifiers;
 132         this.slot = slot;
 133         this.signature = signature;
 134         this.annotations = annotations;
 135     }
 136 
 137     /**
 138      * Package-private routine (exposed to java.lang.Class via
 139      * ReflectAccess) which returns a copy of this Field. The copy's
 140      * "root" field points to this Field.
 141      */
 142     Field copy() {
 143         // This routine enables sharing of FieldAccessor objects
 144         // among Field objects which refer to the same underlying
 145         // method in the VM. (All of this contortion is only necessary
 146         // because of the "accessibility" bit in AccessibleObject,
 147         // which implicitly requires that new java.lang.reflect
 148         // objects be fabricated for each reflective call on Class
 149         // objects.)
 150         if (this.root != null)
 151             throw new IllegalArgumentException("Can not copy a non-root Field");
 152 
 153         Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations);
 154         res.root = this;
 155         // Might as well eagerly propagate this if already present
 156         res.fieldAccessor = fieldAccessor;
 157         res.overrideFieldAccessor = overrideFieldAccessor;
 158 
 159         return res;
 160     }
 161 
 162     /**
 163      * @throws InaccessibleObjectException {@inheritDoc}
 164      * @throws SecurityException {@inheritDoc}
 165      */
 166     @Override
 167     @CallerSensitive
 168     public void setAccessible(boolean flag) {
 169         AccessibleObject.checkPermission();
 170         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
 171         setAccessible0(flag);
 172     }
 173 
 174     @Override
 175     void checkCanSetAccessible(Class<?> caller) {
 176         checkCanSetAccessible(caller, clazz);
 177     }
 178 
 179     /**
 180      * Returns the {@code Class} object representing the class or interface
 181      * that declares the field represented by this {@code Field} object.
 182      */
 183     @Override
 184     public Class<?> getDeclaringClass() {
 185         return clazz;
 186     }
 187 
 188     /**
 189      * Returns the name of the field represented by this {@code Field} object.
 190      */
 191     public String getName() {
 192         return name;
 193     }
 194 
 195     /**
 196      * Returns the Java language modifiers for the field represented
 197      * by this {@code Field} object, as an integer. The {@code Modifier} class should
 198      * be used to decode the modifiers.
 199      *
 200      * @see Modifier
 201      */
 202     public int getModifiers() {
 203         return modifiers;
 204     }
 205 
 206     /**
 207      * Returns {@code true} if this field represents an element of
 208      * an enumerated type; returns {@code false} otherwise.
 209      *
 210      * @return {@code true} if and only if this field represents an element of
 211      * an enumerated type.
 212      * @since 1.5
 213      */
 214     public boolean isEnumConstant() {
 215         return (getModifiers() & Modifier.ENUM) != 0;
 216     }
 217 
 218     /**
 219      * Returns {@code true} if this field is a synthetic
 220      * field; returns {@code false} otherwise.
 221      *
 222      * @return true if and only if this field is a synthetic
 223      * field as defined by the Java Language Specification.
 224      * @since 1.5
 225      */
 226     public boolean isSynthetic() {
 227         return Modifier.isSynthetic(getModifiers());
 228     }
 229 
 230     /**
 231      * Returns a {@code Class} object that identifies the
 232      * declared type for the field represented by this
 233      * {@code Field} object.
 234      *
 235      * @return a {@code Class} object identifying the declared
 236      * type of the field represented by this object
 237      */
 238     public Class<?> getType() {
 239         return type;
 240     }
 241 
 242     /**
 243      * Returns a {@code Type} object that represents the declared type for
 244      * the field represented by this {@code Field} object.
 245      *
 246      * <p>If the {@code Type} is a parameterized type, the
 247      * {@code Type} object returned must accurately reflect the
 248      * actual type parameters used in the source code.
 249      *
 250      * <p>If the type of the underlying field is a type variable or a
 251      * parameterized type, it is created. Otherwise, it is resolved.
 252      *
 253      * @return a {@code Type} object that represents the declared type for
 254      *     the field represented by this {@code Field} object
 255      * @throws GenericSignatureFormatError if the generic field
 256      *     signature does not conform to the format specified in
 257      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 258      * @throws TypeNotPresentException if the generic type
 259      *     signature of the underlying field refers to a non-existent
 260      *     type declaration
 261      * @throws MalformedParameterizedTypeException if the generic
 262      *     signature of the underlying field refers to a parameterized type
 263      *     that cannot be instantiated for any reason
 264      * @since 1.5
 265      */
 266     public Type getGenericType() {
 267         if (getGenericSignature() != null)
 268             return getGenericInfo().getGenericType();
 269         else
 270             return getType();
 271     }
 272 
 273 
 274     /**
 275      * Compares this {@code Field} against the specified object.  Returns
 276      * true if the objects are the same.  Two {@code Field} objects are the same if
 277      * they were declared by the same class and have the same name
 278      * and type.
 279      */
 280     public boolean equals(Object obj) {
 281         if (obj != null && obj instanceof Field) {
 282             Field other = (Field)obj;
 283             return (getDeclaringClass() == other.getDeclaringClass())
 284                 && (getName() == other.getName())
 285                 && (getType() == other.getType());
 286         }
 287         return false;
 288     }
 289 
 290     /**
 291      * Returns a hashcode for this {@code Field}.  This is computed as the
 292      * exclusive-or of the hashcodes for the underlying field's
 293      * declaring class name and its name.
 294      */
 295     public int hashCode() {
 296         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
 297     }
 298 
 299     /**
 300      * Returns a string describing this {@code Field}.  The format is
 301      * the access modifiers for the field, if any, followed
 302      * by the field type, followed by a space, followed by
 303      * the fully-qualified name of the class declaring the field,
 304      * followed by a period, followed by the name of the field.
 305      * For example:
 306      * <pre>
 307      *    public static final int java.lang.Thread.MIN_PRIORITY
 308      *    private int java.io.FileDescriptor.fd
 309      * </pre>
 310      *
 311      * <p>The modifiers are placed in canonical order as specified by
 312      * "The Java Language Specification".  This is {@code public},
 313      * {@code protected} or {@code private} first, and then other
 314      * modifiers in the following order: {@code static}, {@code final},
 315      * {@code transient}, {@code volatile}.
 316      *
 317      * @return a string describing this {@code Field}
 318      * @jls 8.3.1 Field Modifiers
 319      */
 320     public String toString() {
 321         int mod = getModifiers();
 322         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 323             + getType().getTypeName() + " "
 324             + getDeclaringClass().getTypeName() + "."
 325             + getName());
 326     }
 327 
 328     @Override
 329     String toShortString() {
 330         return "field " + getDeclaringClass().getTypeName() + "." + getName();
 331     }
 332 
 333     /**
 334      * Returns a string describing this {@code Field}, including
 335      * its generic type.  The format is the access modifiers for the
 336      * field, if any, followed by the generic field type, followed by
 337      * a space, followed by the fully-qualified name of the class
 338      * declaring the field, followed by a period, followed by the name
 339      * of the field.
 340      *
 341      * <p>The modifiers are placed in canonical order as specified by
 342      * "The Java Language Specification".  This is {@code public},
 343      * {@code protected} or {@code private} first, and then other
 344      * modifiers in the following order: {@code static}, {@code final},
 345      * {@code transient}, {@code volatile}.
 346      *
 347      * @return a string describing this {@code Field}, including
 348      * its generic type
 349      *
 350      * @since 1.5
 351      * @jls 8.3.1 Field Modifiers
 352      */
 353     public String toGenericString() {
 354         int mod = getModifiers();
 355         Type fieldType = getGenericType();
 356         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 357             + fieldType.getTypeName() + " "
 358             + getDeclaringClass().getTypeName() + "."
 359             + getName());
 360     }
 361 
 362     /**
 363      * Returns the value of the field represented by this {@code Field}, on
 364      * the specified object. The value is automatically wrapped in an
 365      * object if it has a primitive type.
 366      *
 367      * <p>The underlying field's value is obtained as follows:
 368      *
 369      * <p>If the underlying field is a static field, the {@code obj} argument
 370      * is ignored; it may be null.
 371      *
 372      * <p>Otherwise, the underlying field is an instance field.  If the
 373      * specified {@code obj} argument is null, the method throws a
 374      * {@code NullPointerException}. If the specified object is not an
 375      * instance of the class or interface declaring the underlying
 376      * field, the method throws an {@code IllegalArgumentException}.
 377      *
 378      * <p>If this {@code Field} object is enforcing Java language access control, and
 379      * the underlying field is inaccessible, the method throws an
 380      * {@code IllegalAccessException}.
 381      * If the underlying field is static, the class that declared the
 382      * field is initialized if it has not already been initialized.
 383      *
 384      * <p>Otherwise, the value is retrieved from the underlying instance
 385      * or static field.  If the field has a primitive type, the value
 386      * is wrapped in an object before being returned, otherwise it is
 387      * returned as is.
 388      *
 389      * <p>If the field is hidden in the type of {@code obj},
 390      * the field's value is obtained according to the preceding rules.
 391      *
 392      * @param obj object from which the represented field's value is
 393      * to be extracted
 394      * @return the value of the represented field in object
 395      * {@code obj}; primitive values are wrapped in an appropriate
 396      * object before being returned
 397      *
 398      * @exception IllegalAccessException    if this {@code Field} object
 399      *              is enforcing Java language access control and the underlying
 400      *              field is inaccessible.
 401      * @exception IllegalArgumentException  if the specified object is not an
 402      *              instance of the class or interface declaring the underlying
 403      *              field (or a subclass or implementor thereof).
 404      * @exception NullPointerException      if the specified object is null
 405      *              and the field is an instance field.
 406      * @exception ExceptionInInitializerError if the initialization provoked
 407      *              by this method fails.
 408      */
 409     @CallerSensitive
 410     @ForceInline // to ensure Reflection.getCallerClass optimization
 411     public Object get(Object obj)
 412         throws IllegalArgumentException, IllegalAccessException
 413     {
 414         if (!override) {
 415             Class<?> caller = Reflection.getCallerClass();
 416             checkAccess(caller, obj);
 417         }
 418         return getFieldAccessor(obj).get(obj);
 419     }
 420 
 421     /**
 422      * Gets the value of a static or instance {@code boolean} field.
 423      *
 424      * @param obj the object to extract the {@code boolean} value
 425      * from
 426      * @return the value of the {@code boolean} field
 427      *
 428      * @exception IllegalAccessException    if this {@code Field} object
 429      *              is enforcing Java language access control and the underlying
 430      *              field is inaccessible.
 431      * @exception IllegalArgumentException  if the specified object is not
 432      *              an instance of the class or interface declaring the
 433      *              underlying field (or a subclass or implementor
 434      *              thereof), or if the field value cannot be
 435      *              converted to the type {@code boolean} by a
 436      *              widening conversion.
 437      * @exception NullPointerException      if the specified object is null
 438      *              and the field is an instance field.
 439      * @exception ExceptionInInitializerError if the initialization provoked
 440      *              by this method fails.
 441      * @see       Field#get
 442      */
 443     @CallerSensitive
 444     @ForceInline // to ensure Reflection.getCallerClass optimization
 445     public boolean getBoolean(Object obj)
 446         throws IllegalArgumentException, IllegalAccessException
 447     {
 448         if (!override) {
 449             Class<?> caller = Reflection.getCallerClass();
 450             checkAccess(caller, obj);
 451         }
 452         return getFieldAccessor(obj).getBoolean(obj);
 453     }
 454 
 455     /**
 456      * Gets the value of a static or instance {@code byte} field.
 457      *
 458      * @param obj the object to extract the {@code byte} value
 459      * from
 460      * @return the value of the {@code byte} field
 461      *
 462      * @exception IllegalAccessException    if this {@code Field} object
 463      *              is enforcing Java language access control and the underlying
 464      *              field is inaccessible.
 465      * @exception IllegalArgumentException  if the specified object is not
 466      *              an instance of the class or interface declaring the
 467      *              underlying field (or a subclass or implementor
 468      *              thereof), or if the field value cannot be
 469      *              converted to the type {@code byte} by a
 470      *              widening conversion.
 471      * @exception NullPointerException      if the specified object is null
 472      *              and the field is an instance field.
 473      * @exception ExceptionInInitializerError if the initialization provoked
 474      *              by this method fails.
 475      * @see       Field#get
 476      */
 477     @CallerSensitive
 478     @ForceInline // to ensure Reflection.getCallerClass optimization
 479     public byte getByte(Object obj)
 480         throws IllegalArgumentException, IllegalAccessException
 481     {
 482         if (!override) {
 483             Class<?> caller = Reflection.getCallerClass();
 484             checkAccess(caller, obj);
 485         }
 486         return getFieldAccessor(obj).getByte(obj);
 487     }
 488 
 489     /**
 490      * Gets the value of a static or instance field of type
 491      * {@code char} or of another primitive type convertible to
 492      * type {@code char} via a widening conversion.
 493      *
 494      * @param obj the object to extract the {@code char} value
 495      * from
 496      * @return the value of the field converted to type {@code char}
 497      *
 498      * @exception IllegalAccessException    if this {@code Field} object
 499      *              is enforcing Java language access control and the underlying
 500      *              field is inaccessible.
 501      * @exception IllegalArgumentException  if the specified object is not
 502      *              an instance of the class or interface declaring the
 503      *              underlying field (or a subclass or implementor
 504      *              thereof), or if the field value cannot be
 505      *              converted to the type {@code char} by a
 506      *              widening conversion.
 507      * @exception NullPointerException      if the specified object is null
 508      *              and the field is an instance field.
 509      * @exception ExceptionInInitializerError if the initialization provoked
 510      *              by this method fails.
 511      * @see Field#get
 512      */
 513     @CallerSensitive
 514     @ForceInline // to ensure Reflection.getCallerClass optimization
 515     public char getChar(Object obj)
 516         throws IllegalArgumentException, IllegalAccessException
 517     {
 518         if (!override) {
 519             Class<?> caller = Reflection.getCallerClass();
 520             checkAccess(caller, obj);
 521         }
 522         return getFieldAccessor(obj).getChar(obj);
 523     }
 524 
 525     /**
 526      * Gets the value of a static or instance field of type
 527      * {@code short} or of another primitive type convertible to
 528      * type {@code short} via a widening conversion.
 529      *
 530      * @param obj the object to extract the {@code short} value
 531      * from
 532      * @return the value of the field converted to type {@code short}
 533      *
 534      * @exception IllegalAccessException    if this {@code Field} object
 535      *              is enforcing Java language access control and the underlying
 536      *              field is inaccessible.
 537      * @exception IllegalArgumentException  if the specified object is not
 538      *              an instance of the class or interface declaring the
 539      *              underlying field (or a subclass or implementor
 540      *              thereof), or if the field value cannot be
 541      *              converted to the type {@code short} by a
 542      *              widening conversion.
 543      * @exception NullPointerException      if the specified object is null
 544      *              and the field is an instance field.
 545      * @exception ExceptionInInitializerError if the initialization provoked
 546      *              by this method fails.
 547      * @see       Field#get
 548      */
 549     @CallerSensitive
 550     @ForceInline // to ensure Reflection.getCallerClass optimization
 551     public short getShort(Object obj)
 552         throws IllegalArgumentException, IllegalAccessException
 553     {
 554         if (!override) {
 555             Class<?> caller = Reflection.getCallerClass();
 556             checkAccess(caller, obj);
 557         }
 558         return getFieldAccessor(obj).getShort(obj);
 559     }
 560 
 561     /**
 562      * Gets the value of a static or instance field of type
 563      * {@code int} or of another primitive type convertible to
 564      * type {@code int} via a widening conversion.
 565      *
 566      * @param obj the object to extract the {@code int} value
 567      * from
 568      * @return the value of the field converted to type {@code int}
 569      *
 570      * @exception IllegalAccessException    if this {@code Field} object
 571      *              is enforcing Java language access control and the underlying
 572      *              field is inaccessible.
 573      * @exception IllegalArgumentException  if the specified object is not
 574      *              an instance of the class or interface declaring the
 575      *              underlying field (or a subclass or implementor
 576      *              thereof), or if the field value cannot be
 577      *              converted to the type {@code int} by a
 578      *              widening conversion.
 579      * @exception NullPointerException      if the specified object is null
 580      *              and the field is an instance field.
 581      * @exception ExceptionInInitializerError if the initialization provoked
 582      *              by this method fails.
 583      * @see       Field#get
 584      */
 585     @CallerSensitive
 586     @ForceInline // to ensure Reflection.getCallerClass optimization
 587     public int getInt(Object obj)
 588         throws IllegalArgumentException, IllegalAccessException
 589     {
 590         if (!override) {
 591             Class<?> caller = Reflection.getCallerClass();
 592             checkAccess(caller, obj);
 593         }
 594         return getFieldAccessor(obj).getInt(obj);
 595     }
 596 
 597     /**
 598      * Gets the value of a static or instance field of type
 599      * {@code long} or of another primitive type convertible to
 600      * type {@code long} via a widening conversion.
 601      *
 602      * @param obj the object to extract the {@code long} value
 603      * from
 604      * @return the value of the field converted to type {@code long}
 605      *
 606      * @exception IllegalAccessException    if this {@code Field} object
 607      *              is enforcing Java language access control and the underlying
 608      *              field is inaccessible.
 609      * @exception IllegalArgumentException  if the specified object is not
 610      *              an instance of the class or interface declaring the
 611      *              underlying field (or a subclass or implementor
 612      *              thereof), or if the field value cannot be
 613      *              converted to the type {@code long} by a
 614      *              widening conversion.
 615      * @exception NullPointerException      if the specified object is null
 616      *              and the field is an instance field.
 617      * @exception ExceptionInInitializerError if the initialization provoked
 618      *              by this method fails.
 619      * @see       Field#get
 620      */
 621     @CallerSensitive
 622     @ForceInline // to ensure Reflection.getCallerClass optimization
 623     public long getLong(Object obj)
 624         throws IllegalArgumentException, IllegalAccessException
 625     {
 626         if (!override) {
 627             Class<?> caller = Reflection.getCallerClass();
 628             checkAccess(caller, obj);
 629         }
 630         return getFieldAccessor(obj).getLong(obj);
 631     }
 632 
 633     /**
 634      * Gets the value of a static or instance field of type
 635      * {@code float} or of another primitive type convertible to
 636      * type {@code float} via a widening conversion.
 637      *
 638      * @param obj the object to extract the {@code float} value
 639      * from
 640      * @return the value of the field converted to type {@code float}
 641      *
 642      * @exception IllegalAccessException    if this {@code Field} object
 643      *              is enforcing Java language access control and the underlying
 644      *              field is inaccessible.
 645      * @exception IllegalArgumentException  if the specified object is not
 646      *              an instance of the class or interface declaring the
 647      *              underlying field (or a subclass or implementor
 648      *              thereof), or if the field value cannot be
 649      *              converted to the type {@code float} by a
 650      *              widening conversion.
 651      * @exception NullPointerException      if the specified object is null
 652      *              and the field is an instance field.
 653      * @exception ExceptionInInitializerError if the initialization provoked
 654      *              by this method fails.
 655      * @see Field#get
 656      */
 657     @CallerSensitive
 658     @ForceInline // to ensure Reflection.getCallerClass optimization
 659     public float getFloat(Object obj)
 660         throws IllegalArgumentException, IllegalAccessException
 661     {
 662         if (!override) {
 663             Class<?> caller = Reflection.getCallerClass();
 664             checkAccess(caller, obj);
 665         }
 666         return getFieldAccessor(obj).getFloat(obj);
 667     }
 668 
 669     /**
 670      * Gets the value of a static or instance field of type
 671      * {@code double} or of another primitive type convertible to
 672      * type {@code double} via a widening conversion.
 673      *
 674      * @param obj the object to extract the {@code double} value
 675      * from
 676      * @return the value of the field converted to type {@code double}
 677      *
 678      * @exception IllegalAccessException    if this {@code Field} object
 679      *              is enforcing Java language access control and the underlying
 680      *              field is inaccessible.
 681      * @exception IllegalArgumentException  if the specified object is not
 682      *              an instance of the class or interface declaring the
 683      *              underlying field (or a subclass or implementor
 684      *              thereof), or if the field value cannot be
 685      *              converted to the type {@code double} by a
 686      *              widening conversion.
 687      * @exception NullPointerException      if the specified object is null
 688      *              and the field is an instance field.
 689      * @exception ExceptionInInitializerError if the initialization provoked
 690      *              by this method fails.
 691      * @see       Field#get
 692      */
 693     @CallerSensitive
 694     @ForceInline // to ensure Reflection.getCallerClass optimization
 695     public double getDouble(Object obj)
 696         throws IllegalArgumentException, IllegalAccessException
 697     {
 698         if (!override) {
 699             Class<?> caller = Reflection.getCallerClass();
 700             checkAccess(caller, obj);
 701         }
 702         return getFieldAccessor(obj).getDouble(obj);
 703     }
 704 
 705     /**
 706      * Sets the field represented by this {@code Field} object on the
 707      * specified object argument to the specified new value. The new
 708      * value is automatically unwrapped if the underlying field has a
 709      * primitive type.
 710      *
 711      * <p>The operation proceeds as follows:
 712      *
 713      * <p>If the underlying field is static, the {@code obj} argument is
 714      * ignored; it may be null.
 715      *
 716      * <p>Otherwise the underlying field is an instance field.  If the
 717      * specified object argument is null, the method throws a
 718      * {@code NullPointerException}.  If the specified object argument is not
 719      * an instance of the class or interface declaring the underlying
 720      * field, the method throws an {@code IllegalArgumentException}.
 721      *
 722      * <p>If this {@code Field} object is enforcing Java language access control, and
 723      * the underlying field is inaccessible, the method throws an
 724      * {@code IllegalAccessException}.
 725      *
 726      * <p>If the underlying field is final, the method throws an
 727      * {@code IllegalAccessException} unless {@code setAccessible(true)}
 728      * has succeeded for this {@code Field} object
 729      * and the field is non-static. Setting a final field in this way
 730      * is meaningful only during deserialization or reconstruction of
 731      * instances of classes with blank final fields, before they are
 732      * made available for access by other parts of a program. Use in
 733      * any other context may have unpredictable effects, including cases
 734      * in which other parts of a program continue to use the original
 735      * value of this field.
 736      *
 737      * <p>If the underlying field is of a primitive type, an unwrapping
 738      * conversion is attempted to convert the new value to a value of
 739      * a primitive type.  If this attempt fails, the method throws an
 740      * {@code IllegalArgumentException}.
 741      *
 742      * <p>If, after possible unwrapping, the new value cannot be
 743      * converted to the type of the underlying field by an identity or
 744      * widening conversion, the method throws an
 745      * {@code IllegalArgumentException}.
 746      *
 747      * <p>If the underlying field is static, the class that declared the
 748      * field is initialized if it has not already been initialized.
 749      *
 750      * <p>The field is set to the possibly unwrapped and widened new value.
 751      *
 752      * <p>If the field is hidden in the type of {@code obj},
 753      * the field's value is set according to the preceding rules.
 754      *
 755      * @param obj the object whose field should be modified
 756      * @param value the new value for the field of {@code obj}
 757      * being modified
 758      *
 759      * @exception IllegalAccessException    if this {@code Field} object
 760      *              is enforcing Java language access control and the underlying
 761      *              field is either inaccessible or final.
 762      * @exception IllegalArgumentException  if the specified object is not an
 763      *              instance of the class or interface declaring the underlying
 764      *              field (or a subclass or implementor thereof),
 765      *              or if an unwrapping conversion fails.
 766      * @exception NullPointerException      if the specified object is null
 767      *              and the field is an instance field.
 768      * @exception ExceptionInInitializerError if the initialization provoked
 769      *              by this method fails.
 770      */
 771     @CallerSensitive
 772     @ForceInline // to ensure Reflection.getCallerClass optimization
 773     public void set(Object obj, Object value)
 774         throws IllegalArgumentException, IllegalAccessException
 775     {
 776         ensureNotValueClass();
 777 
 778         if (!override) {
 779             Class<?> caller = Reflection.getCallerClass();
 780             checkAccess(caller, obj);
 781         }
 782         getFieldAccessor(obj).set(obj, value);
 783     }
 784 
 785     /**
 786      * Sets the value of a field as a {@code boolean} on the specified object.
 787      * This method is equivalent to
 788      * {@code set(obj, zObj)},
 789      * where {@code zObj} is a {@code Boolean} object and
 790      * {@code zObj.booleanValue() == z}.
 791      *
 792      * @param obj the object whose field should be modified
 793      * @param z   the new value for the field of {@code obj}
 794      * being modified
 795      *
 796      * @exception IllegalAccessException    if this {@code Field} object
 797      *              is enforcing Java language access control and the underlying
 798      *              field is either inaccessible or final.
 799      * @exception IllegalArgumentException  if the specified object is not an
 800      *              instance of the class or interface declaring the underlying
 801      *              field (or a subclass or implementor thereof),
 802      *              or if an unwrapping conversion fails.
 803      * @exception NullPointerException      if the specified object is null
 804      *              and the field is an instance field.
 805      * @exception ExceptionInInitializerError if the initialization provoked
 806      *              by this method fails.
 807      * @see       Field#set
 808      */
 809     @CallerSensitive
 810     @ForceInline // to ensure Reflection.getCallerClass optimization
 811     public void setBoolean(Object obj, boolean z)
 812         throws IllegalArgumentException, IllegalAccessException
 813     {
 814         ensureNotValueClass();
 815 
 816         if (!override) {
 817             Class<?> caller = Reflection.getCallerClass();
 818             checkAccess(caller, obj);
 819         }
 820         getFieldAccessor(obj).setBoolean(obj, z);
 821     }
 822 
 823     /**
 824      * Sets the value of a field as a {@code byte} on the specified object.
 825      * This method is equivalent to
 826      * {@code set(obj, bObj)},
 827      * where {@code bObj} is a {@code Byte} object and
 828      * {@code bObj.byteValue() == b}.
 829      *
 830      * @param obj the object whose field should be modified
 831      * @param b   the new value for the field of {@code obj}
 832      * being modified
 833      *
 834      * @exception IllegalAccessException    if this {@code Field} object
 835      *              is enforcing Java language access control and the underlying
 836      *              field is either inaccessible or final.
 837      * @exception IllegalArgumentException  if the specified object is not an
 838      *              instance of the class or interface declaring the underlying
 839      *              field (or a subclass or implementor thereof),
 840      *              or if an unwrapping conversion fails.
 841      * @exception NullPointerException      if the specified object is null
 842      *              and the field is an instance field.
 843      * @exception ExceptionInInitializerError if the initialization provoked
 844      *              by this method fails.
 845      * @see       Field#set
 846      */
 847     @CallerSensitive
 848     @ForceInline // to ensure Reflection.getCallerClass optimization
 849     public void setByte(Object obj, byte b)
 850         throws IllegalArgumentException, IllegalAccessException
 851     {
 852         ensureNotValueClass();
 853 
 854         if (!override) {
 855             Class<?> caller = Reflection.getCallerClass();
 856             checkAccess(caller, obj);
 857         }
 858         getFieldAccessor(obj).setByte(obj, b);
 859     }
 860 
 861     /**
 862      * Sets the value of a field as a {@code char} on the specified object.
 863      * This method is equivalent to
 864      * {@code set(obj, cObj)},
 865      * where {@code cObj} is a {@code Character} object and
 866      * {@code cObj.charValue() == c}.
 867      *
 868      * @param obj the object whose field should be modified
 869      * @param c   the new value for the field of {@code obj}
 870      * being modified
 871      *
 872      * @exception IllegalAccessException    if this {@code Field} object
 873      *              is enforcing Java language access control and the underlying
 874      *              field is either inaccessible or final.
 875      * @exception IllegalArgumentException  if the specified object is not an
 876      *              instance of the class or interface declaring the underlying
 877      *              field (or a subclass or implementor thereof),
 878      *              or if an unwrapping conversion fails.
 879      * @exception NullPointerException      if the specified object is null
 880      *              and the field is an instance field.
 881      * @exception ExceptionInInitializerError if the initialization provoked
 882      *              by this method fails.
 883      * @see       Field#set
 884      */
 885     @CallerSensitive
 886     @ForceInline // to ensure Reflection.getCallerClass optimization
 887     public void setChar(Object obj, char c)
 888         throws IllegalArgumentException, IllegalAccessException
 889     {
 890         ensureNotValueClass();
 891 
 892         if (!override) {
 893             Class<?> caller = Reflection.getCallerClass();
 894             checkAccess(caller, obj);
 895         }
 896         getFieldAccessor(obj).setChar(obj, c);
 897     }
 898 
 899     /**
 900      * Sets the value of a field as a {@code short} on the specified object.
 901      * This method is equivalent to
 902      * {@code set(obj, sObj)},
 903      * where {@code sObj} is a {@code Short} object and
 904      * {@code sObj.shortValue() == s}.
 905      *
 906      * @param obj the object whose field should be modified
 907      * @param s   the new value for the field of {@code obj}
 908      * being modified
 909      *
 910      * @exception IllegalAccessException    if this {@code Field} object
 911      *              is enforcing Java language access control and the underlying
 912      *              field is either inaccessible or final.
 913      * @exception IllegalArgumentException  if the specified object is not an
 914      *              instance of the class or interface declaring the underlying
 915      *              field (or a subclass or implementor thereof),
 916      *              or if an unwrapping conversion fails.
 917      * @exception NullPointerException      if the specified object is null
 918      *              and the field is an instance field.
 919      * @exception ExceptionInInitializerError if the initialization provoked
 920      *              by this method fails.
 921      * @see       Field#set
 922      */
 923     @CallerSensitive
 924     @ForceInline // to ensure Reflection.getCallerClass optimization
 925     public void setShort(Object obj, short s)
 926         throws IllegalArgumentException, IllegalAccessException
 927     {
 928         ensureNotValueClass();
 929 
 930         if (!override) {
 931             Class<?> caller = Reflection.getCallerClass();
 932             checkAccess(caller, obj);
 933         }
 934         getFieldAccessor(obj).setShort(obj, s);
 935     }
 936 
 937     /**
 938      * Sets the value of a field as an {@code int} on the specified object.
 939      * This method is equivalent to
 940      * {@code set(obj, iObj)},
 941      * where {@code iObj} is an {@code Integer} object and
 942      * {@code iObj.intValue() == i}.
 943      *
 944      * @param obj the object whose field should be modified
 945      * @param i   the new value for the field of {@code obj}
 946      * being modified
 947      *
 948      * @exception IllegalAccessException    if this {@code Field} object
 949      *              is enforcing Java language access control and the underlying
 950      *              field is either inaccessible or final.
 951      * @exception IllegalArgumentException  if the specified object is not an
 952      *              instance of the class or interface declaring the underlying
 953      *              field (or a subclass or implementor thereof),
 954      *              or if an unwrapping conversion fails.
 955      * @exception NullPointerException      if the specified object is null
 956      *              and the field is an instance field.
 957      * @exception ExceptionInInitializerError if the initialization provoked
 958      *              by this method fails.
 959      * @see       Field#set
 960      */
 961     @CallerSensitive
 962     @ForceInline // to ensure Reflection.getCallerClass optimization
 963     public void setInt(Object obj, int i)
 964         throws IllegalArgumentException, IllegalAccessException
 965     {
 966         ensureNotValueClass();
 967 
 968         if (!override) {
 969             Class<?> caller = Reflection.getCallerClass();
 970             checkAccess(caller, obj);
 971         }
 972         getFieldAccessor(obj).setInt(obj, i);
 973     }
 974 
 975     /**
 976      * Sets the value of a field as a {@code long} on the specified object.
 977      * This method is equivalent to
 978      * {@code set(obj, lObj)},
 979      * where {@code lObj} is a {@code Long} object and
 980      * {@code lObj.longValue() == l}.
 981      *
 982      * @param obj the object whose field should be modified
 983      * @param l   the new value for the field of {@code obj}
 984      * being modified
 985      *
 986      * @exception IllegalAccessException    if this {@code Field} object
 987      *              is enforcing Java language access control and the underlying
 988      *              field is either inaccessible or final.
 989      * @exception IllegalArgumentException  if the specified object is not an
 990      *              instance of the class or interface declaring the underlying
 991      *              field (or a subclass or implementor thereof),
 992      *              or if an unwrapping conversion fails.
 993      * @exception NullPointerException      if the specified object is null
 994      *              and the field is an instance field.
 995      * @exception ExceptionInInitializerError if the initialization provoked
 996      *              by this method fails.
 997      * @see       Field#set
 998      */
 999     @CallerSensitive
1000     @ForceInline // to ensure Reflection.getCallerClass optimization
1001     public void setLong(Object obj, long l)
1002         throws IllegalArgumentException, IllegalAccessException
1003     {
1004         ensureNotValueClass();
1005 
1006         if (!override) {
1007             Class<?> caller = Reflection.getCallerClass();
1008             checkAccess(caller, obj);
1009         }
1010         getFieldAccessor(obj).setLong(obj, l);
1011     }
1012 
1013     /**
1014      * Sets the value of a field as a {@code float} on the specified object.
1015      * This method is equivalent to
1016      * {@code set(obj, fObj)},
1017      * where {@code fObj} is a {@code Float} object and
1018      * {@code fObj.floatValue() == f}.
1019      *
1020      * @param obj the object whose field should be modified
1021      * @param f   the new value for the field of {@code obj}
1022      * being modified
1023      *
1024      * @exception IllegalAccessException    if this {@code Field} object
1025      *              is enforcing Java language access control and the underlying
1026      *              field is either inaccessible or final.
1027      * @exception IllegalArgumentException  if the specified object is not an
1028      *              instance of the class or interface declaring the underlying
1029      *              field (or a subclass or implementor thereof),
1030      *              or if an unwrapping conversion fails.
1031      * @exception NullPointerException      if the specified object is null
1032      *              and the field is an instance field.
1033      * @exception ExceptionInInitializerError if the initialization provoked
1034      *              by this method fails.
1035      * @see       Field#set
1036      */
1037     @CallerSensitive
1038     @ForceInline // to ensure Reflection.getCallerClass optimization
1039     public void setFloat(Object obj, float f)
1040         throws IllegalArgumentException, IllegalAccessException
1041     {
1042         ensureNotValueClass();
1043 
1044         if (!override) {
1045             Class<?> caller = Reflection.getCallerClass();
1046             checkAccess(caller, obj);
1047         }
1048         getFieldAccessor(obj).setFloat(obj, f);
1049     }
1050 
1051     /**
1052      * Sets the value of a field as a {@code double} on the specified object.
1053      * This method is equivalent to
1054      * {@code set(obj, dObj)},
1055      * where {@code dObj} is a {@code Double} object and
1056      * {@code dObj.doubleValue() == d}.
1057      *
1058      * @param obj the object whose field should be modified
1059      * @param d   the new value for the field of {@code obj}
1060      * being modified
1061      *
1062      * @exception IllegalAccessException    if this {@code Field} object
1063      *              is enforcing Java language access control and the underlying
1064      *              field is either inaccessible or final.
1065      * @exception IllegalArgumentException  if the specified object is not an
1066      *              instance of the class or interface declaring the underlying
1067      *              field (or a subclass or implementor thereof),
1068      *              or if an unwrapping conversion fails.
1069      * @exception NullPointerException      if the specified object is null
1070      *              and the field is an instance field.
1071      * @exception ExceptionInInitializerError if the initialization provoked
1072      *              by this method fails.
1073      * @see       Field#set
1074      */
1075     @CallerSensitive
1076     @ForceInline // to ensure Reflection.getCallerClass optimization
1077     public void setDouble(Object obj, double d)
1078         throws IllegalArgumentException, IllegalAccessException
1079     {
1080         ensureNotValueClass();
1081 
1082         if (!override) {
1083             Class<?> caller = Reflection.getCallerClass();
1084             checkAccess(caller, obj);
1085         }
1086         getFieldAccessor(obj).setDouble(obj, d);
1087     }
1088 
1089     // check access to field
1090     private void checkAccess(Class<?> caller, Object obj)
1091         throws IllegalAccessException
1092     {
1093         checkAccess(caller, clazz,
1094                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
1095                     modifiers);
1096     }
1097 
1098     /*
1099      * Ensure the declaring class is not a value class.
1100      */
1101     private void ensureNotValueClass() throws IllegalAccessException {
1102         if (clazz.isValue()) {
1103             throw new IllegalAccessException("cannot set this field of a value class "
1104                 + clazz.getName());
1105         }
1106     }
1107 
1108     // security check is done before calling this method
1109     private FieldAccessor getFieldAccessor(Object obj)
1110         throws IllegalAccessException
1111     {
1112         boolean ov = override;
1113         FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
1114         return (a != null) ? a : acquireFieldAccessor(ov);
1115     }
1116 
1117     // NOTE that there is no synchronization used here. It is correct
1118     // (though not efficient) to generate more than one FieldAccessor
1119     // for a given Field. However, avoiding synchronization will
1120     // probably make the implementation more scalable.
1121     private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
1122         // First check to see if one has been created yet, and take it
1123         // if so
1124         FieldAccessor tmp = null;
1125         if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);
1126         if (tmp != null) {
1127             if (overrideFinalCheck)
1128                 overrideFieldAccessor = tmp;
1129             else
1130                 fieldAccessor = tmp;
1131         } else {
1132             // Otherwise fabricate one and propagate it up to the root
1133             tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck);
1134             setFieldAccessor(tmp, overrideFinalCheck);
1135         }
1136 
1137         return tmp;
1138     }
1139 
1140     // Returns FieldAccessor for this Field object, not looking up
1141     // the chain to the root
1142     private FieldAccessor getFieldAccessor(boolean overrideFinalCheck) {
1143         return (overrideFinalCheck)? overrideFieldAccessor : fieldAccessor;
1144     }
1145 
1146     // Sets the FieldAccessor for this Field object and
1147     // (recursively) its root
1148     private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) {
1149         if (overrideFinalCheck)
1150             overrideFieldAccessor = accessor;
1151         else
1152             fieldAccessor = accessor;
1153         // Propagate up
1154         if (root != null) {
1155             root.setFieldAccessor(accessor, overrideFinalCheck);
1156         }
1157     }
1158 
1159     /**
1160      * @throws NullPointerException {@inheritDoc}
1161      * @since 1.5
1162      */
1163     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1164         Objects.requireNonNull(annotationClass);
1165         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1166     }
1167 
1168     /**
1169      * {@inheritDoc}
1170      * @throws NullPointerException {@inheritDoc}
1171      * @since 1.8
1172      */
1173     @Override
1174     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1175         Objects.requireNonNull(annotationClass);
1176 
1177         return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
1178     }
1179 
1180     /**
1181      * {@inheritDoc}
1182      */
1183     public Annotation[] getDeclaredAnnotations()  {
1184         return AnnotationParser.toArray(declaredAnnotations());
1185     }
1186 
1187     private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
1188 
1189     private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
1190         Map<Class<? extends Annotation>, Annotation> declAnnos;
1191         if ((declAnnos = declaredAnnotations) == null) {
1192             synchronized (this) {
1193                 if ((declAnnos = declaredAnnotations) == null) {
1194                     Field root = this.root;
1195                     if (root != null) {
1196                         declAnnos = root.declaredAnnotations();
1197                     } else {
1198                         declAnnos = AnnotationParser.parseAnnotations(
1199                                 annotations,
1200                                 SharedSecrets.getJavaLangAccess()
1201                                         .getConstantPool(getDeclaringClass()),
1202                                 getDeclaringClass());
1203                     }
1204                     declaredAnnotations = declAnnos;
1205                 }
1206             }
1207         }
1208         return declAnnos;
1209     }
1210 
1211     private native byte[] getTypeAnnotationBytes0();
1212 
1213     /**
1214      * Returns an AnnotatedType object that represents the use of a type to specify
1215      * the declared type of the field represented by this Field.
1216      * @return an object representing the declared type of the field
1217      * represented by this Field
1218      *
1219      * @since 1.8
1220      */
1221     public AnnotatedType getAnnotatedType() {
1222         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
1223                                                        SharedSecrets.getJavaLangAccess().
1224                                                            getConstantPool(getDeclaringClass()),
1225                                                        this,
1226                                                        getDeclaringClass(),
1227                                                        getGenericType(),
1228                                                        TypeAnnotation.TypeAnnotationTarget.FIELD);
1229 }
1230 }