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