1 | /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
---|
2 |
|
---|
3 | This file is part of libgcj.
|
---|
4 |
|
---|
5 | This software is copyrighted work licensed under the terms of the
|
---|
6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
7 | details. */
|
---|
8 |
|
---|
9 | package java.lang.reflect;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @author Per Bothner <bothner@cygnus.com>
|
---|
13 | * @date September 1998; February 1999.
|
---|
14 | */
|
---|
15 | /* Status: Mostly implemented.
|
---|
16 | * However, access checks are not implemented. See natField.cc for
|
---|
17 | * _Jv_CheckFieldAccessibility as well as the missing getCaller.
|
---|
18 | * Note that the idea is to have to compiler convert calls to
|
---|
19 | * setXXX(...) and getXXX(...) to setXXX(CALLER, ...) and getXXX(CALLER, ...),
|
---|
20 | * where CALLER is reference to the class that contains the calls to
|
---|
21 | * setXXX or getXXX. This is easy for the compiler, and replaces
|
---|
22 | * expensive stack and table searching with a constant.
|
---|
23 | */
|
---|
24 |
|
---|
25 | public final class Field extends AccessibleObject implements Member
|
---|
26 | {
|
---|
27 | private Class declaringClass;
|
---|
28 |
|
---|
29 | // This is filled in by getName.
|
---|
30 | private String name;
|
---|
31 |
|
---|
32 | // Offset in bytes from the start of declaringClass's fields array.
|
---|
33 | private int offset;
|
---|
34 |
|
---|
35 | // This is instantiated by Class sometimes, but it uses C++ and
|
---|
36 | // avoids the Java protection check.
|
---|
37 | Field ()
|
---|
38 | {
|
---|
39 | }
|
---|
40 |
|
---|
41 | public boolean equals (Object fld)
|
---|
42 | {
|
---|
43 | if (! (fld instanceof Field))
|
---|
44 | return false;
|
---|
45 | Field f = (Field) fld;
|
---|
46 | return declaringClass == f.declaringClass && offset == f.offset;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public Class getDeclaringClass ()
|
---|
50 | {
|
---|
51 | return declaringClass;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public native String getName ();
|
---|
55 |
|
---|
56 | public native Class getType ();
|
---|
57 |
|
---|
58 | public native int getModifiers ();
|
---|
59 |
|
---|
60 | public int hashCode()
|
---|
61 | {
|
---|
62 | return (declaringClass.hashCode() ^ offset);
|
---|
63 | }
|
---|
64 |
|
---|
65 | // The idea is that the compiler will magically translate
|
---|
66 | // fld.getShort(obj) to fld.getShort(THISCLASS, obj).
|
---|
67 | // This makes checking assessiblity more efficient,
|
---|
68 | // since we don't have to do any stack-walking.
|
---|
69 |
|
---|
70 | public boolean getBoolean (Object obj)
|
---|
71 | throws IllegalArgumentException, IllegalAccessException
|
---|
72 | {
|
---|
73 | return getBoolean(null, obj);
|
---|
74 | }
|
---|
75 | public char getChar (Object obj)
|
---|
76 | throws IllegalArgumentException, IllegalAccessException
|
---|
77 | {
|
---|
78 | return getChar(null, obj);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public byte getByte (Object obj)
|
---|
82 | throws IllegalArgumentException, IllegalAccessException
|
---|
83 | {
|
---|
84 | return getByte(null, obj);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public short getShort (Object obj)
|
---|
88 | throws IllegalArgumentException, IllegalAccessException
|
---|
89 | {
|
---|
90 | return getShort(null, obj);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public int getInt (Object obj)
|
---|
94 | throws IllegalArgumentException, IllegalAccessException
|
---|
95 | {
|
---|
96 | return getInt(null, obj);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public long getLong (Object obj)
|
---|
100 | throws IllegalArgumentException, IllegalAccessException
|
---|
101 | {
|
---|
102 | return getLong(null, obj);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public float getFloat (Object obj)
|
---|
106 | throws IllegalArgumentException, IllegalAccessException
|
---|
107 | {
|
---|
108 | return getFloat(null, obj);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public double getDouble (Object obj)
|
---|
112 | throws IllegalArgumentException, IllegalAccessException
|
---|
113 | {
|
---|
114 | return getDouble(null, obj);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public Object get (Object obj)
|
---|
118 | throws IllegalArgumentException, IllegalAccessException
|
---|
119 | {
|
---|
120 | return get(null, obj);
|
---|
121 | }
|
---|
122 |
|
---|
123 | private native boolean getBoolean (Class caller, Object obj)
|
---|
124 | throws IllegalArgumentException, IllegalAccessException;
|
---|
125 |
|
---|
126 | private native char getChar (Class caller, Object obj)
|
---|
127 | throws IllegalArgumentException, IllegalAccessException;
|
---|
128 |
|
---|
129 | private native byte getByte (Class caller, Object obj)
|
---|
130 | throws IllegalArgumentException, IllegalAccessException;
|
---|
131 |
|
---|
132 | private native short getShort (Class caller, Object obj)
|
---|
133 | throws IllegalArgumentException, IllegalAccessException;
|
---|
134 |
|
---|
135 | private native int getInt (Class caller, Object obj)
|
---|
136 | throws IllegalArgumentException, IllegalAccessException;
|
---|
137 |
|
---|
138 | private native long getLong (Class caller, Object obj)
|
---|
139 | throws IllegalArgumentException, IllegalAccessException;
|
---|
140 |
|
---|
141 | private native float getFloat (Class caller, Object obj)
|
---|
142 | throws IllegalArgumentException, IllegalAccessException;
|
---|
143 |
|
---|
144 | private native double getDouble (Class caller, Object obj)
|
---|
145 | throws IllegalArgumentException, IllegalAccessException;
|
---|
146 |
|
---|
147 | private native Object get (Class caller, Object obj)
|
---|
148 | throws IllegalArgumentException, IllegalAccessException;
|
---|
149 |
|
---|
150 | public void setByte (Object obj, byte b)
|
---|
151 | throws IllegalArgumentException, IllegalAccessException
|
---|
152 | {
|
---|
153 | setByte(null, obj, b);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void setShort (Object obj, short s)
|
---|
157 | throws IllegalArgumentException, IllegalAccessException
|
---|
158 | {
|
---|
159 | setShort(null, obj, s);
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void setInt (Object obj, int i)
|
---|
163 | throws IllegalArgumentException, IllegalAccessException
|
---|
164 | {
|
---|
165 | setInt(null, obj, i);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void setLong (Object obj, long l)
|
---|
169 | throws IllegalArgumentException, IllegalAccessException
|
---|
170 | {
|
---|
171 | setLong(null, obj, l);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public void setFloat (Object obj, float f)
|
---|
175 | throws IllegalArgumentException, IllegalAccessException
|
---|
176 | {
|
---|
177 | setFloat(null, obj, f);
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void setDouble (Object obj, double d)
|
---|
181 | throws IllegalArgumentException, IllegalAccessException
|
---|
182 | {
|
---|
183 | setDouble(null, obj, d);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void setChar (Object obj, char c)
|
---|
187 | throws IllegalArgumentException, IllegalAccessException
|
---|
188 | {
|
---|
189 | setChar(null, obj, c);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public void setBoolean (Object obj, boolean b)
|
---|
193 | throws IllegalArgumentException, IllegalAccessException
|
---|
194 | {
|
---|
195 | setBoolean(null, obj, b);
|
---|
196 | }
|
---|
197 |
|
---|
198 | private native void setByte (Class caller, Object obj, byte b)
|
---|
199 | throws IllegalArgumentException, IllegalAccessException;
|
---|
200 |
|
---|
201 | private native void setShort (Class caller, Object obj, short s)
|
---|
202 | throws IllegalArgumentException, IllegalAccessException;
|
---|
203 |
|
---|
204 | private native void setInt (Class caller, Object obj, int i)
|
---|
205 | throws IllegalArgumentException, IllegalAccessException;
|
---|
206 |
|
---|
207 | private native void setLong (Class caller, Object obj, long l)
|
---|
208 | throws IllegalArgumentException, IllegalAccessException;
|
---|
209 |
|
---|
210 | private native void setFloat (Class caller, Object obj, float f)
|
---|
211 | throws IllegalArgumentException, IllegalAccessException;
|
---|
212 |
|
---|
213 | private native void setDouble (Class caller, Object obj, double d)
|
---|
214 | throws IllegalArgumentException, IllegalAccessException;
|
---|
215 |
|
---|
216 | private native void setChar (Class caller, Object obj, char c)
|
---|
217 | throws IllegalArgumentException, IllegalAccessException;
|
---|
218 |
|
---|
219 | private native void setBoolean (Class caller, Object obj, boolean b)
|
---|
220 | throws IllegalArgumentException, IllegalAccessException;
|
---|
221 |
|
---|
222 | private native void set (Class caller, Object obj, Object val, Class type)
|
---|
223 | throws IllegalArgumentException, IllegalAccessException;
|
---|
224 |
|
---|
225 | public void set (Object object, Object value)
|
---|
226 | throws IllegalArgumentException, IllegalAccessException
|
---|
227 | {
|
---|
228 | set(null, object, value);
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void set (Class caller, Object object, Object value)
|
---|
232 | throws IllegalArgumentException, IllegalAccessException
|
---|
233 | {
|
---|
234 | Class type = getType();
|
---|
235 | if (! type.isPrimitive())
|
---|
236 | set(caller, object, value, type);
|
---|
237 | else if (value instanceof Byte)
|
---|
238 | setByte(caller, object, ((Byte) value).byteValue());
|
---|
239 | else if (value instanceof Short)
|
---|
240 | setShort (caller, object, ((Short) value).shortValue());
|
---|
241 | else if (value instanceof Integer)
|
---|
242 | setInt(caller, object, ((Integer) value).intValue());
|
---|
243 | else if (value instanceof Long)
|
---|
244 | setLong(caller, object, ((Long) value).longValue());
|
---|
245 | else if (value instanceof Float)
|
---|
246 | setFloat(caller, object, ((Float) value).floatValue());
|
---|
247 | else if (value instanceof Double)
|
---|
248 | setDouble(caller, object, ((Double) value).doubleValue());
|
---|
249 | else if (value instanceof Character)
|
---|
250 | setChar(caller, object, ((Character) value).charValue());
|
---|
251 | else if (value instanceof Boolean)
|
---|
252 | setBoolean(caller, object, ((Boolean) value).booleanValue());
|
---|
253 | else
|
---|
254 | throw new IllegalArgumentException();
|
---|
255 | }
|
---|
256 |
|
---|
257 | public String toString ()
|
---|
258 | {
|
---|
259 | StringBuffer sbuf = new StringBuffer ();
|
---|
260 | int mods = getModifiers();
|
---|
261 | if (mods != 0)
|
---|
262 | {
|
---|
263 | Modifier.toString(mods, sbuf);
|
---|
264 | sbuf.append(' ');
|
---|
265 | }
|
---|
266 | Method.appendClassName (sbuf, getType ());
|
---|
267 | sbuf.append(' ');
|
---|
268 | Method.appendClassName (sbuf, getDeclaringClass());
|
---|
269 | sbuf.append('.');
|
---|
270 | sbuf.append(getName());
|
---|
271 | return sbuf.toString();
|
---|
272 | }
|
---|
273 | }
|
---|