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