< prev index next >

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

Print this page

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

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

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








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

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


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

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

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

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