< prev index next >

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

Print this page

 30 import jdk.internal.misc.Unsafe;
 31 import jdk.internal.vm.annotation.Stable;
 32 
 33 /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
 34     observation is that there are only nine types of fields from the
 35     standpoint of reflection code: the eight primitive types and
 36     Object. Using class Unsafe instead of generated bytecodes saves
 37     memory and loading time for the dynamically-generated
 38     FieldAccessors. */
 39 
 40 abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
 41     static final Unsafe unsafe = Unsafe.getUnsafe();
 42 
 43     @Stable
 44     protected final long    fieldOffset;
 45     protected final boolean isFinal;
 46 
 47     UnsafeFieldAccessorImpl(Field field) {
 48         super(field);
 49         if (Modifier.isStatic(field.getModifiers()))
 50             fieldOffset = unsafe.staticFieldOffset(field);
 51         else
 52             fieldOffset = unsafe.objectFieldOffset(field);
 53         isFinal = Modifier.isFinal(field.getModifiers());
 54     }
 55 
 56     protected void ensureObj(Object o) {
 57         // NOTE: will throw NullPointerException, as specified, if o is null
 58         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
 59             throwSetIllegalArgumentException(o);
 60         }
 61     }
 62 
























 63     private String getQualifiedFieldName() {
 64       return field.getDeclaringClass().getName() + "." +field.getName();
 65     }
 66 
 67     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
 68         return new IllegalArgumentException(
 69           "Attempt to get "+field.getType().getName()+" field \"" +
 70           getQualifiedFieldName() + "\" with illegal data type conversion to "+type
 71         );
 72     }
 73 
 74     protected void throwFinalFieldIllegalAccessException(String attemptedType,
 75                                                          String attemptedValue)
 76                                                          throws IllegalAccessException {
 77         throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
 78 
 79     }
 80     protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
 81         throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
 82     }

 30 import jdk.internal.misc.Unsafe;
 31 import jdk.internal.vm.annotation.Stable;
 32 
 33 /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
 34     observation is that there are only nine types of fields from the
 35     standpoint of reflection code: the eight primitive types and
 36     Object. Using class Unsafe instead of generated bytecodes saves
 37     memory and loading time for the dynamically-generated
 38     FieldAccessors. */
 39 
 40 abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
 41     static final Unsafe unsafe = Unsafe.getUnsafe();
 42 
 43     @Stable
 44     protected final long    fieldOffset;
 45     protected final boolean isFinal;
 46 
 47     UnsafeFieldAccessorImpl(Field field) {
 48         super(field);
 49         if (Modifier.isStatic(field.getModifiers()))
 50             this.fieldOffset = unsafe.staticFieldOffset(field);
 51         else
 52             this.fieldOffset = unsafe.objectFieldOffset(field);
 53         this.isFinal = Modifier.isFinal(field.getModifiers());
 54     }
 55 
 56     protected void ensureObj(Object o) {
 57         // NOTE: will throw NullPointerException, as specified, if o is null
 58         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
 59             throwSetIllegalArgumentException(o);
 60         }
 61     }
 62 
 63     protected boolean isFlattened() {
 64         return unsafe.isFlattened(field);
 65     }
 66 
 67     protected boolean canBeNull() {
 68         return !field.getType().isPrimitiveClass() || field.getType().isPrimaryType();
 69     }
 70 
 71     protected Object checkValue(Object value) {
 72         if (!canBeNull() && value == null)
 73             throw new NullPointerException(field + " cannot be set to null");
 74 
 75         if (value != null) {
 76             Class<?> type = value.getClass();
 77             if (type.isPrimitiveClass()) {
 78                 type = type.asValueType();
 79             }
 80             if (!field.getType().isAssignableFrom(type)) {
 81                 throwSetIllegalArgumentException(value);
 82             }
 83         }
 84         return value;
 85     }
 86 
 87     private String getQualifiedFieldName() {
 88       return field.getDeclaringClass().getName() + "." +field.getName();
 89     }
 90 
 91     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
 92         return new IllegalArgumentException(
 93           "Attempt to get "+field.getType().getName()+" field \"" +
 94           getQualifiedFieldName() + "\" with illegal data type conversion to "+type
 95         );
 96     }
 97 
 98     protected void throwFinalFieldIllegalAccessException(String attemptedType,
 99                                                          String attemptedValue)
100                                                          throws IllegalAccessException {
101         throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
102 
103     }
104     protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
105         throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
106     }
< prev index next >