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 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     private static final int FLAT_VALUE = 0x01;
  42     private static final int CAN_BE_NULL = 0x10;
  43 
  44     protected final Field   field;
  45     protected final long    fieldOffset;
  46     protected final boolean isFinal;
  47     protected final int     flags;
  48 
  49     UnsafeFieldAccessorImpl(Field field) {
  50         this.field = field;
  51         if (Modifier.isStatic(field.getModifiers()))
  52             this.fieldOffset = unsafe.staticFieldOffset(field);
  53         else
  54             this.fieldOffset = unsafe.objectFieldOffset(field);
  55         this.isFinal = Modifier.isFinal(field.getModifiers());
  56 
  57         int flags = 0;
  58         if (ReflectionFactory.langReflectAccess().isFlatValue(field))
  59             flags |= FLAT_VALUE;
  60         if (ReflectionFactory.langReflectAccess().canBeNull(field))
  61             flags |= CAN_BE_NULL;
  62         this.flags = flags;
  63     }
  64 
  65     protected void ensureObj(Object o) {
  66         // NOTE: will throw NullPointerException, as specified, if o is null
  67         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
  68             throwSetIllegalArgumentException(o);
  69         }
  70     }
  71 
  72     protected boolean isFlatValue() {
  73         return (flags & FLAT_VALUE) == FLAT_VALUE;
  74     }
  75 
  76     protected boolean canBeNull() {
  77         return (flags & CAN_BE_NULL) == CAN_BE_NULL;
  78     }
  79 
  80     protected Object checkValue(Object value) {
  81         if (!canBeNull() && value == null)
  82             throw new NullPointerException(field + " cannot be set to null");
  83 
  84         if (value != null) {
  85             if (!field.getType().isAssignableFrom(value.getClass())) {
  86                 throwSetIllegalArgumentException(value);
  87             }
  88         }
  89         return value;
  90     }
  91 
  92     private String getQualifiedFieldName() {
  93       return field.getDeclaringClass().getName() + "." +field.getName();
  94     }
  95 
  96     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
  97         return new IllegalArgumentException(
  98           "Attempt to get "+field.getType().getName()+" field \"" +
  99           getQualifiedFieldName() + "\" with illegal data type conversion to "+type
 100         );
 101     }
 102 
 103     protected void throwFinalFieldIllegalAccessException(String attemptedType,
 104                                                          String attemptedValue)
 105                                                          throws IllegalAccessException {
 106         throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
 107 
 108     }
 109     protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
 110         throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
 111     }
 112 
 113     protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
 114         throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
 115     }
 116 
 117     protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
 118         throwFinalFieldIllegalAccessException("char", Character.toString(b));
 119     }
 120 
 121     protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
 122         throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
 123     }
 124 
 125     protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
 126         throwFinalFieldIllegalAccessException("short", Short.toString(b));
 127     }
 128 
 129     protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
 130         throwFinalFieldIllegalAccessException("int", Integer.toString(i));
 131     }
 132 
 133     protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
 134         throwFinalFieldIllegalAccessException("long", Long.toString(i));
 135     }
 136 
 137     protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
 138         throwFinalFieldIllegalAccessException("float", Float.toString(f));
 139     }
 140 
 141     protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
 142         throwFinalFieldIllegalAccessException("double", Double.toString(f));
 143     }
 144 
 145     protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
 146         return newGetIllegalArgumentException("boolean");
 147     }
 148 
 149     protected IllegalArgumentException newGetByteIllegalArgumentException() {
 150         return newGetIllegalArgumentException("byte");
 151     }
 152 
 153     protected IllegalArgumentException newGetCharIllegalArgumentException() {
 154         return newGetIllegalArgumentException("char");
 155     }
 156 
 157     protected IllegalArgumentException newGetShortIllegalArgumentException() {
 158         return newGetIllegalArgumentException("short");
 159     }
 160 
 161     protected IllegalArgumentException newGetIntIllegalArgumentException() {
 162         return newGetIllegalArgumentException("int");
 163     }
 164 
 165     protected IllegalArgumentException newGetLongIllegalArgumentException() {
 166         return newGetIllegalArgumentException("long");
 167     }
 168 
 169     protected IllegalArgumentException newGetFloatIllegalArgumentException() {
 170         return newGetIllegalArgumentException("float");
 171     }
 172 
 173     protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
 174         return newGetIllegalArgumentException("double");
 175     }
 176 
 177     protected String getSetMessage(String attemptedType, String attemptedValue) {
 178         String err = "Can not set";
 179         if (Modifier.isStatic(field.getModifiers()))
 180             err += " static";
 181         if (isFinal)
 182             err += " final";
 183         err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
 184         if (attemptedValue.length() > 0) {
 185             err += "(" + attemptedType + ")" + attemptedValue;
 186         } else {
 187             if (attemptedType.length() > 0)
 188                 err += attemptedType;
 189             else
 190                 err += "null value";
 191         }
 192         return err;
 193     }
 194 
 195     protected void throwSetIllegalArgumentException(String attemptedType,
 196                                                     String attemptedValue) {
 197         throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
 198     }
 199 
 200     protected void throwSetIllegalArgumentException(Object o) {
 201         throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
 202     }
 203 
 204     protected void throwSetIllegalArgumentException(boolean b) {
 205         throwSetIllegalArgumentException("boolean", Boolean.toString(b));
 206     }
 207 
 208     protected void throwSetIllegalArgumentException(byte b) {
 209         throwSetIllegalArgumentException("byte", Byte.toString(b));
 210     }
 211 
 212     protected void throwSetIllegalArgumentException(char c) {
 213         throwSetIllegalArgumentException("char", Character.toString(c));
 214     }
 215 
 216     protected void throwSetIllegalArgumentException(short s) {
 217         throwSetIllegalArgumentException("short", Short.toString(s));
 218     }
 219 
 220     protected void throwSetIllegalArgumentException(int i) {
 221         throwSetIllegalArgumentException("int", Integer.toString(i));
 222     }
 223 
 224     protected void throwSetIllegalArgumentException(long l) {
 225         throwSetIllegalArgumentException("long", Long.toString(l));
 226     }
 227 
 228     protected void throwSetIllegalArgumentException(float f) {
 229         throwSetIllegalArgumentException("float", Float.toString(f));
 230     }
 231 
 232     protected void throwSetIllegalArgumentException(double d) {
 233         throwSetIllegalArgumentException("double", Double.toString(d));
 234     }
 235 
 236 }