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