1 /* Copyright (C) 1998, 1999 Cygnus Solutions
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 public boolean equals (Object fld
)
37 if (! (fld
instanceof Field
))
39 Field f
= (Field
) fld
;
40 return declaringClass
== f
.declaringClass
&& offset
== f
.offset
;
43 public Class
getDeclaringClass ()
45 return declaringClass
;
48 public native String
getName ();
50 public native Class
getType ();
52 public native int getModifiers ();
56 return (declaringClass
.hashCode() ^ offset
);
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.
64 public boolean getBoolean (Object obj
)
65 throws IllegalArgumentException
, IllegalAccessException
67 return getBoolean(null, obj
);
69 public char getChar (Object obj
)
70 throws IllegalArgumentException
, IllegalAccessException
72 return getChar(null, obj
);
75 public byte getByte (Object obj
)
76 throws IllegalArgumentException
, IllegalAccessException
78 return getByte(null, obj
);
81 public short getShort (Object obj
)
82 throws IllegalArgumentException
, IllegalAccessException
84 return getShort(null, obj
);
87 public int getInt (Object obj
)
88 throws IllegalArgumentException
, IllegalAccessException
90 return getInt(null, obj
);
93 public long getLong (Object obj
)
94 throws IllegalArgumentException
, IllegalAccessException
96 return getLong(null, obj
);
99 public float getFloat (Object obj
)
100 throws IllegalArgumentException
, IllegalAccessException
102 return getFloat(null, obj
);
105 public double getDouble (Object obj
)
106 throws IllegalArgumentException
, IllegalAccessException
108 return getDouble(null, obj
);
111 public Object
get (Object obj
)
112 throws IllegalArgumentException
, IllegalAccessException
114 return get(null, obj
);
117 private native boolean getBoolean (Class caller
, Object obj
)
118 throws IllegalArgumentException
, IllegalAccessException
;
120 private native char getChar (Class caller
, Object obj
)
121 throws IllegalArgumentException
, IllegalAccessException
;
123 private native byte getByte (Class caller
, Object obj
)
124 throws IllegalArgumentException
, IllegalAccessException
;
126 private native short getShort (Class caller
, Object obj
)
127 throws IllegalArgumentException
, IllegalAccessException
;
129 private native int getInt (Class caller
, Object obj
)
130 throws IllegalArgumentException
, IllegalAccessException
;
132 private native long getLong (Class caller
, Object obj
)
133 throws IllegalArgumentException
, IllegalAccessException
;
135 private native float getFloat (Class caller
, Object obj
)
136 throws IllegalArgumentException
, IllegalAccessException
;
138 private native double getDouble (Class caller
, Object obj
)
139 throws IllegalArgumentException
, IllegalAccessException
;
141 public native Object
get (Class caller
, Object obj
)
142 throws IllegalArgumentException
, IllegalAccessException
;
144 public void setByte (Object obj
, byte b
)
145 throws IllegalArgumentException
, IllegalAccessException
147 setByte(null, obj
, b
);
150 public void setShort (Object obj
, short s
)
151 throws IllegalArgumentException
, IllegalAccessException
153 setShort(null, obj
, s
);
156 public void setInt (Object obj
, int i
)
157 throws IllegalArgumentException
, IllegalAccessException
159 setInt(null, obj
, i
);
162 public void setLong (Object obj
, long l
)
163 throws IllegalArgumentException
, IllegalAccessException
165 setLong(null, obj
, l
);
168 public void setFloat (Object obj
, float f
)
169 throws IllegalArgumentException
, IllegalAccessException
171 setFloat(null, obj
, f
);
174 public void setDouble (Object obj
, double d
)
175 throws IllegalArgumentException
, IllegalAccessException
177 setDouble(null, obj
, d
);
180 public void setChar (Object obj
, char c
)
181 throws IllegalArgumentException
, IllegalAccessException
183 setChar(null, obj
, c
);
186 public void setBoolean (Object obj
, boolean b
)
187 throws IllegalArgumentException
, IllegalAccessException
189 setBoolean(null, obj
, b
);
192 public native void setByte (Class caller
, Object obj
, byte b
)
193 throws IllegalArgumentException
, IllegalAccessException
;
195 public native void setShort (Class caller
, Object obj
, short s
)
196 throws IllegalArgumentException
, IllegalAccessException
;
198 public native void setInt (Class caller
, Object obj
, int i
)
199 throws IllegalArgumentException
, IllegalAccessException
;
201 public native void setLong (Class caller
, Object obj
, long l
)
202 throws IllegalArgumentException
, IllegalAccessException
;
204 public native void setFloat (Class caller
, Object obj
, float f
)
205 throws IllegalArgumentException
, IllegalAccessException
;
207 public native void setDouble (Class caller
, Object obj
, double d
)
208 throws IllegalArgumentException
, IllegalAccessException
;
210 public native void setChar (Class caller
, Object obj
, char c
)
211 throws IllegalArgumentException
, IllegalAccessException
;
213 public native void setBoolean (Class caller
, Object obj
, boolean b
)
214 throws IllegalArgumentException
, IllegalAccessException
;
216 private native void set (Class caller
, Object obj
, Object val
, Class type
)
217 throws IllegalArgumentException
, IllegalAccessException
;
219 public void set (Object object
, Object value
)
220 throws IllegalArgumentException
, IllegalAccessException
222 set(null, object
, value
);
225 public void set (Class caller
, Object object
, Object value
)
226 throws IllegalArgumentException
, IllegalAccessException
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());
248 throw new IllegalArgumentException();
251 public String
toString ()
253 StringBuffer sbuf
= new StringBuffer ();
254 int mods
= getModifiers();
256 Modifier
.toString(mods
, sbuf
);
257 sbuf
.append(getType());
259 sbuf
.append(getDeclaringClass());
261 sbuf
.append(getName());
262 return sbuf
.toString();