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