001: /*
002: * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.reflect;
027:
028: import java.lang.reflect.Field;
029: import java.lang.reflect.Modifier;
030: import sun.misc.Unsafe;
031:
032: /** Base class for sun.misc.Unsafe-based FieldAccessors. The
033: observation is that there are only nine types of fields from the
034: standpoint of reflection code: the eight primitive types and
035: Object. Using class Unsafe instead of generated bytecodes saves
036: memory and loading time for the dynamically-generated
037: FieldAccessors. */
038:
039: abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
040: static final Unsafe unsafe = Unsafe.getUnsafe();
041:
042: protected final Field field;
043: protected final int fieldOffset;
044: protected final boolean isFinal;
045:
046: UnsafeFieldAccessorImpl(Field field) {
047: this .field = field;
048: fieldOffset = unsafe.fieldOffset(field);
049: isFinal = Modifier.isFinal(field.getModifiers());
050: }
051:
052: protected void ensureObj(Object o) {
053: // NOTE: will throw NullPointerException, as specified, if o is null
054: if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
055: throwSetIllegalArgumentException(o);
056: }
057: }
058:
059: private String getQualifiedFieldName() {
060: return field.getDeclaringClass().getName() + "."
061: + field.getName();
062: }
063:
064: protected IllegalArgumentException newGetIllegalArgumentException(
065: String type) {
066: return new IllegalArgumentException("Attempt to get "
067: + field.getType().getName() + " field \""
068: + getQualifiedFieldName()
069: + "\" with illegal data type conversion to " + type);
070: }
071:
072: protected void throwFinalFieldIllegalAccessException(
073: String attemptedType, String attemptedValue)
074: throws IllegalAccessException {
075: throw new IllegalAccessException(getSetMessage(attemptedType,
076: attemptedValue));
077:
078: }
079:
080: protected void throwFinalFieldIllegalAccessException(Object o)
081: throws IllegalAccessException {
082: throwFinalFieldIllegalAccessException(o != null ? o.getClass()
083: .getName() : "", "");
084: }
085:
086: protected void throwFinalFieldIllegalAccessException(boolean z)
087: throws IllegalAccessException {
088: throwFinalFieldIllegalAccessException("boolean", Boolean
089: .toString(z));
090: }
091:
092: protected void throwFinalFieldIllegalAccessException(char b)
093: throws IllegalAccessException {
094: throwFinalFieldIllegalAccessException("char", Character
095: .toString(b));
096: }
097:
098: protected void throwFinalFieldIllegalAccessException(byte b)
099: throws IllegalAccessException {
100: throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
101: }
102:
103: protected void throwFinalFieldIllegalAccessException(short b)
104: throws IllegalAccessException {
105: throwFinalFieldIllegalAccessException("short", Short
106: .toString(b));
107: }
108:
109: protected void throwFinalFieldIllegalAccessException(int i)
110: throws IllegalAccessException {
111: throwFinalFieldIllegalAccessException("int", Integer
112: .toString(i));
113: }
114:
115: protected void throwFinalFieldIllegalAccessException(long i)
116: throws IllegalAccessException {
117: throwFinalFieldIllegalAccessException("long", Long.toString(i));
118: }
119:
120: protected void throwFinalFieldIllegalAccessException(float f)
121: throws IllegalAccessException {
122: throwFinalFieldIllegalAccessException("float", Float
123: .toString(f));
124: }
125:
126: protected void throwFinalFieldIllegalAccessException(double f)
127: throws IllegalAccessException {
128: throwFinalFieldIllegalAccessException("double", Double
129: .toString(f));
130: }
131:
132: protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
133: return newGetIllegalArgumentException("boolean");
134: }
135:
136: protected IllegalArgumentException newGetByteIllegalArgumentException() {
137: return newGetIllegalArgumentException("byte");
138: }
139:
140: protected IllegalArgumentException newGetCharIllegalArgumentException() {
141: return newGetIllegalArgumentException("char");
142: }
143:
144: protected IllegalArgumentException newGetShortIllegalArgumentException() {
145: return newGetIllegalArgumentException("short");
146: }
147:
148: protected IllegalArgumentException newGetIntIllegalArgumentException() {
149: return newGetIllegalArgumentException("int");
150: }
151:
152: protected IllegalArgumentException newGetLongIllegalArgumentException() {
153: return newGetIllegalArgumentException("long");
154: }
155:
156: protected IllegalArgumentException newGetFloatIllegalArgumentException() {
157: return newGetIllegalArgumentException("float");
158: }
159:
160: protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
161: return newGetIllegalArgumentException("double");
162: }
163:
164: protected String getSetMessage(String attemptedType,
165: String attemptedValue) {
166: String err = "Can not set";
167: if (Modifier.isStatic(field.getModifiers()))
168: err += " static";
169: if (isFinal)
170: err += " final";
171: err += " " + field.getType().getName() + " field "
172: + getQualifiedFieldName() + " to ";
173: if (attemptedValue.length() > 0) {
174: err += "(" + attemptedType + ")" + attemptedValue;
175: } else {
176: if (attemptedType.length() > 0)
177: err += attemptedType;
178: else
179: err += "null value";
180: }
181: return err;
182: }
183:
184: protected void throwSetIllegalArgumentException(
185: String attemptedType, String attemptedValue) {
186: throw new IllegalArgumentException(getSetMessage(attemptedType,
187: attemptedValue));
188: }
189:
190: protected void throwSetIllegalArgumentException(Object o) {
191: throwSetIllegalArgumentException(o != null ? o.getClass()
192: .getName() : "", "");
193: }
194:
195: protected void throwSetIllegalArgumentException(boolean b) {
196: throwSetIllegalArgumentException("boolean", Boolean.toString(b));
197: }
198:
199: protected void throwSetIllegalArgumentException(byte b) {
200: throwSetIllegalArgumentException("byte", Byte.toString(b));
201: }
202:
203: protected void throwSetIllegalArgumentException(char c) {
204: throwSetIllegalArgumentException("char", Character.toString(c));
205: }
206:
207: protected void throwSetIllegalArgumentException(short s) {
208: throwSetIllegalArgumentException("short", Short.toString(s));
209: }
210:
211: protected void throwSetIllegalArgumentException(int i) {
212: throwSetIllegalArgumentException("int", Integer.toString(i));
213: }
214:
215: protected void throwSetIllegalArgumentException(long l) {
216: throwSetIllegalArgumentException("long", Long.toString(l));
217: }
218:
219: protected void throwSetIllegalArgumentException(float f) {
220: throwSetIllegalArgumentException("float", Float.toString(f));
221: }
222:
223: protected void throwSetIllegalArgumentException(double d) {
224: throwSetIllegalArgumentException("double", Double.toString(d));
225: }
226:
227: }
|