2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / reflect / Field.java
blobb54a103d9bc4956d85978cdb109c375b441956fd
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 // This is instantiated by Class sometimes, but it uses C++ and
27 // avoids the Java protection check.
28 Field ()
32 public boolean equals (Object fld)
34 if (! (fld instanceof Field))
35 return false;
36 Field f = (Field) fld;
37 return declaringClass == f.declaringClass && offset == f.offset;
40 public Class getDeclaringClass ()
42 return declaringClass;
45 public native String getName ();
47 public native Class getType ();
49 public native int getModifiers ();
51 public int hashCode()
53 return (declaringClass.hashCode() ^ offset);
56 public boolean getBoolean (Object obj)
57 throws IllegalArgumentException, IllegalAccessException
59 return getBoolean(null, obj);
61 public char getChar (Object obj)
62 throws IllegalArgumentException, IllegalAccessException
64 return getChar(null, obj);
67 public byte getByte (Object obj)
68 throws IllegalArgumentException, IllegalAccessException
70 return getByte(null, obj);
73 public short getShort (Object obj)
74 throws IllegalArgumentException, IllegalAccessException
76 return getShort(null, obj);
79 public int getInt (Object obj)
80 throws IllegalArgumentException, IllegalAccessException
82 return getInt(null, obj);
85 public long getLong (Object obj)
86 throws IllegalArgumentException, IllegalAccessException
88 return getLong(null, obj);
91 public float getFloat (Object obj)
92 throws IllegalArgumentException, IllegalAccessException
94 return getFloat(null, obj);
97 public double getDouble (Object obj)
98 throws IllegalArgumentException, IllegalAccessException
100 return getDouble(null, obj);
103 public Object get (Object obj)
104 throws IllegalArgumentException, IllegalAccessException
106 return get(null, obj);
109 private native boolean getBoolean (Class caller, Object obj)
110 throws IllegalArgumentException, IllegalAccessException;
112 private native char getChar (Class caller, Object obj)
113 throws IllegalArgumentException, IllegalAccessException;
115 private native byte getByte (Class caller, Object obj)
116 throws IllegalArgumentException, IllegalAccessException;
118 private native short getShort (Class caller, Object obj)
119 throws IllegalArgumentException, IllegalAccessException;
121 private native int getInt (Class caller, Object obj)
122 throws IllegalArgumentException, IllegalAccessException;
124 private native long getLong (Class caller, Object obj)
125 throws IllegalArgumentException, IllegalAccessException;
127 private native float getFloat (Class caller, Object obj)
128 throws IllegalArgumentException, IllegalAccessException;
130 private native double getDouble (Class caller, Object obj)
131 throws IllegalArgumentException, IllegalAccessException;
133 private native Object get (Class caller, Object obj)
134 throws IllegalArgumentException, IllegalAccessException;
136 public void setByte (Object obj, byte b)
137 throws IllegalArgumentException, IllegalAccessException
139 setByte(null, obj, b);
142 public void setShort (Object obj, short s)
143 throws IllegalArgumentException, IllegalAccessException
145 setShort(null, obj, s);
148 public void setInt (Object obj, int i)
149 throws IllegalArgumentException, IllegalAccessException
151 setInt(null, obj, i);
154 public void setLong (Object obj, long l)
155 throws IllegalArgumentException, IllegalAccessException
157 setLong(null, obj, l);
160 public void setFloat (Object obj, float f)
161 throws IllegalArgumentException, IllegalAccessException
163 setFloat(null, obj, f);
166 public void setDouble (Object obj, double d)
167 throws IllegalArgumentException, IllegalAccessException
169 setDouble(null, obj, d);
172 public void setChar (Object obj, char c)
173 throws IllegalArgumentException, IllegalAccessException
175 setChar(null, obj, c);
178 public void setBoolean (Object obj, boolean b)
179 throws IllegalArgumentException, IllegalAccessException
181 setBoolean(null, obj, b);
184 private native void setByte (Class caller, Object obj, byte b)
185 throws IllegalArgumentException, IllegalAccessException;
187 private native void setShort (Class caller, Object obj, short s)
188 throws IllegalArgumentException, IllegalAccessException;
190 private native void setInt (Class caller, Object obj, int i)
191 throws IllegalArgumentException, IllegalAccessException;
193 private native void setLong (Class caller, Object obj, long l)
194 throws IllegalArgumentException, IllegalAccessException;
196 private native void setFloat (Class caller, Object obj, float f)
197 throws IllegalArgumentException, IllegalAccessException;
199 private native void setDouble (Class caller, Object obj, double d)
200 throws IllegalArgumentException, IllegalAccessException;
202 private native void setChar (Class caller, Object obj, char c)
203 throws IllegalArgumentException, IllegalAccessException;
205 private native void setBoolean (Class caller, Object obj, boolean b)
206 throws IllegalArgumentException, IllegalAccessException;
208 private native void set (Class caller, Object obj, Object val, Class type)
209 throws IllegalArgumentException, IllegalAccessException;
211 public void set (Object object, Object value)
212 throws IllegalArgumentException, IllegalAccessException
214 set(null, object, value);
217 private void set (Class caller, Object object, Object value)
218 throws IllegalArgumentException, IllegalAccessException
220 Class type = getType();
221 if (! type.isPrimitive())
222 set(caller, object, value, type);
223 else if (value instanceof Byte)
224 setByte(caller, object, ((Byte) value).byteValue());
225 else if (value instanceof Short)
226 setShort (caller, object, ((Short) value).shortValue());
227 else if (value instanceof Integer)
228 setInt(caller, object, ((Integer) value).intValue());
229 else if (value instanceof Long)
230 setLong(caller, object, ((Long) value).longValue());
231 else if (value instanceof Float)
232 setFloat(caller, object, ((Float) value).floatValue());
233 else if (value instanceof Double)
234 setDouble(caller, object, ((Double) value).doubleValue());
235 else if (value instanceof Character)
236 setChar(caller, object, ((Character) value).charValue());
237 else if (value instanceof Boolean)
238 setBoolean(caller, object, ((Boolean) value).booleanValue());
239 else
240 throw new IllegalArgumentException();
243 public String toString ()
245 StringBuffer sbuf = new StringBuffer ();
246 int mods = getModifiers();
247 if (mods != 0)
249 Modifier.toString(mods, sbuf);
250 sbuf.append(' ');
252 Method.appendClassName (sbuf, getType ());
253 sbuf.append(' ');
254 Method.appendClassName (sbuf, getDeclaringClass());
255 sbuf.append('.');
256 sbuf.append(getName());
257 return sbuf.toString();