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