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         if (!override) {
 777             Class<?> caller = Reflection.getCallerClass();
 778             checkAccess(caller, obj);
 779         }
 780         getFieldAccessor(obj).set(obj, value);
 781     }
 782 
 783     /**
 784      * Sets the value of a field as a {@code boolean} on the specified object.
 785      * This method is equivalent to
 786      * {@code set(obj, zObj)},
 787      * where {@code zObj} is a {@code Boolean} object and
 788      * {@code zObj.booleanValue() == z}.
 789      *
 790      * @param obj the object whose field should be modified
 791      * @param z   the new value for the field of {@code obj}
 792      * being modified
 793      *
 794      * @exception IllegalAccessException    if this {@code Field} object
 795      *              is enforcing Java language access control and the underlying
 796      *              field is either inaccessible or final.
 797      * @exception IllegalArgumentException  if the specified object is not an
 798      *              instance of the class or interface declaring the underlying
 799      *              field (or a subclass or implementor thereof),
 800      *              or if an unwrapping conversion fails.
 801      * @exception NullPointerException      if the specified object is null
 802      *              and the field is an instance field.
 803      * @exception ExceptionInInitializerError if the initialization provoked
 804      *              by this method fails.
 805      * @see       Field#set
 806      */
 807     @CallerSensitive
 808     @ForceInline // to ensure Reflection.getCallerClass optimization
 809     public void setBoolean(Object obj, boolean z)
 810         throws IllegalArgumentException, IllegalAccessException
 811     {
 812         if (!override) {
 813             Class<?> caller = Reflection.getCallerClass();
 814             checkAccess(caller, obj);
 815         }
 816         getFieldAccessor(obj).setBoolean(obj, z);
 817     }
 818 
 819     /**
 820      * Sets the value of a field as a {@code byte} on the specified object.
 821      * This method is equivalent to
 822      * {@code set(obj, bObj)},
 823      * where {@code bObj} is a {@code Byte} object and
 824      * {@code bObj.byteValue() == b}.
 825      *
 826      * @param obj the object whose field should be modified
 827      * @param b   the new value for the field of {@code obj}
 828      * being modified
 829      *
 830      * @exception IllegalAccessException    if this {@code Field} object
 831      *              is enforcing Java language access control and the underlying
 832      *              field is either inaccessible or final.
 833      * @exception IllegalArgumentException  if the specified object is not an
 834      *              instance of the class or interface declaring the underlying
 835      *              field (or a subclass or implementor thereof),
 836      *              or if an unwrapping conversion fails.
 837      * @exception NullPointerException      if the specified object is null
 838      *              and the field is an instance field.
 839      * @exception ExceptionInInitializerError if the initialization provoked
 840      *              by this method fails.
 841      * @see       Field#set
 842      */
 843     @CallerSensitive
 844     @ForceInline // to ensure Reflection.getCallerClass optimization
 845     public void setByte(Object obj, byte b)
 846         throws IllegalArgumentException, IllegalAccessException
 847     {
 848         if (!override) {
 849             Class<?> caller = Reflection.getCallerClass();
 850             checkAccess(caller, obj);
 851         }
 852         getFieldAccessor(obj).setByte(obj, b);
 853     }
 854 
 855     /**
 856      * Sets the value of a field as a {@code char} on the specified object.
 857      * This method is equivalent to
 858      * {@code set(obj, cObj)},
 859      * where {@code cObj} is a {@code Character} object and
 860      * {@code cObj.charValue() == c}.
 861      *
 862      * @param obj the object whose field should be modified
 863      * @param c   the new value for the field of {@code obj}
 864      * being modified
 865      *
 866      * @exception IllegalAccessException    if this {@code Field} object
 867      *              is enforcing Java language access control and the underlying
 868      *              field is either inaccessible or final.
 869      * @exception IllegalArgumentException  if the specified object is not an
 870      *              instance of the class or interface declaring the underlying
 871      *              field (or a subclass or implementor thereof),
 872      *              or if an unwrapping conversion fails.
 873      * @exception NullPointerException      if the specified object is null
 874      *              and the field is an instance field.
 875      * @exception ExceptionInInitializerError if the initialization provoked
 876      *              by this method fails.
 877      * @see       Field#set
 878      */
 879     @CallerSensitive
 880     @ForceInline // to ensure Reflection.getCallerClass optimization
 881     public void setChar(Object obj, char c)
 882         throws IllegalArgumentException, IllegalAccessException
 883     {
 884         if (!override) {
 885             Class<?> caller = Reflection.getCallerClass();
 886             checkAccess(caller, obj);
 887         }
 888         getFieldAccessor(obj).setChar(obj, c);
 889     }
 890 
 891     /**
 892      * Sets the value of a field as a {@code short} on the specified object.
 893      * This method is equivalent to
 894      * {@code set(obj, sObj)},
 895      * where {@code sObj} is a {@code Short} object and
 896      * {@code sObj.shortValue() == s}.
 897      *
 898      * @param obj the object whose field should be modified
 899      * @param s   the new value for the field of {@code obj}
 900      * being modified
 901      *
 902      * @exception IllegalAccessException    if this {@code Field} object
 903      *              is enforcing Java language access control and the underlying
 904      *              field is either inaccessible or final.
 905      * @exception IllegalArgumentException  if the specified object is not an
 906      *              instance of the class or interface declaring the underlying
 907      *              field (or a subclass or implementor thereof),
 908      *              or if an unwrapping conversion fails.
 909      * @exception NullPointerException      if the specified object is null
 910      *              and the field is an instance field.
 911      * @exception ExceptionInInitializerError if the initialization provoked
 912      *              by this method fails.
 913      * @see       Field#set
 914      */
 915     @CallerSensitive
 916     @ForceInline // to ensure Reflection.getCallerClass optimization
 917     public void setShort(Object obj, short s)
 918         throws IllegalArgumentException, IllegalAccessException
 919     {
 920         if (!override) {
 921             Class<?> caller = Reflection.getCallerClass();
 922             checkAccess(caller, obj);
 923         }
 924         getFieldAccessor(obj).setShort(obj, s);
 925     }
 926 
 927     /**
 928      * Sets the value of a field as an {@code int} on the specified object.
 929      * This method is equivalent to
 930      * {@code set(obj, iObj)},
 931      * where {@code iObj} is an {@code Integer} object and
 932      * {@code iObj.intValue() == i}.
 933      *
 934      * @param obj the object whose field should be modified
 935      * @param i   the new value for the field of {@code obj}
 936      * being modified
 937      *
 938      * @exception IllegalAccessException    if this {@code Field} object
 939      *              is enforcing Java language access control and the underlying
 940      *              field is either inaccessible or final.
 941      * @exception IllegalArgumentException  if the specified object is not an
 942      *              instance of the class or interface declaring the underlying
 943      *              field (or a subclass or implementor thereof),
 944      *              or if an unwrapping conversion fails.
 945      * @exception NullPointerException      if the specified object is null
 946      *              and the field is an instance field.
 947      * @exception ExceptionInInitializerError if the initialization provoked
 948      *              by this method fails.
 949      * @see       Field#set
 950      */
 951     @CallerSensitive
 952     @ForceInline // to ensure Reflection.getCallerClass optimization
 953     public void setInt(Object obj, int i)
 954         throws IllegalArgumentException, IllegalAccessException
 955     {
 956         if (!override) {
 957             Class<?> caller = Reflection.getCallerClass();
 958             checkAccess(caller, obj);
 959         }
 960         getFieldAccessor(obj).setInt(obj, i);
 961     }
 962 
 963     /**
 964      * Sets the value of a field as a {@code long} on the specified object.
 965      * This method is equivalent to
 966      * {@code set(obj, lObj)},
 967      * where {@code lObj} is a {@code Long} object and
 968      * {@code lObj.longValue() == l}.
 969      *
 970      * @param obj the object whose field should be modified
 971      * @param l   the new value for the field of {@code obj}
 972      * being modified
 973      *
 974      * @exception IllegalAccessException    if this {@code Field} object
 975      *              is enforcing Java language access control and the underlying
 976      *              field is either inaccessible or final.
 977      * @exception IllegalArgumentException  if the specified object is not an
 978      *              instance of the class or interface declaring the underlying
 979      *              field (or a subclass or implementor thereof),
 980      *              or if an unwrapping conversion fails.
 981      * @exception NullPointerException      if the specified object is null
 982      *              and the field is an instance field.
 983      * @exception ExceptionInInitializerError if the initialization provoked
 984      *              by this method fails.
 985      * @see       Field#set
 986      */
 987     @CallerSensitive
 988     @ForceInline // to ensure Reflection.getCallerClass optimization
 989     public void setLong(Object obj, long l)
 990         throws IllegalArgumentException, IllegalAccessException
 991     {
 992         if (!override) {
 993             Class<?> caller = Reflection.getCallerClass();
 994             checkAccess(caller, obj);
 995         }
 996         getFieldAccessor(obj).setLong(obj, l);
 997     }
 998 
 999     /**
1000      * Sets the value of a field as a {@code float} on the specified object.
1001      * This method is equivalent to
1002      * {@code set(obj, fObj)},
1003      * where {@code fObj} is a {@code Float} object and
1004      * {@code fObj.floatValue() == f}.
1005      *
1006      * @param obj the object whose field should be modified
1007      * @param f   the new value for the field of {@code obj}
1008      * being modified
1009      *
1010      * @exception IllegalAccessException    if this {@code Field} object
1011      *              is enforcing Java language access control and the underlying
1012      *              field is either inaccessible or final.
1013      * @exception IllegalArgumentException  if the specified object is not an
1014      *              instance of the class or interface declaring the underlying
1015      *              field (or a subclass or implementor thereof),
1016      *              or if an unwrapping conversion fails.
1017      * @exception NullPointerException      if the specified object is null
1018      *              and the field is an instance field.
1019      * @exception ExceptionInInitializerError if the initialization provoked
1020      *              by this method fails.
1021      * @see       Field#set
1022      */
1023     @CallerSensitive
1024     @ForceInline // to ensure Reflection.getCallerClass optimization
1025     public void setFloat(Object obj, float f)
1026         throws IllegalArgumentException, IllegalAccessException
1027     {
1028         if (!override) {
1029             Class<?> caller = Reflection.getCallerClass();
1030             checkAccess(caller, obj);
1031         }
1032         getFieldAccessor(obj).setFloat(obj, f);
1033     }
1034 
1035     /**
1036      * Sets the value of a field as a {@code double} on the specified object.
1037      * This method is equivalent to
1038      * {@code set(obj, dObj)},
1039      * where {@code dObj} is a {@code Double} object and
1040      * {@code dObj.doubleValue() == d}.
1041      *
1042      * @param obj the object whose field should be modified
1043      * @param d   the new value for the field of {@code obj}
1044      * being modified
1045      *
1046      * @exception IllegalAccessException    if this {@code Field} object
1047      *              is enforcing Java language access control and the underlying
1048      *              field is either inaccessible or final.
1049      * @exception IllegalArgumentException  if the specified object is not an
1050      *              instance of the class or interface declaring the underlying
1051      *              field (or a subclass or implementor thereof),
1052      *              or if an unwrapping conversion fails.
1053      * @exception NullPointerException      if the specified object is null
1054      *              and the field is an instance field.
1055      * @exception ExceptionInInitializerError if the initialization provoked
1056      *              by this method fails.
1057      * @see       Field#set
1058      */
1059     @CallerSensitive
1060     @ForceInline // to ensure Reflection.getCallerClass optimization
1061     public void setDouble(Object obj, double d)
1062         throws IllegalArgumentException, IllegalAccessException
1063     {
1064         if (!override) {
1065             Class<?> caller = Reflection.getCallerClass();
1066             checkAccess(caller, obj);
1067         }
1068         getFieldAccessor(obj).setDouble(obj, d);
1069     }
1070 
1071     // check access to field
1072     private void checkAccess(Class<?> caller, Object obj)
1073         throws IllegalAccessException
1074     {
1075         checkAccess(caller, clazz,
1076                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
1077                     modifiers);
1078     }
1079 
1080     // security check is done before calling this method
1081     private FieldAccessor getFieldAccessor(Object obj)
1082         throws IllegalAccessException
1083     {
1084         boolean ov = override;
1085         FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
1086         return (a != null) ? a : acquireFieldAccessor(ov);
1087     }
1088 
1089     // NOTE that there is no synchronization used here. It is correct
1090     // (though not efficient) to generate more than one FieldAccessor
1091     // for a given Field. However, avoiding synchronization will
1092     // probably make the implementation more scalable.
1093     private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
1094         // First check to see if one has been created yet, and take it
1095         // if so
1096         FieldAccessor tmp = null;
1097         if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);
1098         if (tmp != null) {
1099             if (overrideFinalCheck)
1100                 overrideFieldAccessor = tmp;
1101             else
1102                 fieldAccessor = tmp;
1103         } else {
1104             // Otherwise fabricate one and propagate it up to the root
1105             tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck);
1106             setFieldAccessor(tmp, overrideFinalCheck);
1107         }
1108 
1109         return tmp;
1110     }
1111 
1112     // Returns FieldAccessor for this Field object, not looking up
1113     // the chain to the root
1114     private FieldAccessor getFieldAccessor(boolean overrideFinalCheck) {
1115         return (overrideFinalCheck)? overrideFieldAccessor : fieldAccessor;
1116     }
1117 
1118     // Sets the FieldAccessor for this Field object and
1119     // (recursively) its root
1120     private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) {
1121         if (overrideFinalCheck)
1122             overrideFieldAccessor = accessor;
1123         else
1124             fieldAccessor = accessor;
1125         // Propagate up
1126         if (root != null) {
1127             root.setFieldAccessor(accessor, overrideFinalCheck);
1128         }
1129     }
1130 
1131     @Override
1132     Field getRoot() {
1133         return root;
1134     }
1135 
1136     /**
1137      * @throws NullPointerException {@inheritDoc}
1138      * @since 1.5
1139      */
1140     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1141         Objects.requireNonNull(annotationClass);
1142         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1143     }
1144 
1145     /**
1146      * {@inheritDoc}
1147      * @throws NullPointerException {@inheritDoc}
1148      * @since 1.8
1149      */
1150     @Override
1151     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1152         Objects.requireNonNull(annotationClass);
1153 
1154         return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
1155     }
1156 
1157     /**
1158      * {@inheritDoc}
1159      */
1160     public Annotation[] getDeclaredAnnotations()  {
1161         return AnnotationParser.toArray(declaredAnnotations());
1162     }
1163 
1164     private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
1165 
1166     private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
1167         Map<Class<? extends Annotation>, Annotation> declAnnos;
1168         if ((declAnnos = declaredAnnotations) == null) {
1169             synchronized (this) {
1170                 if ((declAnnos = declaredAnnotations) == null) {
1171                     Field root = this.root;
1172                     if (root != null) {
1173                         declAnnos = root.declaredAnnotations();
1174                     } else {
1175                         declAnnos = AnnotationParser.parseAnnotations(
1176                                 annotations,
1177                                 SharedSecrets.getJavaLangAccess()
1178                                         .getConstantPool(getDeclaringClass()),
1179                                 getDeclaringClass());
1180                     }
1181                     declaredAnnotations = declAnnos;
1182                 }
1183             }
1184         }
1185         return declAnnos;
1186     }
1187 
1188     private native byte[] getTypeAnnotationBytes0();
1189 
1190     /**
1191      * Returns an AnnotatedType object that represents the use of a type to specify
1192      * the declared type of the field represented by this Field.
1193      * @return an object representing the declared type of the field
1194      * represented by this Field
1195      *
1196      * @since 1.8
1197      */
1198     public AnnotatedType getAnnotatedType() {
1199         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
1200                                                        SharedSecrets.getJavaLangAccess().
1201                                                            getConstantPool(getDeclaringClass()),
1202                                                        this,
1203                                                        getDeclaringClass(),
1204                                                        getGenericType(),
1205                                                        TypeAnnotation.TypeAnnotationTarget.FIELD);
1206 }
1207 }