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