< prev index next > src/java.base/share/classes/jdk/internal/reflect/UnsafeFieldAccessorImpl.java
Print this page
/*
- * 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
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
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 >