1 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 package java
.lang
.reflect
;
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date September 1998; February 1999.
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.
25 public final class Field
extends AccessibleObject
implements Member
27 private Class declaringClass
;
29 // This is filled in by getName.
32 // Offset in bytes from the start of declaringClass's fields array.
35 // This is instantiated by Class sometimes, but it uses C++ and
36 // avoids the Java protection check.
41 public boolean equals (Object fld
)
43 if (! (fld
instanceof Field
))
45 Field f
= (Field
) fld
;
46 return declaringClass
== f
.declaringClass
&& offset
== f
.offset
;
49 public Class
getDeclaringClass ()
51 return declaringClass
;
54 public native String
getName ();
56 public native Class
getType ();
58 public native int getModifiers ();
62 return (declaringClass
.hashCode() ^ offset
);
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.
70 public boolean getBoolean (Object obj
)
71 throws IllegalArgumentException
, IllegalAccessException
73 return getBoolean(null, obj
);
75 public char getChar (Object obj
)
76 throws IllegalArgumentException
, IllegalAccessException
78 return getChar(null, obj
);
81 public byte getByte (Object obj
)
82 throws IllegalArgumentException
, IllegalAccessException
84 return getByte(null, obj
);
87 public short getShort (Object obj
)
88 throws IllegalArgumentException
, IllegalAccessException
90 return getShort(null, obj
);
93 public int getInt (Object obj
)
94 throws IllegalArgumentException
, IllegalAccessException
96 return getInt(null, obj
);
99 public long getLong (Object obj
)
100 throws IllegalArgumentException
, IllegalAccessException
102 return getLong(null, obj
);
105 public float getFloat (Object obj
)
106 throws IllegalArgumentException
, IllegalAccessException
108 return getFloat(null, obj
);
111 public double getDouble (Object obj
)
112 throws IllegalArgumentException
, IllegalAccessException
114 return getDouble(null, obj
);
117 public Object
get (Object obj
)
118 throws IllegalArgumentException
, IllegalAccessException
120 return get(null, obj
);
123 private native boolean getBoolean (Class caller
, Object obj
)
124 throws IllegalArgumentException
, IllegalAccessException
;
126 private native char getChar (Class caller
, Object obj
)
127 throws IllegalArgumentException
, IllegalAccessException
;
129 private native byte getByte (Class caller
, Object obj
)
130 throws IllegalArgumentException
, IllegalAccessException
;
132 private native short getShort (Class caller
, Object obj
)
133 throws IllegalArgumentException
, IllegalAccessException
;
135 private native int getInt (Class caller
, Object obj
)
136 throws IllegalArgumentException
, IllegalAccessException
;
138 private native long getLong (Class caller
, Object obj
)
139 throws IllegalArgumentException
, IllegalAccessException
;
141 private native float getFloat (Class caller
, Object obj
)
142 throws IllegalArgumentException
, IllegalAccessException
;
144 private native double getDouble (Class caller
, Object obj
)
145 throws IllegalArgumentException
, IllegalAccessException
;
147 private native Object
get (Class caller
, Object obj
)
148 throws IllegalArgumentException
, IllegalAccessException
;
150 public void setByte (Object obj
, byte b
)
151 throws IllegalArgumentException
, IllegalAccessException
153 setByte(null, obj
, b
);
156 public void setShort (Object obj
, short s
)
157 throws IllegalArgumentException
, IllegalAccessException
159 setShort(null, obj
, s
);
162 public void setInt (Object obj
, int i
)
163 throws IllegalArgumentException
, IllegalAccessException
165 setInt(null, obj
, i
);
168 public void setLong (Object obj
, long l
)
169 throws IllegalArgumentException
, IllegalAccessException
171 setLong(null, obj
, l
);
174 public void setFloat (Object obj
, float f
)
175 throws IllegalArgumentException
, IllegalAccessException
177 setFloat(null, obj
, f
);
180 public void setDouble (Object obj
, double d
)
181 throws IllegalArgumentException
, IllegalAccessException
183 setDouble(null, obj
, d
);
186 public void setChar (Object obj
, char c
)
187 throws IllegalArgumentException
, IllegalAccessException
189 setChar(null, obj
, c
);
192 public void setBoolean (Object obj
, boolean b
)
193 throws IllegalArgumentException
, IllegalAccessException
195 setBoolean(null, obj
, b
);
198 private native void setByte (Class caller
, Object obj
, byte b
)
199 throws IllegalArgumentException
, IllegalAccessException
;
201 private native void setShort (Class caller
, Object obj
, short s
)
202 throws IllegalArgumentException
, IllegalAccessException
;
204 private native void setInt (Class caller
, Object obj
, int i
)
205 throws IllegalArgumentException
, IllegalAccessException
;
207 private native void setLong (Class caller
, Object obj
, long l
)
208 throws IllegalArgumentException
, IllegalAccessException
;
210 private native void setFloat (Class caller
, Object obj
, float f
)
211 throws IllegalArgumentException
, IllegalAccessException
;
213 private native void setDouble (Class caller
, Object obj
, double d
)
214 throws IllegalArgumentException
, IllegalAccessException
;
216 private native void setChar (Class caller
, Object obj
, char c
)
217 throws IllegalArgumentException
, IllegalAccessException
;
219 private native void setBoolean (Class caller
, Object obj
, boolean b
)
220 throws IllegalArgumentException
, IllegalAccessException
;
222 private native void set (Class caller
, Object obj
, Object val
, Class type
)
223 throws IllegalArgumentException
, IllegalAccessException
;
225 public void set (Object object
, Object value
)
226 throws IllegalArgumentException
, IllegalAccessException
228 set(null, object
, value
);
231 private void set (Class caller
, Object object
, Object value
)
232 throws IllegalArgumentException
, IllegalAccessException
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());
254 throw new IllegalArgumentException();
257 public String
toString ()
259 StringBuffer sbuf
= new StringBuffer ();
260 int mods
= getModifiers();
263 Modifier
.toString(mods
, sbuf
);
266 Method
.appendClassName (sbuf
, getType ());
268 Method
.appendClassName (sbuf
, getDeclaringClass());
270 sbuf
.append(getName());
271 return sbuf
.toString();