< prev index next >

src/java.base/share/classes/java/lang/reflect/Field.java

Print this page

   1 /*
   2  * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import jdk.internal.access.SharedSecrets;

  29 import jdk.internal.reflect.CallerSensitive;
  30 import jdk.internal.reflect.FieldAccessor;
  31 import jdk.internal.reflect.Reflection;
  32 import jdk.internal.vm.annotation.ForceInline;
  33 import jdk.internal.vm.annotation.Stable;
  34 import sun.reflect.generics.repository.FieldRepository;
  35 import sun.reflect.generics.factory.CoreReflectionFactory;
  36 import sun.reflect.generics.factory.GenericsFactory;
  37 import sun.reflect.generics.scope.ClassScope;
  38 import java.lang.annotation.Annotation;
  39 import java.util.Map;
  40 import java.util.Set;
  41 import java.util.Objects;
  42 import sun.reflect.annotation.AnnotationParser;
  43 import sun.reflect.annotation.AnnotationSupport;
  44 import sun.reflect.annotation.TypeAnnotation;
  45 import sun.reflect.annotation.TypeAnnotationParser;
  46 
  47 /**
  48  * A {@code Field} provides information about, and dynamic access to, a

 112             genericInfo = FieldRepository.make(getGenericSignature(),
 113                                                getFactory());
 114         }
 115         return genericInfo; //return cached repository
 116     }
 117 
 118 
 119     /**
 120      * Package-private constructor
 121      */
 122     @SuppressWarnings("deprecation")
 123     Field(Class<?> declaringClass,
 124           String name,
 125           Class<?> type,
 126           int modifiers,
 127           boolean trustedFinal,
 128           int slot,
 129           String signature,
 130           byte[] annotations)
 131     {

 132         this.clazz = declaringClass;
 133         this.name = name;
 134         this.type = type;
 135         this.modifiers = modifiers;
 136         this.trustedFinal = trustedFinal;
 137         this.slot = slot;
 138         this.signature = signature;
 139         this.annotations = annotations;
 140     }
 141 
 142     /**
 143      * Package-private routine (exposed to java.lang.Class via
 144      * ReflectAccess) which returns a copy of this Field. The copy's
 145      * "root" field points to this Field.
 146      */
 147     Field copy() {
 148         // This routine enables sharing of FieldAccessor objects
 149         // among Field objects which refer to the same underlying
 150         // method in the VM. (All of this contortion is only necessary
 151         // because of the "accessibility" bit in AccessibleObject,

 327      * followed by a period, followed by the name of the field.
 328      * For example:
 329      * <pre>
 330      *    public static final int java.lang.Thread.MIN_PRIORITY
 331      *    private int java.io.FileDescriptor.fd
 332      * </pre>
 333      *
 334      * <p>The modifiers are placed in canonical order as specified by
 335      * "The Java Language Specification".  This is {@code public},
 336      * {@code protected} or {@code private} first, and then other
 337      * modifiers in the following order: {@code static}, {@code final},
 338      * {@code transient}, {@code volatile}.
 339      *
 340      * @return a string describing this {@code Field}
 341      * @jls 8.3.1 Field Modifiers
 342      */
 343     public String toString() {
 344         int mod = getModifiers();
 345         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 346             + getType().getTypeName() + " "
 347             + getDeclaringClass().getTypeName() + "."
 348             + getName());
 349     }
 350 
 351     @Override
 352     String toShortString() {
 353         return "field " + getDeclaringClass().getTypeName() + "." + getName();








 354     }
 355 
 356     /**
 357      * Returns a string describing this {@code Field}, including
 358      * its generic type.  The format is the access modifiers for the
 359      * field, if any, followed by the generic field type, followed by
 360      * a space, followed by the fully-qualified name of the class
 361      * declaring the field, followed by a period, followed by the name
 362      * of the field.
 363      *
 364      * <p>The modifiers are placed in canonical order as specified by
 365      * "The Java Language Specification".  This is {@code public},
 366      * {@code protected} or {@code private} first, and then other
 367      * modifiers in the following order: {@code static}, {@code final},
 368      * {@code transient}, {@code volatile}.
 369      *
 370      * @return a string describing this {@code Field}, including
 371      * its generic type
 372      *
 373      * @since 1.5
 374      * @jls 8.3.1 Field Modifiers
 375      */
 376     public String toGenericString() {
 377         int mod = getModifiers();
 378         Type fieldType = getGenericType();
 379         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 380             + fieldType.getTypeName() + " "
 381             + getDeclaringClass().getTypeName() + "."
 382             + getName());
 383     }
 384 
 385     /**
 386      * Returns the value of the field represented by this {@code Field}, on
 387      * the specified object. The value is automatically wrapped in an
 388      * object if it has a primitive type.
 389      *
 390      * <p>The underlying field's value is obtained as follows:
 391      *
 392      * <p>If the underlying field is a static field, the {@code obj} argument
 393      * is ignored; it may be null.
 394      *
 395      * <p>Otherwise, the underlying field is an instance field.  If the
 396      * specified {@code obj} argument is null, the method throws a
 397      * {@code NullPointerException}. If the specified object is not an
 398      * instance of the class or interface declaring the underlying
 399      * field, the method throws an {@code IllegalArgumentException}.
 400      *
 401      * <p>If this {@code Field} object is enforcing Java language access control, and

 754      * <p>If the underlying field is static, the {@code obj} argument is
 755      * ignored; it may be null.
 756      *
 757      * <p>Otherwise the underlying field is an instance field.  If the
 758      * specified object argument is null, the method throws a
 759      * {@code NullPointerException}.  If the specified object argument is not
 760      * an instance of the class or interface declaring the underlying
 761      * field, the method throws an {@code IllegalArgumentException}.
 762      *
 763      * <p>If this {@code Field} object is enforcing Java language access control, and
 764      * the underlying field is inaccessible, the method throws an
 765      * {@code IllegalAccessException}.
 766      *
 767      * <p>If the underlying field is final, this {@code Field} object has
 768      * <em>write</em> access if and only if the following conditions are met:
 769      * <ul>
 770      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
 771      *     this {@code Field} object;</li>
 772      * <li>the field is non-static; and</li>
 773      * <li>the field's declaring class is not a {@linkplain Class#isHidden()
 774      *     hidden class}; and</li>


 775      * <li>the field's declaring class is not a {@linkplain Class#isRecord()
 776      *     record class}.</li>
 777      * </ul>
 778      * If any of the above checks is not met, this method throws an
 779      * {@code IllegalAccessException}.
 780      *
 781      * <p> Setting a final field in this way
 782      * is meaningful only during deserialization or reconstruction of
 783      * instances of classes with blank final fields, before they are
 784      * made available for access by other parts of a program. Use in
 785      * any other context may have unpredictable effects, including cases
 786      * in which other parts of a program continue to use the original
 787      * value of this field.
 788      *
 789      * <p>If the underlying field is of a primitive type, an unwrapping
 790      * conversion is attempted to convert the new value to a value of
 791      * a primitive type.  If this attempt fails, the method throws an
 792      * {@code IllegalArgumentException}.
 793      *
 794      * <p>If, after possible unwrapping, the new value cannot be

   1 /*
   2  * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import jdk.internal.access.SharedSecrets;
  29 import jdk.internal.value.PrimitiveClass;
  30 import jdk.internal.reflect.CallerSensitive;
  31 import jdk.internal.reflect.FieldAccessor;
  32 import jdk.internal.reflect.Reflection;
  33 import jdk.internal.vm.annotation.ForceInline;
  34 import jdk.internal.vm.annotation.Stable;
  35 import sun.reflect.generics.repository.FieldRepository;
  36 import sun.reflect.generics.factory.CoreReflectionFactory;
  37 import sun.reflect.generics.factory.GenericsFactory;
  38 import sun.reflect.generics.scope.ClassScope;
  39 import java.lang.annotation.Annotation;
  40 import java.util.Map;
  41 import java.util.Set;
  42 import java.util.Objects;
  43 import sun.reflect.annotation.AnnotationParser;
  44 import sun.reflect.annotation.AnnotationSupport;
  45 import sun.reflect.annotation.TypeAnnotation;
  46 import sun.reflect.annotation.TypeAnnotationParser;
  47 
  48 /**
  49  * A {@code Field} provides information about, and dynamic access to, a

 113             genericInfo = FieldRepository.make(getGenericSignature(),
 114                                                getFactory());
 115         }
 116         return genericInfo; //return cached repository
 117     }
 118 
 119 
 120     /**
 121      * Package-private constructor
 122      */
 123     @SuppressWarnings("deprecation")
 124     Field(Class<?> declaringClass,
 125           String name,
 126           Class<?> type,
 127           int modifiers,
 128           boolean trustedFinal,
 129           int slot,
 130           String signature,
 131           byte[] annotations)
 132     {
 133         assert PrimitiveClass.isPrimaryType(declaringClass);
 134         this.clazz = declaringClass;
 135         this.name = name;
 136         this.type = type;
 137         this.modifiers = modifiers;
 138         this.trustedFinal = trustedFinal;
 139         this.slot = slot;
 140         this.signature = signature;
 141         this.annotations = annotations;
 142     }
 143 
 144     /**
 145      * Package-private routine (exposed to java.lang.Class via
 146      * ReflectAccess) which returns a copy of this Field. The copy's
 147      * "root" field points to this Field.
 148      */
 149     Field copy() {
 150         // This routine enables sharing of FieldAccessor objects
 151         // among Field objects which refer to the same underlying
 152         // method in the VM. (All of this contortion is only necessary
 153         // because of the "accessibility" bit in AccessibleObject,

 329      * followed by a period, followed by the name of the field.
 330      * For example:
 331      * <pre>
 332      *    public static final int java.lang.Thread.MIN_PRIORITY
 333      *    private int java.io.FileDescriptor.fd
 334      * </pre>
 335      *
 336      * <p>The modifiers are placed in canonical order as specified by
 337      * "The Java Language Specification".  This is {@code public},
 338      * {@code protected} or {@code private} first, and then other
 339      * modifiers in the following order: {@code static}, {@code final},
 340      * {@code transient}, {@code volatile}.
 341      *
 342      * @return a string describing this {@code Field}
 343      * @jls 8.3.1 Field Modifiers
 344      */
 345     public String toString() {
 346         int mod = getModifiers();
 347         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 348             + getType().getTypeName() + " "
 349             + getDeclaringClassTypeName() + "."
 350             + getName());
 351     }
 352 
 353     @Override
 354     String toShortString() {
 355         return "field " + getDeclaringClassTypeName() + "." + getName();
 356     }
 357 
 358     String getDeclaringClassTypeName() {
 359         Class<?> c = getDeclaringClass();
 360         if (PrimitiveClass.isPrimitiveClass(c)) {
 361             c = PrimitiveClass.asValueType(c);
 362         }
 363         return c.getTypeName();
 364     }
 365 
 366     /**
 367      * Returns a string describing this {@code Field}, including
 368      * its generic type.  The format is the access modifiers for the
 369      * field, if any, followed by the generic field type, followed by
 370      * a space, followed by the fully-qualified name of the class
 371      * declaring the field, followed by a period, followed by the name
 372      * of the field.
 373      *
 374      * <p>The modifiers are placed in canonical order as specified by
 375      * "The Java Language Specification".  This is {@code public},
 376      * {@code protected} or {@code private} first, and then other
 377      * modifiers in the following order: {@code static}, {@code final},
 378      * {@code transient}, {@code volatile}.
 379      *
 380      * @return a string describing this {@code Field}, including
 381      * its generic type
 382      *
 383      * @since 1.5
 384      * @jls 8.3.1 Field Modifiers
 385      */
 386     public String toGenericString() {
 387         int mod = getModifiers();
 388         Type fieldType = getGenericType();
 389         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 390             + fieldType.getTypeName() + " "
 391             + getDeclaringClassTypeName() + "."
 392             + getName());
 393     }
 394 
 395     /**
 396      * Returns the value of the field represented by this {@code Field}, on
 397      * the specified object. The value is automatically wrapped in an
 398      * object if it has a primitive type.
 399      *
 400      * <p>The underlying field's value is obtained as follows:
 401      *
 402      * <p>If the underlying field is a static field, the {@code obj} argument
 403      * is ignored; it may be null.
 404      *
 405      * <p>Otherwise, the underlying field is an instance field.  If the
 406      * specified {@code obj} argument is null, the method throws a
 407      * {@code NullPointerException}. If the specified object is not an
 408      * instance of the class or interface declaring the underlying
 409      * field, the method throws an {@code IllegalArgumentException}.
 410      *
 411      * <p>If this {@code Field} object is enforcing Java language access control, and

 764      * <p>If the underlying field is static, the {@code obj} argument is
 765      * ignored; it may be null.
 766      *
 767      * <p>Otherwise the underlying field is an instance field.  If the
 768      * specified object argument is null, the method throws a
 769      * {@code NullPointerException}.  If the specified object argument is not
 770      * an instance of the class or interface declaring the underlying
 771      * field, the method throws an {@code IllegalArgumentException}.
 772      *
 773      * <p>If this {@code Field} object is enforcing Java language access control, and
 774      * the underlying field is inaccessible, the method throws an
 775      * {@code IllegalAccessException}.
 776      *
 777      * <p>If the underlying field is final, this {@code Field} object has
 778      * <em>write</em> access if and only if the following conditions are met:
 779      * <ul>
 780      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
 781      *     this {@code Field} object;</li>
 782      * <li>the field is non-static; and</li>
 783      * <li>the field's declaring class is not a {@linkplain Class#isHidden()
 784      *     hidden class};</li>
 785      * <li>the field's declaring class is not a {@linkplain Class#isValue()
 786      *     value class}; and</li>
 787      * <li>the field's declaring class is not a {@linkplain Class#isRecord()
 788      *     record class}.</li>
 789      * </ul>
 790      * If any of the above checks is not met, this method throws an
 791      * {@code IllegalAccessException}.
 792      *
 793      * <p> Setting a final field in this way
 794      * is meaningful only during deserialization or reconstruction of
 795      * instances of classes with blank final fields, before they are
 796      * made available for access by other parts of a program. Use in
 797      * any other context may have unpredictable effects, including cases
 798      * in which other parts of a program continue to use the original
 799      * value of this field.
 800      *
 801      * <p>If the underlying field is of a primitive type, an unwrapping
 802      * conversion is attempted to convert the new value to a value of
 803      * a primitive type.  If this attempt fails, the method throws an
 804      * {@code IllegalArgumentException}.
 805      *
 806      * <p>If, after possible unwrapping, the new value cannot be
< prev index next >