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
114 getFactory());
115 this.genericInfo = genericInfo;
116 }
117 return genericInfo; //return cached repository
118 }
119
120
121 /**
122 * Package-private constructor
123 */
124 @SuppressWarnings("deprecation")
125 Field(Class<?> declaringClass,
126 String name,
127 Class<?> type,
128 int modifiers,
129 boolean trustedFinal,
130 int slot,
131 String signature,
132 byte[] annotations)
133 {
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,
199 return name;
200 }
201
202 /**
203 * Returns the Java language modifiers for the field represented
204 * by this {@code Field} object, as an integer. The {@code Modifier} class should
205 * be used to decode the modifiers.
206 *
207 * @see Modifier
208 * @see #accessFlags()
209 * @jls 8.3 Field Declarations
210 * @jls 9.3 Field (Constant) Declarations
211 */
212 public int getModifiers() {
213 return modifiers;
214 }
215
216 /**
217 * {@return an unmodifiable set of the {@linkplain AccessFlag
218 * access flags} for this field, possibly empty}
219 * @see #getModifiers()
220 * @jvms 4.5 Fields
221 * @since 20
222 */
223 @Override
224 public Set<AccessFlag> accessFlags() {
225 return AccessFlag.maskToAccessFlags(getModifiers(), AccessFlag.Location.FIELD);
226 }
227
228 /**
229 * Returns {@code true} if this field represents an element of
230 * an enumerated class; returns {@code false} otherwise.
231 *
232 * @return {@code true} if and only if this field represents an element of
233 * an enumerated class.
234 * @since 1.5
235 * @jls 8.9.1 Enum Constants
236 */
237 public boolean isEnumConstant() {
238 return (getModifiers() & Modifier.ENUM) != 0;
239 }
240
241 /**
242 * Returns {@code true} if this field is a synthetic
243 * field; returns {@code false} otherwise.
244 *
245 * @return true if and only if this field is a synthetic
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 + getDeclaringClass().getTypeName() + "."
350 + getName());
351 }
352
353 @Override
354 String toShortString() {
355 return "field " + getDeclaringClass().getTypeName() + "." + getName();
356 }
357
358 /**
359 * Returns a string describing this {@code Field}, including
360 * its generic type. The format is the access modifiers for the
361 * field, if any, followed by the generic field type, followed by
362 * a space, followed by the fully-qualified name of the class
363 * declaring the field, followed by a period, followed by the name
364 * of the field.
365 *
366 * <p>The modifiers are placed in canonical order as specified by
367 * "The Java Language Specification". This is {@code public},
368 * {@code protected} or {@code private} first, and then other
369 * modifiers in the following order: {@code static}, {@code final},
370 * {@code transient}, {@code volatile}.
371 *
372 * @return a string describing this {@code Field}, including
373 * its generic type
374 *
375 * @since 1.5
376 * @jls 8.3.1 Field Modifiers
377 */
378 public String toGenericString() {
379 int mod = getModifiers();
380 Type fieldType = getGenericType();
381 return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
382 + fieldType.getTypeName() + " "
383 + getDeclaringClass().getTypeName() + "."
384 + getName());
385 }
386
387 /**
388 * Returns the value of the field represented by this {@code Field}, on
389 * the specified object. The value is automatically wrapped in an
390 * object if it has a primitive type.
391 *
392 * <p>The underlying field's value is obtained as follows:
393 *
394 * <p>If the underlying field is a static field, the {@code obj} argument
395 * is ignored; it may be null.
396 *
397 * <p>Otherwise, the underlying field is an instance field. If the
398 * specified {@code obj} argument is null, the method throws a
399 * {@code NullPointerException}. If the specified object is not an
400 * instance of the class or interface declaring the underlying
401 * field, the method throws an {@code IllegalArgumentException}.
402 *
403 * <p>If this {@code Field} object is enforcing Java language access control, and
756 * <p>If the underlying field is static, the {@code obj} argument is
757 * ignored; it may be null.
758 *
759 * <p>Otherwise the underlying field is an instance field. If the
760 * specified object argument is null, the method throws a
761 * {@code NullPointerException}. If the specified object argument is not
762 * an instance of the class or interface declaring the underlying
763 * field, the method throws an {@code IllegalArgumentException}.
764 *
765 * <p>If this {@code Field} object is enforcing Java language access control, and
766 * the underlying field is inaccessible, the method throws an
767 * {@code IllegalAccessException}.
768 *
769 * <p>If the underlying field is final, this {@code Field} object has
770 * <em>write</em> access if and only if the following conditions are met:
771 * <ul>
772 * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
773 * this {@code Field} object;</li>
774 * <li>the field is non-static; and</li>
775 * <li>the field's declaring class is not a {@linkplain Class#isHidden()
776 * hidden class}; and</li>
777 * <li>the field's declaring class is not a {@linkplain Class#isRecord()
778 * record class}.</li>
779 * </ul>
780 * If any of the above checks is not met, this method throws an
781 * {@code IllegalAccessException}.
782 *
783 * <p> Setting a final field in this way
784 * is meaningful only during deserialization or reconstruction of
785 * instances of classes with blank final fields, before they are
786 * made available for access by other parts of a program. Use in
787 * any other context may have unpredictable effects, including cases
788 * in which other parts of a program continue to use the original
789 * value of this field.
790 *
791 * <p>If the underlying field is of a primitive type, an unwrapping
792 * conversion is attempted to convert the new value to a value of
793 * a primitive type. If this attempt fails, the method throws an
794 * {@code IllegalArgumentException}.
795 *
796 * <p>If, after possible unwrapping, the new value cannot be
|
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
115 getFactory());
116 this.genericInfo = genericInfo;
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 PrimitiveClass.isPrimaryType(declaringClass);
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,
201 return name;
202 }
203
204 /**
205 * Returns the Java language modifiers for the field represented
206 * by this {@code Field} object, as an integer. The {@code Modifier} class should
207 * be used to decode the modifiers.
208 *
209 * @see Modifier
210 * @see #accessFlags()
211 * @jls 8.3 Field Declarations
212 * @jls 9.3 Field (Constant) Declarations
213 */
214 public int getModifiers() {
215 return modifiers;
216 }
217
218 /**
219 * {@return an unmodifiable set of the {@linkplain AccessFlag
220 * access flags} for this field, possibly empty}
221 * The {@code AccessFlags} may depend on the class file format version of the class.
222 *
223 * @see #getModifiers()
224 * @jvms 4.5 Fields
225 * @since 20
226 */
227 @Override
228 public Set<AccessFlag> accessFlags() {
229 int major = SharedSecrets.getJavaLangAccess().classFileFormatVersion(getDeclaringClass()) & 0xffff;
230 var cffv = ClassFileFormatVersion.fromMajor(major);
231 return AccessFlag.maskToAccessFlags(getModifiers(),
232 AccessFlag.Location.FIELD,
233 cffv);
234 }
235
236 /**
237 * Returns {@code true} if this field represents an element of
238 * an enumerated class; returns {@code false} otherwise.
239 *
240 * @return {@code true} if and only if this field represents an element of
241 * an enumerated class.
242 * @since 1.5
243 * @jls 8.9.1 Enum Constants
244 */
245 public boolean isEnumConstant() {
246 return (getModifiers() & Modifier.ENUM) != 0;
247 }
248
249 /**
250 * Returns {@code true} if this field is a synthetic
251 * field; returns {@code false} otherwise.
252 *
253 * @return true if and only if this field is a synthetic
337 * followed by a period, followed by the name of the field.
338 * For example:
339 * <pre>
340 * public static final int java.lang.Thread.MIN_PRIORITY
341 * private int java.io.FileDescriptor.fd
342 * </pre>
343 *
344 * <p>The modifiers are placed in canonical order as specified by
345 * "The Java Language Specification". This is {@code public},
346 * {@code protected} or {@code private} first, and then other
347 * modifiers in the following order: {@code static}, {@code final},
348 * {@code transient}, {@code volatile}.
349 *
350 * @return a string describing this {@code Field}
351 * @jls 8.3.1 Field Modifiers
352 */
353 public String toString() {
354 int mod = getModifiers();
355 return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
356 + getType().getTypeName() + " "
357 + getDeclaringClassTypeName() + "."
358 + getName());
359 }
360
361 @Override
362 String toShortString() {
363 return "field " + getDeclaringClassTypeName() + "." + getName();
364 }
365
366 String getDeclaringClassTypeName() {
367 Class<?> c = getDeclaringClass();
368 if (PrimitiveClass.isPrimitiveClass(c)) {
369 c = PrimitiveClass.asValueType(c);
370 }
371 return c.getTypeName();
372 }
373
374 /**
375 * Returns a string describing this {@code Field}, including
376 * its generic type. The format is the access modifiers for the
377 * field, if any, followed by the generic field type, followed by
378 * a space, followed by the fully-qualified name of the class
379 * declaring the field, followed by a period, followed by the name
380 * of the field.
381 *
382 * <p>The modifiers are placed in canonical order as specified by
383 * "The Java Language Specification". This is {@code public},
384 * {@code protected} or {@code private} first, and then other
385 * modifiers in the following order: {@code static}, {@code final},
386 * {@code transient}, {@code volatile}.
387 *
388 * @return a string describing this {@code Field}, including
389 * its generic type
390 *
391 * @since 1.5
392 * @jls 8.3.1 Field Modifiers
393 */
394 public String toGenericString() {
395 int mod = getModifiers();
396 Type fieldType = getGenericType();
397 return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
398 + fieldType.getTypeName() + " "
399 + getDeclaringClassTypeName() + "."
400 + getName());
401 }
402
403 /**
404 * Returns the value of the field represented by this {@code Field}, on
405 * the specified object. The value is automatically wrapped in an
406 * object if it has a primitive type.
407 *
408 * <p>The underlying field's value is obtained as follows:
409 *
410 * <p>If the underlying field is a static field, the {@code obj} argument
411 * is ignored; it may be null.
412 *
413 * <p>Otherwise, the underlying field is an instance field. If the
414 * specified {@code obj} argument is null, the method throws a
415 * {@code NullPointerException}. If the specified object is not an
416 * instance of the class or interface declaring the underlying
417 * field, the method throws an {@code IllegalArgumentException}.
418 *
419 * <p>If this {@code Field} object is enforcing Java language access control, and
772 * <p>If the underlying field is static, the {@code obj} argument is
773 * ignored; it may be null.
774 *
775 * <p>Otherwise the underlying field is an instance field. If the
776 * specified object argument is null, the method throws a
777 * {@code NullPointerException}. If the specified object argument is not
778 * an instance of the class or interface declaring the underlying
779 * field, the method throws an {@code IllegalArgumentException}.
780 *
781 * <p>If this {@code Field} object is enforcing Java language access control, and
782 * the underlying field is inaccessible, the method throws an
783 * {@code IllegalAccessException}.
784 *
785 * <p>If the underlying field is final, this {@code Field} object has
786 * <em>write</em> access if and only if the following conditions are met:
787 * <ul>
788 * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
789 * this {@code Field} object;</li>
790 * <li>the field is non-static; and</li>
791 * <li>the field's declaring class is not a {@linkplain Class#isHidden()
792 * hidden class};</li>
793 * <li>the field's declaring class is not a {@linkplain Class#isValue()
794 * value class}; and</li>
795 * <li>the field's declaring class is not a {@linkplain Class#isRecord()
796 * record class}.</li>
797 * </ul>
798 * If any of the above checks is not met, this method throws an
799 * {@code IllegalAccessException}.
800 *
801 * <p> Setting a final field in this way
802 * is meaningful only during deserialization or reconstruction of
803 * instances of classes with blank final fields, before they are
804 * made available for access by other parts of a program. Use in
805 * any other context may have unpredictable effects, including cases
806 * in which other parts of a program continue to use the original
807 * value of this field.
808 *
809 * <p>If the underlying field is of a primitive type, an unwrapping
810 * conversion is attempted to convert the new value to a value of
811 * a primitive type. If this attempt fails, the method throws an
812 * {@code IllegalArgumentException}.
813 *
814 * <p>If, after possible unwrapping, the new value cannot be
|