10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 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 protected final boolean isFlattened; 46 47 UnsafeFieldAccessorImpl(Field field) { 48 this.field = 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 isFlattened = ReflectionFactory.langReflectAccess().isFlattened(field); 55 } 56 57 protected void ensureObj(Object o) { 58 // NOTE: will throw NullPointerException, as specified, if o is null 59 if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) { 60 throwSetIllegalArgumentException(o); 61 } 62 } 63 64 private String getQualifiedFieldName() { 65 return field.getDeclaringClass().getName() + "." +field.getName(); 66 } 67 68 protected IllegalArgumentException newGetIllegalArgumentException(String type) { 69 return new IllegalArgumentException( 70 "Attempt to get "+field.getType().getName()+" field \"" + 71 getQualifiedFieldName() + "\" with illegal data type conversion to "+type 72 ); 73 } 74 75 protected void throwFinalFieldIllegalAccessException(String attemptedType, 76 String attemptedValue) 77 throws IllegalAccessException { 78 throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue)); 79 80 } 81 protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException { 82 throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", ""); 83 } | 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 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 java.util.Objects; 31 32 import jdk.internal.misc.Unsafe; 33 34 /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The 35 observation is that there are only nine types of fields from the 36 standpoint of reflection code: the eight primitive types and 37 Object. Using class Unsafe instead of generated bytecodes saves 38 memory and loading time for the dynamically-generated 39 FieldAccessors. */ 40 41 abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl { 42 static final Unsafe unsafe = Unsafe.getUnsafe(); 43 private static final int FLATTENABLE = 0x01; 44 private static final int FLATTENED = 0x10; 45 private static final int NULLABLE = 0x100; 46 47 protected final Field field; 48 protected final long fieldOffset; 49 protected final boolean isFinal; 50 protected final int flags; 51 52 UnsafeFieldAccessorImpl(Field field) { 53 this.field = field; 54 if (Modifier.isStatic(field.getModifiers())) 55 this.fieldOffset = unsafe.staticFieldOffset(field); 56 else 57 this.fieldOffset = unsafe.objectFieldOffset(field); 58 this.isFinal = Modifier.isFinal(field.getModifiers()); 59 60 int flags = 0; 61 if (ReflectionFactory.langReflectAccess().isFlattenable(field)) 62 flags |= FLATTENABLE; 63 if (ReflectionFactory.langReflectAccess().isFlattened(field)) 64 flags |= FLATTENED; 65 if (ReflectionFactory.langReflectAccess().isNullable(field)) 66 flags |= NULLABLE; 67 this.flags = flags; 68 } 69 70 protected void ensureObj(Object o) { 71 // NOTE: will throw NullPointerException, as specified, if o is null 72 if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) { 73 throwSetIllegalArgumentException(o); 74 } 75 } 76 77 protected boolean isFlattenable() { 78 return (flags & FLATTENABLE) == FLATTENABLE; 79 } 80 81 protected boolean isFlattened() { 82 return (flags & FLATTENED) == FLATTENED; 83 } 84 85 protected boolean isNullable() { 86 return (flags & NULLABLE) == NULLABLE; 87 } 88 89 protected Object checkValue(Object value) { 90 if (!isNullable() && value == null) 91 throw new NullPointerException(field + " cannot be set to null"); 92 93 if (value != null) { 94 if (!field.getType().isAssignableFrom(value.getClass())) { 95 throwSetIllegalArgumentException(value); 96 } 97 } 98 return value; 99 } 100 101 private String getQualifiedFieldName() { 102 return field.getDeclaringClass().getName() + "." +field.getName(); 103 } 104 105 protected IllegalArgumentException newGetIllegalArgumentException(String type) { 106 return new IllegalArgumentException( 107 "Attempt to get "+field.getType().getName()+" field \"" + 108 getQualifiedFieldName() + "\" with illegal data type conversion to "+type 109 ); 110 } 111 112 protected void throwFinalFieldIllegalAccessException(String attemptedType, 113 String attemptedValue) 114 throws IllegalAccessException { 115 throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue)); 116 117 } 118 protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException { 119 throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", ""); 120 } |