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