0N/A/*
2362N/A * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang.reflect;
0N/A
6338N/Aimport sun.reflect.CallerSensitive;
0N/Aimport sun.reflect.FieldAccessor;
0N/Aimport sun.reflect.Reflection;
0N/Aimport sun.reflect.generics.repository.FieldRepository;
0N/Aimport sun.reflect.generics.factory.CoreReflectionFactory;
0N/Aimport sun.reflect.generics.factory.GenericsFactory;
0N/Aimport sun.reflect.generics.scope.ClassScope;
0N/Aimport java.lang.annotation.Annotation;
0N/Aimport java.util.Map;
0N/Aimport sun.reflect.annotation.AnnotationParser;
0N/A
0N/A
0N/A/**
0N/A * A {@code Field} provides information about, and dynamic access to, a
0N/A * single field of a class or an interface. The reflected field may
0N/A * be a class (static) field or an instance field.
0N/A *
0N/A * <p>A {@code Field} permits widening conversions to occur during a get or
0N/A * set access operation, but throws an {@code IllegalArgumentException} if a
0N/A * narrowing conversion would occur.
0N/A *
0N/A * @see Member
0N/A * @see java.lang.Class
0N/A * @see java.lang.Class#getFields()
0N/A * @see java.lang.Class#getField(String)
0N/A * @see java.lang.Class#getDeclaredFields()
0N/A * @see java.lang.Class#getDeclaredField(String)
0N/A *
0N/A * @author Kenneth Russell
0N/A * @author Nakul Saraiya
0N/A */
0N/Apublic final
0N/Aclass Field extends AccessibleObject implements Member {
0N/A
1717N/A private Class<?> clazz;
0N/A private int slot;
0N/A // This is guaranteed to be interned by the VM in the 1.4
0N/A // reflection implementation
0N/A private String name;
1717N/A private Class<?> type;
0N/A private int modifiers;
0N/A // Generics and annotations support
0N/A private transient String signature;
0N/A // generic info repository; lazily initialized
0N/A private transient FieldRepository genericInfo;
0N/A private byte[] annotations;
0N/A // Cached field accessor created without override
0N/A private FieldAccessor fieldAccessor;
0N/A // Cached field accessor created with override
0N/A private FieldAccessor overrideFieldAccessor;
0N/A // For sharing of FieldAccessors. This branching structure is
0N/A // currently only two levels deep (i.e., one root Field and
0N/A // potentially many Field objects pointing to it.)
0N/A private Field root;
0N/A
0N/A // Generics infrastructure
0N/A
0N/A private String getGenericSignature() {return signature;}
0N/A
0N/A // Accessor for factory
0N/A private GenericsFactory getFactory() {
0N/A Class<?> c = getDeclaringClass();
0N/A // create scope and factory
0N/A return CoreReflectionFactory.make(c, ClassScope.make(c));
0N/A }
0N/A
0N/A // Accessor for generic info repository
0N/A private FieldRepository getGenericInfo() {
0N/A // lazily initialize repository if necessary
0N/A if (genericInfo == null) {
0N/A // create and cache generic info repository
0N/A genericInfo = FieldRepository.make(getGenericSignature(),
0N/A getFactory());
0N/A }
0N/A return genericInfo; //return cached repository
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Package-private constructor used by ReflectAccess to enable
0N/A * instantiation of these objects in Java code from the java.lang
0N/A * package via sun.reflect.LangReflectAccess.
0N/A */
1717N/A Field(Class<?> declaringClass,
0N/A String name,
1717N/A Class<?> type,
0N/A int modifiers,
0N/A int slot,
0N/A String signature,
0N/A byte[] annotations)
0N/A {
0N/A this.clazz = declaringClass;
0N/A this.name = name;
0N/A this.type = type;
0N/A this.modifiers = modifiers;
0N/A this.slot = slot;
0N/A this.signature = signature;
0N/A this.annotations = annotations;
0N/A }
0N/A
0N/A /**
0N/A * Package-private routine (exposed to java.lang.Class via
0N/A * ReflectAccess) which returns a copy of this Field. The copy's
0N/A * "root" field points to this Field.
0N/A */
0N/A Field copy() {
0N/A // This routine enables sharing of FieldAccessor objects
0N/A // among Field objects which refer to the same underlying
0N/A // method in the VM. (All of this contortion is only necessary
0N/A // because of the "accessibility" bit in AccessibleObject,
0N/A // which implicitly requires that new java.lang.reflect
0N/A // objects be fabricated for each reflective call on Class
0N/A // objects.)
0N/A Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations);
0N/A res.root = this;
0N/A // Might as well eagerly propagate this if already present
0N/A res.fieldAccessor = fieldAccessor;
0N/A res.overrideFieldAccessor = overrideFieldAccessor;
0N/A return res;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code Class} object representing the class or interface
0N/A * that declares the field represented by this {@code Field} object.
0N/A */
0N/A public Class<?> getDeclaringClass() {
0N/A return clazz;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the field represented by this {@code Field} object.
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Java language modifiers for the field represented
0N/A * by this {@code Field} object, as an integer. The {@code Modifier} class should
0N/A * be used to decode the modifiers.
0N/A *
0N/A * @see Modifier
0N/A */
0N/A public int getModifiers() {
0N/A return modifiers;
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this field represents an element of
0N/A * an enumerated type; returns {@code false} otherwise.
0N/A *
0N/A * @return {@code true} if and only if this field represents an element of
0N/A * an enumerated type.
0N/A * @since 1.5
0N/A */
0N/A public boolean isEnumConstant() {
0N/A return (getModifiers() & Modifier.ENUM) != 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this field is a synthetic
0N/A * field; returns {@code false} otherwise.
0N/A *
0N/A * @return true if and only if this field is a synthetic
0N/A * field as defined by the Java Language Specification.
0N/A * @since 1.5
0N/A */
0N/A public boolean isSynthetic() {
0N/A return Modifier.isSynthetic(getModifiers());
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Class} object that identifies the
0N/A * declared type for the field represented by this
0N/A * {@code Field} object.
0N/A *
0N/A * @return a {@code Class} object identifying the declared
0N/A * type of the field represented by this object
0N/A */
0N/A public Class<?> getType() {
0N/A return type;
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Type} object that represents the declared type for
0N/A * the field represented by this {@code Field} object.
0N/A *
0N/A * <p>If the {@code Type} is a parameterized type, the
0N/A * {@code Type} object returned must accurately reflect the
0N/A * actual type parameters used in the source code.
0N/A *
0N/A * <p>If the type of the underlying field is a type variable or a
0N/A * parameterized type, it is created. Otherwise, it is resolved.
0N/A *
0N/A * @return a {@code Type} object that represents the declared type for
0N/A * the field represented by this {@code Field} object
0N/A * @throws GenericSignatureFormatError if the generic field
4008N/A * signature does not conform to the format specified in
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>
0N/A * @throws TypeNotPresentException if the generic type
0N/A * signature of the underlying field refers to a non-existent
0N/A * type declaration
0N/A * @throws MalformedParameterizedTypeException if the generic
0N/A * signature of the underlying field refers to a parameterized type
0N/A * that cannot be instantiated for any reason
0N/A * @since 1.5
0N/A */
0N/A public Type getGenericType() {
0N/A if (getGenericSignature() != null)
0N/A return getGenericInfo().getGenericType();
0N/A else
0N/A return getType();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Compares this {@code Field} against the specified object. Returns
0N/A * true if the objects are the same. Two {@code Field} objects are the same if
0N/A * they were declared by the same class and have the same name
0N/A * and type.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj != null && obj instanceof Field) {
0N/A Field other = (Field)obj;
0N/A return (getDeclaringClass() == other.getDeclaringClass())
0N/A && (getName() == other.getName())
0N/A && (getType() == other.getType());
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this {@code Field}. This is computed as the
0N/A * exclusive-or of the hashcodes for the underlying field's
0N/A * declaring class name and its name.
0N/A */
0N/A public int hashCode() {
0N/A return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string describing this {@code Field}. The format is
0N/A * the access modifiers for the field, if any, followed
0N/A * by the field type, followed by a space, followed by
0N/A * the fully-qualified name of the class declaring the field,
0N/A * followed by a period, followed by the name of the field.
0N/A * For example:
0N/A * <pre>
0N/A * public static final int java.lang.Thread.MIN_PRIORITY
0N/A * private int java.io.FileDescriptor.fd
0N/A * </pre>
0N/A *
0N/A * <p>The modifiers are placed in canonical order as specified by
0N/A * "The Java Language Specification". This is {@code public},
0N/A * {@code protected} or {@code private} first, and then other
0N/A * modifiers in the following order: {@code static}, {@code final},
0N/A * {@code transient}, {@code volatile}.
0N/A */
0N/A public String toString() {
0N/A int mod = getModifiers();
0N/A return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
0N/A + getTypeName(getType()) + " "
0N/A + getTypeName(getDeclaringClass()) + "."
0N/A + getName());
0N/A }
0N/A
0N/A /**
0N/A * Returns a string describing this {@code Field}, including
0N/A * its generic type. The format is the access modifiers for the
0N/A * field, if any, followed by the generic field type, followed by
0N/A * a space, followed by the fully-qualified name of the class
0N/A * declaring the field, followed by a period, followed by the name
0N/A * of the field.
0N/A *
0N/A * <p>The modifiers are placed in canonical order as specified by
0N/A * "The Java Language Specification". This is {@code public},
0N/A * {@code protected} or {@code private} first, and then other
0N/A * modifiers in the following order: {@code static}, {@code final},
0N/A * {@code transient}, {@code volatile}.
0N/A *
0N/A * @return a string describing this {@code Field}, including
0N/A * its generic type
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public String toGenericString() {
0N/A int mod = getModifiers();
0N/A Type fieldType = getGenericType();
0N/A return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
0N/A + ((fieldType instanceof Class) ?
0N/A getTypeName((Class)fieldType): fieldType.toString())+ " "
0N/A + getTypeName(getDeclaringClass()) + "."
0N/A + getName());
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of the field represented by this {@code Field}, on
0N/A * the specified object. The value is automatically wrapped in an
0N/A * object if it has a primitive type.
0N/A *
0N/A * <p>The underlying field's value is obtained as follows:
0N/A *
0N/A * <p>If the underlying field is a static field, the {@code obj} argument
0N/A * is ignored; it may be null.
0N/A *
0N/A * <p>Otherwise, the underlying field is an instance field. If the
0N/A * specified {@code obj} argument is null, the method throws a
0N/A * {@code NullPointerException}. If the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field, the method throws an {@code IllegalArgumentException}.
0N/A *
3896N/A * <p>If this {@code Field} object is enforcing Java language access control, and
0N/A * the underlying field is inaccessible, the method throws an
0N/A * {@code IllegalAccessException}.
0N/A * If the underlying field is static, the class that declared the
0N/A * field is initialized if it has not already been initialized.
0N/A *
0N/A * <p>Otherwise, the value is retrieved from the underlying instance
0N/A * or static field. If the field has a primitive type, the value
0N/A * is wrapped in an object before being returned, otherwise it is
0N/A * returned as is.
0N/A *
0N/A * <p>If the field is hidden in the type of {@code obj},
0N/A * the field's value is obtained according to the preceding rules.
0N/A *
0N/A * @param obj object from which the represented field's value is
0N/A * to be extracted
0N/A * @return the value of the represented field in object
0N/A * {@code obj}; primitive values are wrapped in an appropriate
0N/A * object before being returned
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof).
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A */
6338N/A @CallerSensitive
0N/A public Object get(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).get(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance {@code boolean} field.
0N/A *
0N/A * @param obj the object to extract the {@code boolean} value
0N/A * from
0N/A * @return the value of the {@code boolean} field
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code boolean} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public boolean getBoolean(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getBoolean(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance {@code byte} field.
0N/A *
0N/A * @param obj the object to extract the {@code byte} value
0N/A * from
0N/A * @return the value of the {@code byte} field
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code byte} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public byte getByte(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getByte(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance field of type
0N/A * {@code char} or of another primitive type convertible to
0N/A * type {@code char} via a widening conversion.
0N/A *
0N/A * @param obj the object to extract the {@code char} value
0N/A * from
0N/A * @return the value of the field converted to type {@code char}
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code char} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public char getChar(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getChar(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance field of type
0N/A * {@code short} or of another primitive type convertible to
0N/A * type {@code short} via a widening conversion.
0N/A *
0N/A * @param obj the object to extract the {@code short} value
0N/A * from
0N/A * @return the value of the field converted to type {@code short}
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code short} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public short getShort(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getShort(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance field of type
0N/A * {@code int} or of another primitive type convertible to
0N/A * type {@code int} via a widening conversion.
0N/A *
0N/A * @param obj the object to extract the {@code int} value
0N/A * from
0N/A * @return the value of the field converted to type {@code int}
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code int} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public int getInt(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getInt(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance field of type
0N/A * {@code long} or of another primitive type convertible to
0N/A * type {@code long} via a widening conversion.
0N/A *
0N/A * @param obj the object to extract the {@code long} value
0N/A * from
0N/A * @return the value of the field converted to type {@code long}
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code long} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public long getLong(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getLong(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance field of type
0N/A * {@code float} or of another primitive type convertible to
0N/A * type {@code float} via a widening conversion.
0N/A *
0N/A * @param obj the object to extract the {@code float} value
0N/A * from
0N/A * @return the value of the field converted to type {@code float}
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code float} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public float getFloat(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getFloat(obj);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of a static or instance field of type
0N/A * {@code double} or of another primitive type convertible to
0N/A * type {@code double} via a widening conversion.
0N/A *
0N/A * @param obj the object to extract the {@code double} value
0N/A * from
0N/A * @return the value of the field converted to type {@code double}
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is inaccessible.
0N/A * @exception IllegalArgumentException if the specified object is not
0N/A * an instance of the class or interface declaring the
0N/A * underlying field (or a subclass or implementor
0N/A * thereof), or if the field value cannot be
0N/A * converted to the type {@code double} by a
0N/A * widening conversion.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#get
0N/A */
6338N/A @CallerSensitive
0N/A public double getDouble(Object obj)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A return getFieldAccessor(obj).getDouble(obj);
0N/A }
0N/A
0N/A /**
0N/A * Sets the field represented by this {@code Field} object on the
0N/A * specified object argument to the specified new value. The new
0N/A * value is automatically unwrapped if the underlying field has a
0N/A * primitive type.
0N/A *
0N/A * <p>The operation proceeds as follows:
0N/A *
0N/A * <p>If the underlying field is static, the {@code obj} argument is
0N/A * ignored; it may be null.
0N/A *
0N/A * <p>Otherwise the underlying field is an instance field. If the
0N/A * specified object argument is null, the method throws a
0N/A * {@code NullPointerException}. If the specified object argument is not
0N/A * an instance of the class or interface declaring the underlying
0N/A * field, the method throws an {@code IllegalArgumentException}.
0N/A *
3896N/A * <p>If this {@code Field} object is enforcing Java language access control, and
0N/A * the underlying field is inaccessible, the method throws an
0N/A * {@code IllegalAccessException}.
0N/A *
0N/A * <p>If the underlying field is final, the method throws an
3896N/A * {@code IllegalAccessException} unless {@code setAccessible(true)}
3896N/A * has succeeded for this {@code Field} object
3896N/A * and the field is non-static. Setting a final field in this way
0N/A * is meaningful only during deserialization or reconstruction of
0N/A * instances of classes with blank final fields, before they are
0N/A * made available for access by other parts of a program. Use in
0N/A * any other context may have unpredictable effects, including cases
0N/A * in which other parts of a program continue to use the original
0N/A * value of this field.
0N/A *
0N/A * <p>If the underlying field is of a primitive type, an unwrapping
0N/A * conversion is attempted to convert the new value to a value of
0N/A * a primitive type. If this attempt fails, the method throws an
0N/A * {@code IllegalArgumentException}.
0N/A *
0N/A * <p>If, after possible unwrapping, the new value cannot be
0N/A * converted to the type of the underlying field by an identity or
0N/A * widening conversion, the method throws an
0N/A * {@code IllegalArgumentException}.
0N/A *
0N/A * <p>If the underlying field is static, the class that declared the
0N/A * field is initialized if it has not already been initialized.
0N/A *
0N/A * <p>The field is set to the possibly unwrapped and widened new value.
0N/A *
0N/A * <p>If the field is hidden in the type of {@code obj},
0N/A * the field's value is set according to the preceding rules.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param value the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A */
6338N/A @CallerSensitive
0N/A public void set(Object obj, Object value)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).set(obj, value);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code boolean} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, zObj)},
0N/A * where {@code zObj} is a {@code Boolean} object and
0N/A * {@code zObj.booleanValue() == z}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param z the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setBoolean(Object obj, boolean z)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setBoolean(obj, z);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code byte} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, bObj)},
0N/A * where {@code bObj} is a {@code Byte} object and
0N/A * {@code bObj.byteValue() == b}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param b the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setByte(Object obj, byte b)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setByte(obj, b);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code char} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, cObj)},
0N/A * where {@code cObj} is a {@code Character} object and
0N/A * {@code cObj.charValue() == c}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param c the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setChar(Object obj, char c)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setChar(obj, c);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code short} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, sObj)},
0N/A * where {@code sObj} is a {@code Short} object and
0N/A * {@code sObj.shortValue() == s}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param s the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setShort(Object obj, short s)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setShort(obj, s);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as an {@code int} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, iObj)},
0N/A * where {@code iObj} is a {@code Integer} object and
0N/A * {@code iObj.intValue() == i}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param i the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setInt(Object obj, int i)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setInt(obj, i);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code long} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, lObj)},
0N/A * where {@code lObj} is a {@code Long} object and
0N/A * {@code lObj.longValue() == l}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param l the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setLong(Object obj, long l)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setLong(obj, l);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code float} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, fObj)},
0N/A * where {@code fObj} is a {@code Float} object and
0N/A * {@code fObj.floatValue() == f}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param f the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setFloat(Object obj, float f)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setFloat(obj, f);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of a field as a {@code double} on the specified object.
0N/A * This method is equivalent to
0N/A * {@code set(obj, dObj)},
0N/A * where {@code dObj} is a {@code Double} object and
0N/A * {@code dObj.doubleValue() == d}.
0N/A *
0N/A * @param obj the object whose field should be modified
0N/A * @param d the new value for the field of {@code obj}
0N/A * being modified
0N/A *
3896N/A * @exception IllegalAccessException if this {@code Field} object
3896N/A * is enforcing Java language access control and the underlying
3896N/A * field is either inaccessible or final.
0N/A * @exception IllegalArgumentException if the specified object is not an
0N/A * instance of the class or interface declaring the underlying
0N/A * field (or a subclass or implementor thereof),
0N/A * or if an unwrapping conversion fails.
0N/A * @exception NullPointerException if the specified object is null
0N/A * and the field is an instance field.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A * @see Field#set
0N/A */
6338N/A @CallerSensitive
0N/A public void setDouble(Object obj, double d)
0N/A throws IllegalArgumentException, IllegalAccessException
0N/A {
6338N/A if (!override) {
6338N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers);
6338N/A }
6338N/A }
0N/A getFieldAccessor(obj).setDouble(obj, d);
0N/A }
0N/A
6338N/A // security check is done before calling this method
0N/A private FieldAccessor getFieldAccessor(Object obj)
0N/A throws IllegalAccessException
0N/A {
0N/A boolean ov = override;
6338N/A FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
6338N/A return (a != null) ? a : acquireFieldAccessor(ov);
0N/A }
0N/A
0N/A // NOTE that there is no synchronization used here. It is correct
0N/A // (though not efficient) to generate more than one FieldAccessor
0N/A // for a given Field. However, avoiding synchronization will
0N/A // probably make the implementation more scalable.
0N/A private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
0N/A // First check to see if one has been created yet, and take it
0N/A // if so
0N/A FieldAccessor tmp = null;
0N/A if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);
0N/A if (tmp != null) {
0N/A if (overrideFinalCheck)
0N/A overrideFieldAccessor = tmp;
0N/A else
0N/A fieldAccessor = tmp;
0N/A } else {
0N/A // Otherwise fabricate one and propagate it up to the root
0N/A tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck);
0N/A setFieldAccessor(tmp, overrideFinalCheck);
0N/A }
3903N/A
0N/A return tmp;
0N/A }
0N/A
0N/A // Returns FieldAccessor for this Field object, not looking up
0N/A // the chain to the root
0N/A private FieldAccessor getFieldAccessor(boolean overrideFinalCheck) {
0N/A return (overrideFinalCheck)? overrideFieldAccessor : fieldAccessor;
0N/A }
0N/A
0N/A // Sets the FieldAccessor for this Field object and
0N/A // (recursively) its root
0N/A private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) {
0N/A if (overrideFinalCheck)
0N/A overrideFieldAccessor = accessor;
0N/A else
0N/A fieldAccessor = accessor;
0N/A // Propagate up
0N/A if (root != null) {
0N/A root.setFieldAccessor(accessor, overrideFinalCheck);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Utility routine to paper over array type names
0N/A */
1717N/A static String getTypeName(Class<?> type) {
0N/A if (type.isArray()) {
0N/A try {
1717N/A Class<?> cl = type;
0N/A int dimensions = 0;
0N/A while (cl.isArray()) {
0N/A dimensions++;
0N/A cl = cl.getComponentType();
0N/A }
0N/A StringBuffer sb = new StringBuffer();
0N/A sb.append(cl.getName());
0N/A for (int i = 0; i < dimensions; i++) {
0N/A sb.append("[]");
0N/A }
0N/A return sb.toString();
0N/A } catch (Throwable e) { /*FALLTHRU*/ }
0N/A }
0N/A return type.getName();
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
0N/A if (annotationClass == null)
0N/A throw new NullPointerException();
0N/A
0N/A return (T) declaredAnnotations().get(annotationClass);
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public Annotation[] getDeclaredAnnotations() {
930N/A return AnnotationParser.toArray(declaredAnnotations());
0N/A }
0N/A
1717N/A private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
0N/A
1717N/A private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
0N/A if (declaredAnnotations == null) {
0N/A declaredAnnotations = AnnotationParser.parseAnnotations(
0N/A annotations, sun.misc.SharedSecrets.getJavaLangAccess().
0N/A getConstantPool(getDeclaringClass()),
0N/A getDeclaringClass());
0N/A }
0N/A return declaredAnnotations;
0N/A }
0N/A}