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 java.lang.annotation.Annotation;
29 import java.net.URL;
30 import java.security.CodeSource;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.Objects;
34 import jdk.internal.access.SharedSecrets;
35 import jdk.internal.event.FinalFieldMutationEvent;
36 import jdk.internal.loader.ClassLoaders;
37 import jdk.internal.misc.VM;
38 import jdk.internal.module.ModuleBootstrap;
39 import jdk.internal.module.Modules;
40 import jdk.internal.reflect.CallerSensitive;
41 import jdk.internal.reflect.FieldAccessor;
42 import jdk.internal.reflect.Reflection;
43 import jdk.internal.vm.annotation.ForceInline;
44 import jdk.internal.vm.annotation.Stable;
45 import sun.reflect.generics.repository.FieldRepository;
46 import sun.reflect.generics.factory.CoreReflectionFactory;
47 import sun.reflect.generics.factory.GenericsFactory;
48 import sun.reflect.generics.scope.ClassScope;
49 import sun.reflect.annotation.AnnotationParser;
50 import sun.reflect.annotation.AnnotationSupport;
51 import sun.reflect.annotation.TypeAnnotation;
52 import sun.reflect.annotation.TypeAnnotationParser;
53
54 /**
55 * A {@code Field} provides information about, and dynamic access to, a
56 * single field of a class or an interface. The reflected field may
57 * be a class (static) field or an instance field.
58 *
59 * <p>A {@code Field} permits widening conversions to occur during a get or
60 * set access operation, but throws an {@code IllegalArgumentException} if a
61 * narrowing conversion would occur.
63 * @see Member
64 * @see java.lang.Class
65 * @see java.lang.Class#getFields()
66 * @see java.lang.Class#getField(String)
67 * @see java.lang.Class#getDeclaredFields()
68 * @see java.lang.Class#getDeclaredField(String)
69 *
70 * @author Kenneth Russell
71 * @author Nakul Saraiya
72 * @since 1.1
73 */
74 public final
75 class Field extends AccessibleObject implements Member {
76 private final Class<?> clazz;
77 private final int slot;
78 // This is guaranteed to be interned by the VM in the 1.4
79 // reflection implementation
80 private final String name;
81 private final Class<?> type;
82 private final int modifiers;
83 private final boolean trustedFinal;
84 // Generics and annotations support
85 private final transient String signature;
86 private final byte[] annotations;
87
88 /**
89 * Fields are mutable due to {@link AccessibleObject#setAccessible(boolean)}.
90 * Thus, we return a new copy of a root each time a field is returned.
91 * Some lazily initialized immutable states can be stored on root and shared to the copies.
92 */
93 private Field root;
94 private transient volatile FieldRepository genericInfo;
95 private @Stable FieldAccessor fieldAccessor; // access control enabled
96 private @Stable FieldAccessor overrideFieldAccessor; // access control suppressed
97 // End shared states
98
99 // Generics infrastructure
100
101 private String getGenericSignature() {return signature;}
102
103 // Accessor for factory
113 if (genericInfo == null) {
114 var root = this.root;
115 if (root != null) {
116 genericInfo = root.getGenericInfo();
117 } else {
118 genericInfo = FieldRepository.make(getGenericSignature(), getFactory());
119 }
120 this.genericInfo = genericInfo;
121 }
122 return genericInfo;
123 }
124
125 /**
126 * Package-private constructor
127 */
128 @SuppressWarnings("deprecation")
129 Field(Class<?> declaringClass,
130 String name,
131 Class<?> type,
132 int modifiers,
133 boolean trustedFinal,
134 int slot,
135 String signature,
136 byte[] annotations)
137 {
138 this.clazz = declaringClass;
139 this.name = name;
140 this.type = type;
141 this.modifiers = modifiers;
142 this.trustedFinal = trustedFinal;
143 this.slot = slot;
144 this.signature = signature;
145 this.annotations = annotations;
146 }
147
148 /**
149 * Package-private routine (exposed to java.lang.Class via
150 * ReflectAccess) which returns a copy of this Field. The copy's
151 * "root" field points to this Field.
152 */
153 Field copy() {
154 // This routine enables sharing of FieldAccessor objects
155 // among Field objects which refer to the same underlying
156 // method in the VM. (All of this contortion is only necessary
157 // because of the "accessibility" bit in AccessibleObject,
158 // which implicitly requires that new java.lang.reflect
159 // objects be fabricated for each reflective call on Class
160 // objects.)
161 if (this.root != null)
162 throw new IllegalArgumentException("Can not copy a non-root Field");
163
164 Field res = new Field(clazz, name, type, modifiers, trustedFinal, slot, signature, annotations);
165 res.root = this;
166 // Might as well eagerly propagate this if already present
167 res.fieldAccessor = fieldAccessor;
168 res.overrideFieldAccessor = overrideFieldAccessor;
169 res.genericInfo = genericInfo;
170
171 return res;
172 }
173
174 /**
175 * {@inheritDoc}
176 *
177 * <p>If this reflected object represents a non-final field, and this method is
178 * used to enable access, then both <em>{@linkplain #get(Object) read}</em>
179 * and <em>{@linkplain #set(Object, Object) write}</em> access to the field
180 * are enabled.
181 *
182 * <p>If this reflected object represents a <em>non-modifiable</em> final field
183 * then enabling access only enables read access. Any attempt to {@linkplain
184 * #set(Object, Object) set} the field value throws an {@code
223 return name;
224 }
225
226 /**
227 * Returns the Java language modifiers for the field represented
228 * by this {@code Field} object, as an integer. The {@code Modifier} class should
229 * be used to decode the modifiers.
230 *
231 * @see Modifier
232 * @see #accessFlags()
233 * @jls 8.3 Field Declarations
234 * @jls 9.3 Field (Constant) Declarations
235 */
236 public int getModifiers() {
237 return modifiers;
238 }
239
240 /**
241 * {@return an unmodifiable set of the {@linkplain AccessFlag
242 * access flags} for this field, possibly empty}
243 * @see #getModifiers()
244 * @jvms 4.5 Fields
245 * @since 20
246 */
247 @Override
248 public Set<AccessFlag> accessFlags() {
249 return reflectionFactory.parseAccessFlags(getModifiers(), AccessFlag.Location.FIELD, getDeclaringClass());
250 }
251
252 /**
253 * Returns {@code true} if this field represents an element of
254 * an enumerated class; returns {@code false} otherwise.
255 *
256 * @return {@code true} if and only if this field represents an element of
257 * an enumerated class.
258 * @since 1.5
259 * @jls 8.9.1 Enum Constants
260 */
261 public boolean isEnumConstant() {
262 return (getModifiers() & Modifier.ENUM) != 0;
263 }
264
265 /**
266 * Returns {@code true} if this field is a synthetic
267 * field; returns {@code false} otherwise.
268 *
269 * @return true if and only if this field is a synthetic
270 * field as defined by the Java Language Specification.
271 * @since 1.5
272 * @see <a
273 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
274 * programming language and JVM modeling in core reflection</a>
275 */
276 public boolean isSynthetic() {
277 return Modifier.isSynthetic(getModifiers());
278 }
279
280 /**
281 * Returns a {@code Class} object that identifies the
282 * declared type for the field represented by this
283 * {@code Field} object.
284 *
285 * @return a {@code Class} object identifying the declared
286 * type of the field represented by this object
287 */
288 public Class<?> getType() {
289 return type;
290 }
291
292 /**
293 * Returns a {@code Type} object that represents the declared type for
294 * the field represented by this {@code Field} object.
295 *
296 * <p>If the declared type of the field is a parameterized type,
297 * the {@code Type} object returned must accurately reflect the
298 * actual type arguments used in the source code.
299 *
793 * <p>If the underlying field is final, this {@code Field} object has <em>write</em>
794 * access if and only if all of the following conditions are true, where {@code D} is
795 * the field's {@linkplain #getDeclaringClass() declaring class}:
796 *
797 * <ul>
798 * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for this
799 * {@code Field} object.</li>
800 * <li><a href="doc-files/MutationMethods.html">final field mutation is enabled</a>
801 * for the caller's module.</li>
802 * <li> At least one of the following conditions holds:
803 * <ol type="a">
804 * <li> {@code D} and the caller class are in the same module.</li>
805 * <li> The field is {@code public} and {@code D} is {@code public} in a package
806 * that the module containing {@code D} exports to at least the caller's module.</li>
807 * <li> {@code D} is in a package that is {@linkplain Module#isOpen(String, Module)
808 * open} to the caller's module.</li>
809 * </ol>
810 * </li>
811 * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
812 * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
813 * <li>The field is non-static.</li>
814 * </ul>
815 *
816 * <p>If any of the above conditions is not met, this method throws an
817 * {@code IllegalAccessException}.
818 *
819 * <p>These conditions are more restrictive than the conditions specified by {@link
820 * #setAccessible(boolean)} to suppress access checks. In particular, updating a
821 * module to export or open a package cannot be used to allow <em>write</em> access
822 * to final fields with the {@code set} methods defined by {@code Field}.
823 * Condition (b) is not met if the module containing {@code D} has been updated with
824 * {@linkplain Module#addExports(String, Module) addExports} to export the package to
825 * the caller's module. Condition (c) is not met if the module containing {@code D}
826 * has been updated with {@linkplain Module#addOpens(String, Module) addOpens} to open
827 * the package to the caller's module.
828 *
829 * <p>This method may be called by <a href="{@docRoot}/../specs/jni/index.html">
830 * JNI code</a> with no caller class on the stack. In that case, and when the
831 * underlying field is final, this {@code Field} object has <em>write</em> access
832 * if and only if all of the following conditions are true, where {@code D} is the
1333 root.setFieldAccessor(accessor);
1334 }
1335 }
1336
1337 // Sets the overrideFieldAccessor for this Field object and
1338 // (recursively) its root
1339 private void setOverrideFieldAccessor(FieldAccessor accessor) {
1340 overrideFieldAccessor = accessor;
1341 // Propagate up
1342 Field root = this.root;
1343 if (root != null) {
1344 root.setOverrideFieldAccessor(accessor);
1345 }
1346 }
1347
1348 @Override
1349 /* package-private */ Field getRoot() {
1350 return root;
1351 }
1352
1353 /* package-private */ boolean isTrustedFinal() {
1354 return trustedFinal;
1355 }
1356
1357 /**
1358 * {@inheritDoc}
1359 *
1360 * @throws NullPointerException {@inheritDoc}
1361 * @since 1.5
1362 */
1363 @Override
1364 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1365 Objects.requireNonNull(annotationClass);
1366 return annotationClass.cast(declaredAnnotations().get(annotationClass));
1367 }
1368
1369 /**
1370 * {@inheritDoc}
1371 *
1372 * @throws NullPointerException {@inheritDoc}
1373 * @since 1.8
1374 */
|
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 java.lang.annotation.Annotation;
29 import java.net.URL;
30 import java.security.CodeSource;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.Objects;
34 import jdk.internal.access.SharedSecrets;
35 import jdk.internal.event.FinalFieldMutationEvent;
36 import jdk.internal.javac.PreviewFeature;
37 import jdk.internal.loader.ClassLoaders;
38 import jdk.internal.misc.VM;
39 import jdk.internal.module.ModuleBootstrap;
40 import jdk.internal.module.Modules;
41 import jdk.internal.reflect.AccessFlagSet;
42 import jdk.internal.reflect.CallerSensitive;
43 import jdk.internal.reflect.FieldAccessor;
44 import jdk.internal.reflect.PreviewAccessFlags;
45 import jdk.internal.reflect.Reflection;
46 import jdk.internal.vm.annotation.ForceInline;
47 import jdk.internal.vm.annotation.Stable;
48 import sun.reflect.generics.repository.FieldRepository;
49 import sun.reflect.generics.factory.CoreReflectionFactory;
50 import sun.reflect.generics.factory.GenericsFactory;
51 import sun.reflect.generics.scope.ClassScope;
52 import sun.reflect.annotation.AnnotationParser;
53 import sun.reflect.annotation.AnnotationSupport;
54 import sun.reflect.annotation.TypeAnnotation;
55 import sun.reflect.annotation.TypeAnnotationParser;
56
57 /**
58 * A {@code Field} provides information about, and dynamic access to, a
59 * single field of a class or an interface. The reflected field may
60 * be a class (static) field or an instance field.
61 *
62 * <p>A {@code Field} permits widening conversions to occur during a get or
63 * set access operation, but throws an {@code IllegalArgumentException} if a
64 * narrowing conversion would occur.
66 * @see Member
67 * @see java.lang.Class
68 * @see java.lang.Class#getFields()
69 * @see java.lang.Class#getField(String)
70 * @see java.lang.Class#getDeclaredFields()
71 * @see java.lang.Class#getDeclaredField(String)
72 *
73 * @author Kenneth Russell
74 * @author Nakul Saraiya
75 * @since 1.1
76 */
77 public final
78 class Field extends AccessibleObject implements Member {
79 private final Class<?> clazz;
80 private final int slot;
81 // This is guaranteed to be interned by the VM in the 1.4
82 // reflection implementation
83 private final String name;
84 private final Class<?> type;
85 private final int modifiers;
86 private final int flags;
87 // Generics and annotations support
88 private final transient String signature;
89 private final byte[] annotations;
90
91 /**
92 * Fields are mutable due to {@link AccessibleObject#setAccessible(boolean)}.
93 * Thus, we return a new copy of a root each time a field is returned.
94 * Some lazily initialized immutable states can be stored on root and shared to the copies.
95 */
96 private Field root;
97 private transient volatile FieldRepository genericInfo;
98 private @Stable FieldAccessor fieldAccessor; // access control enabled
99 private @Stable FieldAccessor overrideFieldAccessor; // access control suppressed
100 // End shared states
101
102 // Generics infrastructure
103
104 private String getGenericSignature() {return signature;}
105
106 // Accessor for factory
116 if (genericInfo == null) {
117 var root = this.root;
118 if (root != null) {
119 genericInfo = root.getGenericInfo();
120 } else {
121 genericInfo = FieldRepository.make(getGenericSignature(), getFactory());
122 }
123 this.genericInfo = genericInfo;
124 }
125 return genericInfo;
126 }
127
128 /**
129 * Package-private constructor
130 */
131 @SuppressWarnings("deprecation")
132 Field(Class<?> declaringClass,
133 String name,
134 Class<?> type,
135 int modifiers,
136 int flags,
137 int slot,
138 String signature,
139 byte[] annotations)
140 {
141 this.clazz = declaringClass;
142 this.name = name;
143 this.type = type;
144 this.modifiers = modifiers;
145 this.flags = flags;
146 this.slot = slot;
147 this.signature = signature;
148 this.annotations = annotations;
149 }
150
151 /**
152 * Package-private routine (exposed to java.lang.Class via
153 * ReflectAccess) which returns a copy of this Field. The copy's
154 * "root" field points to this Field.
155 */
156 Field copy() {
157 // This routine enables sharing of FieldAccessor objects
158 // among Field objects which refer to the same underlying
159 // method in the VM. (All of this contortion is only necessary
160 // because of the "accessibility" bit in AccessibleObject,
161 // which implicitly requires that new java.lang.reflect
162 // objects be fabricated for each reflective call on Class
163 // objects.)
164 if (this.root != null)
165 throw new IllegalArgumentException("Can not copy a non-root Field");
166
167 Field res = new Field(clazz, name, type, modifiers, flags, slot, signature, annotations);
168 res.root = this;
169 // Might as well eagerly propagate this if already present
170 res.fieldAccessor = fieldAccessor;
171 res.overrideFieldAccessor = overrideFieldAccessor;
172 res.genericInfo = genericInfo;
173
174 return res;
175 }
176
177 /**
178 * {@inheritDoc}
179 *
180 * <p>If this reflected object represents a non-final field, and this method is
181 * used to enable access, then both <em>{@linkplain #get(Object) read}</em>
182 * and <em>{@linkplain #set(Object, Object) write}</em> access to the field
183 * are enabled.
184 *
185 * <p>If this reflected object represents a <em>non-modifiable</em> final field
186 * then enabling access only enables read access. Any attempt to {@linkplain
187 * #set(Object, Object) set} the field value throws an {@code
226 return name;
227 }
228
229 /**
230 * Returns the Java language modifiers for the field represented
231 * by this {@code Field} object, as an integer. The {@code Modifier} class should
232 * be used to decode the modifiers.
233 *
234 * @see Modifier
235 * @see #accessFlags()
236 * @jls 8.3 Field Declarations
237 * @jls 9.3 Field (Constant) Declarations
238 */
239 public int getModifiers() {
240 return modifiers;
241 }
242
243 /**
244 * {@return an unmodifiable set of the {@linkplain AccessFlag
245 * access flags} for this field, possibly empty}
246 * The {@code AccessFlags} may depend on the class file format version of the class.
247 *
248 * @see #getModifiers()
249 * @jvms 4.5 Fields
250 * @since 20
251 */
252 @Override
253 public Set<AccessFlag> accessFlags() {
254 return AccessFlagSet.ofValidated(PreviewAccessFlags.FIELD_PREVIEW_FLAGS, getModifiers());
255 }
256
257 /**
258 * Returns {@code true} if this field represents an element of
259 * an enumerated class; returns {@code false} otherwise.
260 *
261 * @return {@code true} if and only if this field represents an element of
262 * an enumerated class.
263 * @since 1.5
264 * @jls 8.9.1 Enum Constants
265 */
266 public boolean isEnumConstant() {
267 return (getModifiers() & Modifier.ENUM) != 0;
268 }
269
270 /**
271 * Returns {@code true} if this field is a synthetic
272 * field; returns {@code false} otherwise.
273 *
274 * @return true if and only if this field is a synthetic
275 * field as defined by the Java Language Specification.
276 * @since 1.5
277 * @see <a
278 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
279 * programming language and JVM modeling in core reflection</a>
280 */
281 public boolean isSynthetic() {
282 return Modifier.isSynthetic(getModifiers());
283 }
284
285 /**
286 * Returns {@code true} if this field is a strictly
287 * initialized field; returns {@code false} otherwise.
288 *
289 * @return true if and only if this field is a strictly
290 * initialized field as defined by the Java Virtual Machine Specification
291 * @jvms strict-fields-4.5 Field access and property flags
292 * @since Valhalla
293 */
294 @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
295 public boolean isStrictInit() {
296 return accessFlags().contains(AccessFlag.STRICT_INIT);
297 }
298
299 /**
300 * Returns a {@code Class} object that identifies the
301 * declared type for the field represented by this
302 * {@code Field} object.
303 *
304 * @return a {@code Class} object identifying the declared
305 * type of the field represented by this object
306 */
307 public Class<?> getType() {
308 return type;
309 }
310
311 /**
312 * Returns a {@code Type} object that represents the declared type for
313 * the field represented by this {@code Field} object.
314 *
315 * <p>If the declared type of the field is a parameterized type,
316 * the {@code Type} object returned must accurately reflect the
317 * actual type arguments used in the source code.
318 *
812 * <p>If the underlying field is final, this {@code Field} object has <em>write</em>
813 * access if and only if all of the following conditions are true, where {@code D} is
814 * the field's {@linkplain #getDeclaringClass() declaring class}:
815 *
816 * <ul>
817 * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for this
818 * {@code Field} object.</li>
819 * <li><a href="doc-files/MutationMethods.html">final field mutation is enabled</a>
820 * for the caller's module.</li>
821 * <li> At least one of the following conditions holds:
822 * <ol type="a">
823 * <li> {@code D} and the caller class are in the same module.</li>
824 * <li> The field is {@code public} and {@code D} is {@code public} in a package
825 * that the module containing {@code D} exports to at least the caller's module.</li>
826 * <li> {@code D} is in a package that is {@linkplain Module#isOpen(String, Module)
827 * open} to the caller's module.</li>
828 * </ol>
829 * </li>
830 * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
831 * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
832 * <li>{@code D} is not a {@linkplain Class#isValue() value class}.</li>
833 * <li>The field is non-static.</li>
834 * </ul>
835 *
836 * <p>If any of the above conditions is not met, this method throws an
837 * {@code IllegalAccessException}.
838 *
839 * <p>These conditions are more restrictive than the conditions specified by {@link
840 * #setAccessible(boolean)} to suppress access checks. In particular, updating a
841 * module to export or open a package cannot be used to allow <em>write</em> access
842 * to final fields with the {@code set} methods defined by {@code Field}.
843 * Condition (b) is not met if the module containing {@code D} has been updated with
844 * {@linkplain Module#addExports(String, Module) addExports} to export the package to
845 * the caller's module. Condition (c) is not met if the module containing {@code D}
846 * has been updated with {@linkplain Module#addOpens(String, Module) addOpens} to open
847 * the package to the caller's module.
848 *
849 * <p>This method may be called by <a href="{@docRoot}/../specs/jni/index.html">
850 * JNI code</a> with no caller class on the stack. In that case, and when the
851 * underlying field is final, this {@code Field} object has <em>write</em> access
852 * if and only if all of the following conditions are true, where {@code D} is the
1353 root.setFieldAccessor(accessor);
1354 }
1355 }
1356
1357 // Sets the overrideFieldAccessor for this Field object and
1358 // (recursively) its root
1359 private void setOverrideFieldAccessor(FieldAccessor accessor) {
1360 overrideFieldAccessor = accessor;
1361 // Propagate up
1362 Field root = this.root;
1363 if (root != null) {
1364 root.setOverrideFieldAccessor(accessor);
1365 }
1366 }
1367
1368 @Override
1369 /* package-private */ Field getRoot() {
1370 return root;
1371 }
1372
1373 private static final int TRUST_FINAL = 0x0010;
1374 private static final int NULL_RESTRICTED = 0x0020;
1375
1376 /* package-private */ boolean isTrustedFinal() {
1377 return (flags & TRUST_FINAL) == TRUST_FINAL;
1378 }
1379
1380 /* package-private */ boolean isNullRestricted() {
1381 return (flags & NULL_RESTRICTED) == NULL_RESTRICTED;
1382 }
1383
1384 /**
1385 * {@inheritDoc}
1386 *
1387 * @throws NullPointerException {@inheritDoc}
1388 * @since 1.5
1389 */
1390 @Override
1391 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1392 Objects.requireNonNull(annotationClass);
1393 return annotationClass.cast(declaredAnnotations().get(annotationClass));
1394 }
1395
1396 /**
1397 * {@inheritDoc}
1398 *
1399 * @throws NullPointerException {@inheritDoc}
1400 * @since 1.8
1401 */
|