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