Merge from the pain train
[official-gcc.git] / libjava / java / lang / reflect / Field.java
blobcb852cf2fb2fb4b916a57e677082ff268a8562c0
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2003 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
7 details. */
9 package java.lang.reflect;
11 /**
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date September 1998; February 1999.
16 public final class Field extends AccessibleObject implements Member
18 private Class declaringClass;
20 // This is filled in by getName.
21 private String name;
23 // Offset in bytes from the start of declaringClass's fields array.
24 private int offset;
26 // The Class (or primitive TYPE) of this field.
27 private Class type;
29 // This is instantiated by Class sometimes, but it uses C++ and
30 // avoids the Java protection check.
31 Field ()
35 public boolean equals (Object fld)
37 if (! (fld instanceof Field))
38 return false;
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 ();
54 public int hashCode()
56 return (declaringClass.hashCode() ^ offset);
59 public boolean getBoolean (Object obj)
60 throws IllegalArgumentException, IllegalAccessException
62 return getBoolean(null, obj);
64 public char getChar (Object obj)
65 throws IllegalArgumentException, IllegalAccessException
67 return getChar(null, obj);
70 public byte getByte (Object obj)
71 throws IllegalArgumentException, IllegalAccessException
73 return getByte(null, obj);
76 public short getShort (Object obj)
77 throws IllegalArgumentException, IllegalAccessException
79 return getShort(null, obj);
82 public int getInt (Object obj)
83 throws IllegalArgumentException, IllegalAccessException
85 return getInt(null, obj);
88 public long getLong (Object obj)
89 throws IllegalArgumentException, IllegalAccessException
91 return getLong(null, obj);
94 public float getFloat (Object obj)
95 throws IllegalArgumentException, IllegalAccessException
97 return getFloat(null, obj);
100 public double getDouble (Object obj)
101 throws IllegalArgumentException, IllegalAccessException
103 return getDouble(null, obj);
106 public Object get (Object obj)
107 throws IllegalArgumentException, IllegalAccessException
109 return get(null, obj);
112 private native boolean getBoolean (Class caller, Object obj)
113 throws IllegalArgumentException, IllegalAccessException;
115 private native char getChar (Class caller, Object obj)
116 throws IllegalArgumentException, IllegalAccessException;
118 private native byte getByte (Class caller, Object obj)
119 throws IllegalArgumentException, IllegalAccessException;
121 private native short getShort (Class caller, Object obj)
122 throws IllegalArgumentException, IllegalAccessException;
124 private native int getInt (Class caller, Object obj)
125 throws IllegalArgumentException, IllegalAccessException;
127 private native long getLong (Class caller, Object obj)
128 throws IllegalArgumentException, IllegalAccessException;
130 private native float getFloat (Class caller, Object obj)
131 throws IllegalArgumentException, IllegalAccessException;
133 private native double getDouble (Class caller, Object obj)
134 throws IllegalArgumentException, IllegalAccessException;
136 private native Object get (Class caller, Object obj)
137 throws IllegalArgumentException, IllegalAccessException;
139 public void setByte (Object obj, byte b)
140 throws IllegalArgumentException, IllegalAccessException
142 setByte(null, obj, b, true);
145 public void setShort (Object obj, short s)
146 throws IllegalArgumentException, IllegalAccessException
148 setShort(null, obj, s, true);
151 public void setInt (Object obj, int i)
152 throws IllegalArgumentException, IllegalAccessException
154 setInt(null, obj, i, true);
157 public void setLong (Object obj, long l)
158 throws IllegalArgumentException, IllegalAccessException
160 setLong(null, obj, l, true);
163 public void setFloat (Object obj, float f)
164 throws IllegalArgumentException, IllegalAccessException
166 setFloat(null, obj, f, true);
169 public void setDouble (Object obj, double d)
170 throws IllegalArgumentException, IllegalAccessException
172 setDouble(null, obj, d, true);
175 public void setChar (Object obj, char c)
176 throws IllegalArgumentException, IllegalAccessException
178 setChar(null, obj, c, true);
181 public void setBoolean (Object obj, boolean b)
182 throws IllegalArgumentException, IllegalAccessException
184 setBoolean(null, obj, b, true);
187 native void setByte (Class caller, Object obj, byte b, boolean checkFinal)
188 throws IllegalArgumentException, IllegalAccessException;
190 native void setShort (Class caller, Object obj, short s, boolean checkFinal)
191 throws IllegalArgumentException, IllegalAccessException;
193 native void setInt (Class caller, Object obj, int i, boolean checkFinal)
194 throws IllegalArgumentException, IllegalAccessException;
196 native void setLong (Class caller, Object obj, long l, boolean checkFinal)
197 throws IllegalArgumentException, IllegalAccessException;
199 native void setFloat (Class caller, Object obj, float f, boolean checkFinal)
200 throws IllegalArgumentException, IllegalAccessException;
202 native void setDouble (Class caller, Object obj, double d,
203 boolean checkFinal)
204 throws IllegalArgumentException, IllegalAccessException;
206 native void setChar (Class caller, Object obj, char c, boolean checkFinal)
207 throws IllegalArgumentException, IllegalAccessException;
209 native void setBoolean (Class caller, Object obj, boolean b,
210 boolean checkFinal)
211 throws IllegalArgumentException, IllegalAccessException;
213 native void set (Class caller, Object obj, Object val, Class type,
214 boolean checkFinal)
215 throws IllegalArgumentException, IllegalAccessException;
217 public void set (Object object, Object value)
218 throws IllegalArgumentException, IllegalAccessException
220 set(null, object, value);
223 private void set (Class caller, Object object, Object value)
224 throws IllegalArgumentException, IllegalAccessException
226 Class type = getType();
227 if (! type.isPrimitive())
228 set(caller, object, value, type, true);
229 else if (value instanceof Byte)
230 setByte(caller, object, ((Byte) value).byteValue(), true);
231 else if (value instanceof Short)
232 setShort (caller, object, ((Short) value).shortValue(), true);
233 else if (value instanceof Integer)
234 setInt(caller, object, ((Integer) value).intValue(), true);
235 else if (value instanceof Long)
236 setLong(caller, object, ((Long) value).longValue(), true);
237 else if (value instanceof Float)
238 setFloat(caller, object, ((Float) value).floatValue(), true);
239 else if (value instanceof Double)
240 setDouble(caller, object, ((Double) value).doubleValue(), true);
241 else if (value instanceof Character)
242 setChar(caller, object, ((Character) value).charValue(), true);
243 else if (value instanceof Boolean)
244 setBoolean(caller, object, ((Boolean) value).booleanValue(), true);
245 else
246 throw new IllegalArgumentException();
249 public String toString ()
251 StringBuffer sbuf = new StringBuffer ();
252 int mods = getModifiers();
253 if (mods != 0)
255 Modifier.toString(mods, sbuf);
256 sbuf.append(' ');
258 Method.appendClassName (sbuf, getType ());
259 sbuf.append(' ');
260 Method.appendClassName (sbuf, getDeclaringClass());
261 sbuf.append('.');
262 sbuf.append(getName());
263 return sbuf.toString();