< prev index next >

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

Print this page




  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 jdk.internal.reflect;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Modifier;
  30 import jdk.internal.misc.Unsafe;
  31 
  32 /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
  33     observation is that there are only nine types of fields from the
  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     private static final int FLAT_VALUE = 0x01;
  42     private static final int CAN_BE_NULL = 0x10;
  43 
  44     protected final Field   field;
  45     protected final long    fieldOffset;
  46     protected final boolean isFinal;
  47     protected final int     flags;
  48 
  49     UnsafeFieldAccessorImpl(Field field) {
  50         this.field = field;
  51         if (Modifier.isStatic(field.getModifiers()))
  52             this.fieldOffset = unsafe.staticFieldOffset(field);
  53         else
  54             this.fieldOffset = unsafe.objectFieldOffset(field);
  55         this.isFinal = Modifier.isFinal(field.getModifiers());
  56 
  57         int flags = 0;
  58         if (ReflectionFactory.langReflectAccess().isFlatValue(field))
  59             flags |= FLAT_VALUE;
  60         if (ReflectionFactory.langReflectAccess().canBeNull(field))
  61             flags |= CAN_BE_NULL;
  62         this.flags = flags;
  63     }
  64 
  65     protected void ensureObj(Object o) {
  66         // NOTE: will throw NullPointerException, as specified, if o is null
  67         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
  68             throwSetIllegalArgumentException(o);
  69         }
  70     }
  71 
  72     protected boolean isFlatValue() {
  73         return (flags & FLAT_VALUE) == FLAT_VALUE;
  74     }
  75 
  76     protected boolean canBeNull() {
  77         return (flags & CAN_BE_NULL) == CAN_BE_NULL;
  78     }
  79 
  80     protected Object checkValue(Object value) {
  81         if (!canBeNull() && value == null)
  82             throw new NullPointerException(field + " cannot be set to null");
  83 
  84         if (value != null) {
  85             if (!field.getType().isAssignableFrom(value.getClass())) {
  86                 throwSetIllegalArgumentException(value);
  87             }
  88         }
  89         return value;
  90     }
  91 
  92     private String getQualifiedFieldName() {
  93       return field.getDeclaringClass().getName() + "." +field.getName();
  94     }
  95 
  96     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
  97         return new IllegalArgumentException(




  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 jdk.internal.reflect;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Modifier;
  30 import jdk.internal.misc.Unsafe;
  31 
  32 /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
  33     observation is that there are only nine types of fields from the
  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 Field   field;
  43     protected final long    fieldOffset;
  44     protected final boolean isFinal;

  45 
  46     UnsafeFieldAccessorImpl(Field field) {
  47         this.field = field;
  48         if (Modifier.isStatic(field.getModifiers()))
  49             this.fieldOffset = unsafe.staticFieldOffset(field);
  50         else
  51             this.fieldOffset = unsafe.objectFieldOffset(field);
  52         this.isFinal = Modifier.isFinal(field.getModifiers());







  53     }
  54 
  55     protected void ensureObj(Object o) {
  56         // NOTE: will throw NullPointerException, as specified, if o is null
  57         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
  58             throwSetIllegalArgumentException(o);
  59         }
  60     }
  61 
  62     protected boolean isFlattened() {
  63         return unsafe.isFlattened(field);
  64     }
  65 
  66     protected boolean canBeNull() {
  67         return field.getType() == field.getType().asBoxType();
  68     }
  69 
  70     protected Object checkValue(Object value) {
  71         if (!canBeNull() && value == null)
  72             throw new NullPointerException(field + " cannot be set to null");
  73 
  74         if (value != null) {
  75             if (!field.getType().isAssignableFrom(value.getClass())) {
  76                 throwSetIllegalArgumentException(value);
  77             }
  78         }
  79         return value;
  80     }
  81 
  82     private String getQualifiedFieldName() {
  83       return field.getDeclaringClass().getName() + "." +field.getName();
  84     }
  85 
  86     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
  87         return new IllegalArgumentException(


< prev index next >