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