< prev index next >

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

Print this page
*** 45,23 ***
      protected final boolean isFinal;
  
      UnsafeFieldAccessorImpl(Field field) {
          super(field);
          if (Modifier.isStatic(field.getModifiers()))
!             fieldOffset = unsafe.staticFieldOffset(field);
          else
!             fieldOffset = unsafe.objectFieldOffset(field);
!         isFinal = Modifier.isFinal(field.getModifiers());
      }
  
      protected void ensureObj(Object o) {
          // NOTE: will throw NullPointerException, as specified, if o is null
          if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
              throwSetIllegalArgumentException(o);
          }
      }
  
      private String getQualifiedFieldName() {
        return field.getDeclaringClass().getName() + "." +field.getName();
      }
  
      protected IllegalArgumentException newGetIllegalArgumentException(String type) {
--- 45,47 ---
      protected final boolean isFinal;
  
      UnsafeFieldAccessorImpl(Field field) {
          super(field);
          if (Modifier.isStatic(field.getModifiers()))
!             this.fieldOffset = unsafe.staticFieldOffset(field);
          else
!             this.fieldOffset = unsafe.objectFieldOffset(field);
!         this.isFinal = Modifier.isFinal(field.getModifiers());
      }
  
      protected void ensureObj(Object o) {
          // NOTE: will throw NullPointerException, as specified, if o is null
          if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
              throwSetIllegalArgumentException(o);
          }
      }
  
+     protected boolean isFlattened() {
+         return unsafe.isFlattened(field);
+     }
+ 
+     protected boolean canBeNull() {
+         return !field.getType().isPrimitiveClass() || field.getType().isPrimaryType();
+     }
+ 
+     protected Object checkValue(Object value) {
+         if (!canBeNull() && value == null)
+             throw new NullPointerException(field + " cannot be set to null");
+ 
+         if (value != null) {
+             Class<?> type = value.getClass();
+             if (type.isPrimitiveClass()) {
+                 type = type.asValueType();
+             }
+             if (!field.getType().isAssignableFrom(type)) {
+                 throwSetIllegalArgumentException(value);
+             }
+         }
+         return value;
+     }
+ 
      private String getQualifiedFieldName() {
        return field.getDeclaringClass().getName() + "." +field.getName();
      }
  
      protected IllegalArgumentException newGetIllegalArgumentException(String type) {
< prev index next >