Initial revision
[gcc.git] / libjava / java / lang / reflect / Field.java
1 /* Copyright (C) 1998, 1999 Cygnus Solutions
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 public boolean equals (Object fld)
36 {
37 if (! (fld instanceof Field))
38 return false;
39 Field f = (Field) fld;
40 return declaringClass == f.declaringClass && offset == f.offset;
41 }
42
43 public Class getDeclaringClass ()
44 {
45 return declaringClass;
46 }
47
48 public native String getName ();
49
50 public native Class getType ();
51
52 public native int getModifiers ();
53
54 public int hashCode()
55 {
56 return (declaringClass.hashCode() ^ offset);
57 }
58
59 // The idea is that the compiler will magically translate
60 // fld.getShort(obj) to fld.getShort(THISCLASS, obj).
61 // This makes checking assessiblity more efficient,
62 // since we don't have to do any stack-walking.
63
64 public boolean getBoolean (Object obj)
65 throws IllegalArgumentException, IllegalAccessException
66 {
67 return getBoolean(null, obj);
68 }
69 public char getChar (Object obj)
70 throws IllegalArgumentException, IllegalAccessException
71 {
72 return getChar(null, obj);
73 }
74
75 public byte getByte (Object obj)
76 throws IllegalArgumentException, IllegalAccessException
77 {
78 return getByte(null, obj);
79 }
80
81 public short getShort (Object obj)
82 throws IllegalArgumentException, IllegalAccessException
83 {
84 return getShort(null, obj);
85 }
86
87 public int getInt (Object obj)
88 throws IllegalArgumentException, IllegalAccessException
89 {
90 return getInt(null, obj);
91 }
92
93 public long getLong (Object obj)
94 throws IllegalArgumentException, IllegalAccessException
95 {
96 return getLong(null, obj);
97 }
98
99 public float getFloat (Object obj)
100 throws IllegalArgumentException, IllegalAccessException
101 {
102 return getFloat(null, obj);
103 }
104
105 public double getDouble (Object obj)
106 throws IllegalArgumentException, IllegalAccessException
107 {
108 return getDouble(null, obj);
109 }
110
111 public Object get (Object obj)
112 throws IllegalArgumentException, IllegalAccessException
113 {
114 return get(null, obj);
115 }
116
117 private native boolean getBoolean (Class caller, Object obj)
118 throws IllegalArgumentException, IllegalAccessException;
119
120 private native char getChar (Class caller, Object obj)
121 throws IllegalArgumentException, IllegalAccessException;
122
123 private native byte getByte (Class caller, Object obj)
124 throws IllegalArgumentException, IllegalAccessException;
125
126 private native short getShort (Class caller, Object obj)
127 throws IllegalArgumentException, IllegalAccessException;
128
129 private native int getInt (Class caller, Object obj)
130 throws IllegalArgumentException, IllegalAccessException;
131
132 private native long getLong (Class caller, Object obj)
133 throws IllegalArgumentException, IllegalAccessException;
134
135 private native float getFloat (Class caller, Object obj)
136 throws IllegalArgumentException, IllegalAccessException;
137
138 private native double getDouble (Class caller, Object obj)
139 throws IllegalArgumentException, IllegalAccessException;
140
141 public native Object get (Class caller, Object obj)
142 throws IllegalArgumentException, IllegalAccessException;
143
144 public void setByte (Object obj, byte b)
145 throws IllegalArgumentException, IllegalAccessException
146 {
147 setByte(null, obj, b);
148 }
149
150 public void setShort (Object obj, short s)
151 throws IllegalArgumentException, IllegalAccessException
152 {
153 setShort(null, obj, s);
154 }
155
156 public void setInt (Object obj, int i)
157 throws IllegalArgumentException, IllegalAccessException
158 {
159 setInt(null, obj, i);
160 }
161
162 public void setLong (Object obj, long l)
163 throws IllegalArgumentException, IllegalAccessException
164 {
165 setLong(null, obj, l);
166 }
167
168 public void setFloat (Object obj, float f)
169 throws IllegalArgumentException, IllegalAccessException
170 {
171 setFloat(null, obj, f);
172 }
173
174 public void setDouble (Object obj, double d)
175 throws IllegalArgumentException, IllegalAccessException
176 {
177 setDouble(null, obj, d);
178 }
179
180 public void setChar (Object obj, char c)
181 throws IllegalArgumentException, IllegalAccessException
182 {
183 setChar(null, obj, c);
184 }
185
186 public void setBoolean (Object obj, boolean b)
187 throws IllegalArgumentException, IllegalAccessException
188 {
189 setBoolean(null, obj, b);
190 }
191
192 public native void setByte (Class caller, Object obj, byte b)
193 throws IllegalArgumentException, IllegalAccessException;
194
195 public native void setShort (Class caller, Object obj, short s)
196 throws IllegalArgumentException, IllegalAccessException;
197
198 public native void setInt (Class caller, Object obj, int i)
199 throws IllegalArgumentException, IllegalAccessException;
200
201 public native void setLong (Class caller, Object obj, long l)
202 throws IllegalArgumentException, IllegalAccessException;
203
204 public native void setFloat (Class caller, Object obj, float f)
205 throws IllegalArgumentException, IllegalAccessException;
206
207 public native void setDouble (Class caller, Object obj, double d)
208 throws IllegalArgumentException, IllegalAccessException;
209
210 public native void setChar (Class caller, Object obj, char c)
211 throws IllegalArgumentException, IllegalAccessException;
212
213 public native void setBoolean (Class caller, Object obj, boolean b)
214 throws IllegalArgumentException, IllegalAccessException;
215
216 private native void set (Class caller, Object obj, Object val, Class type)
217 throws IllegalArgumentException, IllegalAccessException;
218
219 public void set (Object object, Object value)
220 throws IllegalArgumentException, IllegalAccessException
221 {
222 set(null, object, value);
223 }
224
225 public void set (Class caller, Object object, Object value)
226 throws IllegalArgumentException, IllegalAccessException
227 {
228 Class type = getType();
229 if (! type.isPrimitive())
230 set(caller, object, value, type);
231 else if (value instanceof Byte)
232 setByte(caller, object, ((Byte) value).byteValue());
233 else if (value instanceof Short)
234 setShort (caller, object, ((Short) value).shortValue());
235 else if (value instanceof Integer)
236 setInt(caller, object, ((Integer) value).intValue());
237 else if (value instanceof Long)
238 setLong(caller, object, ((Long) value).longValue());
239 else if (value instanceof Float)
240 setFloat(caller, object, ((Float) value).floatValue());
241 else if (value instanceof Double)
242 setDouble(caller, object, ((Double) value).doubleValue());
243 else if (value instanceof Character)
244 setChar(caller, object, ((Character) value).charValue());
245 else if (value instanceof Boolean)
246 setBoolean(caller, object, ((Boolean) value).booleanValue());
247 else
248 throw new IllegalArgumentException();
249 }
250
251 public String toString ()
252 {
253 StringBuffer sbuf = new StringBuffer ();
254 int mods = getModifiers();
255 if (mods != 0)
256 Modifier.toString(mods, sbuf);
257 sbuf.append(getType());
258 sbuf.append(' ');
259 sbuf.append(getDeclaringClass());
260 sbuf.append('.');
261 sbuf.append(getName());
262 return sbuf.toString();
263 }
264 }