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