< prev index next >

src/java.base/share/classes/jdk/internal/reflect/UnsafeFieldAccessorImpl.java

Print this page

34     standpoint of reflection code: the eight primitive types and
35     Object. Using class Unsafe instead of generated bytecodes saves
36     memory and loading time for the dynamically-generated
37     FieldAccessors. */
38 
39 abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
40     static final Unsafe unsafe = Unsafe.getUnsafe();
41 
42     protected final long    fieldOffset;
43     protected final boolean isFinal;
44 
45     UnsafeFieldAccessorImpl(Field field) {
46         super(field);
47         int mods = field.getModifiers();
48         this.isFinal = Modifier.isFinal(mods);
49         if (Modifier.isStatic(mods))
50             fieldOffset = unsafe.staticFieldOffset(field);
51         else
52             fieldOffset = unsafe.objectFieldOffset(field);
53     }

























54 }

34     standpoint of reflection code: the eight primitive types and
35     Object. Using class Unsafe instead of generated bytecodes saves
36     memory and loading time for the dynamically-generated
37     FieldAccessors. */
38 
39 abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
40     static final Unsafe unsafe = Unsafe.getUnsafe();
41 
42     protected final long    fieldOffset;
43     protected final boolean isFinal;
44 
45     UnsafeFieldAccessorImpl(Field field) {
46         super(field);
47         int mods = field.getModifiers();
48         this.isFinal = Modifier.isFinal(mods);
49         if (Modifier.isStatic(mods))
50             fieldOffset = unsafe.staticFieldOffset(field);
51         else
52             fieldOffset = unsafe.objectFieldOffset(field);
53     }
54 
55     protected boolean isFlattened() {
56         return unsafe.isFlattened(field);
57     }
58 
59     protected boolean canBeNull() {
60         return !field.getType().isPrimitiveClass() || field.getType().isPrimaryType();
61     }
62 
63     protected Object checkValue(Object value) {
64         if (!canBeNull() && value == null)
65             throw new NullPointerException(field + " cannot be set to null");
66 
67         if (value != null) {
68             Class<?> type = value.getClass();
69             if (type.isPrimitiveClass()) {
70                 type = type.asValueType();
71             }
72             if (!field.getType().isAssignableFrom(type)) {
73                 throwSetIllegalArgumentException(value);
74             }
75         }
76         return value;
77     }
78 
79 }
< prev index next >