001: /*
002: * Copyright 2001-2004 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:
031: class UnsafeFieldAccessorFactory {
032: static FieldAccessor newFieldAccessor(Field field, boolean override) {
033: Class type = field.getType();
034: boolean isStatic = Modifier.isStatic(field.getModifiers());
035: boolean isFinal = Modifier.isFinal(field.getModifiers());
036: boolean isVolatile = Modifier.isVolatile(field.getModifiers());
037: boolean isQualified = isFinal || isVolatile;
038: boolean isReadOnly = isFinal && (isStatic || !override);
039: if (isStatic) {
040: // This code path does not guarantee that the field's
041: // declaring class has been initialized, but it must be
042: // before performing reflective operations.
043: UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field
044: .getDeclaringClass());
045:
046: if (!isQualified) {
047: if (type == Boolean.TYPE) {
048: return new UnsafeStaticBooleanFieldAccessorImpl(
049: field);
050: } else if (type == Byte.TYPE) {
051: return new UnsafeStaticByteFieldAccessorImpl(field);
052: } else if (type == Short.TYPE) {
053: return new UnsafeStaticShortFieldAccessorImpl(field);
054: } else if (type == Character.TYPE) {
055: return new UnsafeStaticCharacterFieldAccessorImpl(
056: field);
057: } else if (type == Integer.TYPE) {
058: return new UnsafeStaticIntegerFieldAccessorImpl(
059: field);
060: } else if (type == Long.TYPE) {
061: return new UnsafeStaticLongFieldAccessorImpl(field);
062: } else if (type == Float.TYPE) {
063: return new UnsafeStaticFloatFieldAccessorImpl(field);
064: } else if (type == Double.TYPE) {
065: return new UnsafeStaticDoubleFieldAccessorImpl(
066: field);
067: } else {
068: return new UnsafeStaticObjectFieldAccessorImpl(
069: field);
070: }
071: } else {
072: if (type == Boolean.TYPE) {
073: return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(
074: field, isReadOnly);
075: } else if (type == Byte.TYPE) {
076: return new UnsafeQualifiedStaticByteFieldAccessorImpl(
077: field, isReadOnly);
078: } else if (type == Short.TYPE) {
079: return new UnsafeQualifiedStaticShortFieldAccessorImpl(
080: field, isReadOnly);
081: } else if (type == Character.TYPE) {
082: return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(
083: field, isReadOnly);
084: } else if (type == Integer.TYPE) {
085: return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(
086: field, isReadOnly);
087: } else if (type == Long.TYPE) {
088: return new UnsafeQualifiedStaticLongFieldAccessorImpl(
089: field, isReadOnly);
090: } else if (type == Float.TYPE) {
091: return new UnsafeQualifiedStaticFloatFieldAccessorImpl(
092: field, isReadOnly);
093: } else if (type == Double.TYPE) {
094: return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(
095: field, isReadOnly);
096: } else {
097: return new UnsafeQualifiedStaticObjectFieldAccessorImpl(
098: field, isReadOnly);
099: }
100: }
101: } else {
102: if (!isQualified) {
103: if (type == Boolean.TYPE) {
104: return new UnsafeBooleanFieldAccessorImpl(field);
105: } else if (type == Byte.TYPE) {
106: return new UnsafeByteFieldAccessorImpl(field);
107: } else if (type == Short.TYPE) {
108: return new UnsafeShortFieldAccessorImpl(field);
109: } else if (type == Character.TYPE) {
110: return new UnsafeCharacterFieldAccessorImpl(field);
111: } else if (type == Integer.TYPE) {
112: return new UnsafeIntegerFieldAccessorImpl(field);
113: } else if (type == Long.TYPE) {
114: return new UnsafeLongFieldAccessorImpl(field);
115: } else if (type == Float.TYPE) {
116: return new UnsafeFloatFieldAccessorImpl(field);
117: } else if (type == Double.TYPE) {
118: return new UnsafeDoubleFieldAccessorImpl(field);
119: } else {
120: return new UnsafeObjectFieldAccessorImpl(field);
121: }
122: } else {
123: if (type == Boolean.TYPE) {
124: return new UnsafeQualifiedBooleanFieldAccessorImpl(
125: field, isReadOnly);
126: } else if (type == Byte.TYPE) {
127: return new UnsafeQualifiedByteFieldAccessorImpl(
128: field, isReadOnly);
129: } else if (type == Short.TYPE) {
130: return new UnsafeQualifiedShortFieldAccessorImpl(
131: field, isReadOnly);
132: } else if (type == Character.TYPE) {
133: return new UnsafeQualifiedCharacterFieldAccessorImpl(
134: field, isReadOnly);
135: } else if (type == Integer.TYPE) {
136: return new UnsafeQualifiedIntegerFieldAccessorImpl(
137: field, isReadOnly);
138: } else if (type == Long.TYPE) {
139: return new UnsafeQualifiedLongFieldAccessorImpl(
140: field, isReadOnly);
141: } else if (type == Float.TYPE) {
142: return new UnsafeQualifiedFloatFieldAccessorImpl(
143: field, isReadOnly);
144: } else if (type == Double.TYPE) {
145: return new UnsafeQualifiedDoubleFieldAccessorImpl(
146: field, isReadOnly);
147: } else {
148: return new UnsafeQualifiedObjectFieldAccessorImpl(
149: field, isReadOnly);
150: }
151: }
152: }
153: }
154: }
|