< prev index next >

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

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

@@ -25,10 +25,12 @@
  
  package jdk.internal.reflect;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Modifier;
+ 
+ import jdk.internal.value.PrimitiveClass;
  import jdk.internal.misc.Unsafe;
  
  /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
      observation is that there are only nine types of fields from the
      standpoint of reflection code: the eight primitive types and

@@ -49,6 +51,32 @@
          if (Modifier.isStatic(mods))
              fieldOffset = unsafe.staticFieldOffset(field);
          else
              fieldOffset = unsafe.objectFieldOffset(field);
      }
+ 
+     protected boolean isFlattened() {
+         return unsafe.isFlattened(field);
+     }
+ 
+     protected boolean canBeNull() {
+         return !PrimitiveClass.isPrimitiveClass(field.getType()) ||
+                 PrimitiveClass.isPrimaryType(field.getType());
+     }
+ 
+     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 (PrimitiveClass.isPrimitiveClass(type)) {
+                 type = PrimitiveClass.asValueType(type);
+             }
+             if (!field.getType().isInstance(value)) {
+                 throwSetIllegalArgumentException(value);
+             }
+         }
+         return value;
+     }
+ 
  }
< prev index next >