1 /*
   2  * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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     }
 121 
 122     protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
 123         throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
 124     }
 125 
 126     protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
 127         throwFinalFieldIllegalAccessException("char", Character.toString(b));
 128     }
 129 
 130     protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
 131         throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
 132     }
 133 
 134     protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
 135         throwFinalFieldIllegalAccessException("short", Short.toString(b));
 136     }
 137 
 138     protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
 139         throwFinalFieldIllegalAccessException("int", Integer.toString(i));
 140     }
 141 
 142     protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
 143         throwFinalFieldIllegalAccessException("long", Long.toString(i));
 144     }
 145 
 146     protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
 147         throwFinalFieldIllegalAccessException("float", Float.toString(f));
 148     }
 149 
 150     protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
 151         throwFinalFieldIllegalAccessException("double", Double.toString(f));
 152     }
 153 
 154     protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
 155         return newGetIllegalArgumentException("boolean");
 156     }
 157 
 158     protected IllegalArgumentException newGetByteIllegalArgumentException() {
 159         return newGetIllegalArgumentException("byte");
 160     }
 161 
 162     protected IllegalArgumentException newGetCharIllegalArgumentException() {
 163         return newGetIllegalArgumentException("char");
 164     }
 165 
 166     protected IllegalArgumentException newGetShortIllegalArgumentException() {
 167         return newGetIllegalArgumentException("short");
 168     }
 169 
 170     protected IllegalArgumentException newGetIntIllegalArgumentException() {
 171         return newGetIllegalArgumentException("int");
 172     }
 173 
 174     protected IllegalArgumentException newGetLongIllegalArgumentException() {
 175         return newGetIllegalArgumentException("long");
 176     }
 177 
 178     protected IllegalArgumentException newGetFloatIllegalArgumentException() {
 179         return newGetIllegalArgumentException("float");
 180     }
 181 
 182     protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
 183         return newGetIllegalArgumentException("double");
 184     }
 185 
 186     protected String getSetMessage(String attemptedType, String attemptedValue) {
 187         String err = "Can not set";
 188         if (Modifier.isStatic(field.getModifiers()))
 189             err += " static";
 190         if (isFinal)
 191             err += " final";
 192         err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
 193         if (attemptedValue.length() > 0) {
 194             err += "(" + attemptedType + ")" + attemptedValue;
 195         } else {
 196             if (attemptedType.length() > 0)
 197                 err += attemptedType;
 198             else
 199                 err += "null value";
 200         }
 201         return err;
 202     }
 203 
 204     protected void throwSetIllegalArgumentException(String attemptedType,
 205                                                     String attemptedValue) {
 206         throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
 207     }
 208 
 209     protected void throwSetIllegalArgumentException(Object o) {
 210         throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
 211     }
 212 
 213     protected void throwSetIllegalArgumentException(boolean b) {
 214         throwSetIllegalArgumentException("boolean", Boolean.toString(b));
 215     }
 216 
 217     protected void throwSetIllegalArgumentException(byte b) {
 218         throwSetIllegalArgumentException("byte", Byte.toString(b));
 219     }
 220 
 221     protected void throwSetIllegalArgumentException(char c) {
 222         throwSetIllegalArgumentException("char", Character.toString(c));
 223     }
 224 
 225     protected void throwSetIllegalArgumentException(short s) {
 226         throwSetIllegalArgumentException("short", Short.toString(s));
 227     }
 228 
 229     protected void throwSetIllegalArgumentException(int i) {
 230         throwSetIllegalArgumentException("int", Integer.toString(i));
 231     }
 232 
 233     protected void throwSetIllegalArgumentException(long l) {
 234         throwSetIllegalArgumentException("long", Long.toString(l));
 235     }
 236 
 237     protected void throwSetIllegalArgumentException(float f) {
 238         throwSetIllegalArgumentException("float", Float.toString(f));
 239     }
 240 
 241     protected void throwSetIllegalArgumentException(double d) {
 242         throwSetIllegalArgumentException("double", Double.toString(d));
 243     }
 244 
 245 }