Field.java 7.57 KB
Newer Older
Tom Tromey committed
1
/* Copyright (C) 1998, 1999, 2000, 2001, 2003  Free Software Foundation
Tom Tromey committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.lang.reflect;

/**
 * @author Per Bothner <bothner@cygnus.com>
 * @date September 1998;  February 1999.
 */

public final class Field extends AccessibleObject implements Member
{
  private Class declaringClass;

  // This is filled in by getName.
  private String name;

  // Offset in bytes from the start of declaringClass's fields array.
  private int offset;

Bryce McKinlay committed
26 27 28
  // The Class (or primitive TYPE) of this field.
  private Class type;

29 30 31 32 33 34
  // This is instantiated by Class sometimes, but it uses C++ and
  // avoids the Java protection check.
  Field ()
  {
  }

Tom Tromey committed
35
  public boolean equals (Object fld)
Tom Tromey committed
36 37 38 39 40 41
  {
    if (! (fld instanceof Field))
      return false;
    Field f = (Field) fld;
    return declaringClass == f.declaringClass && offset == f.offset;
  }
Tom Tromey committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  public Class getDeclaringClass ()
  {
    return declaringClass;
  }

  public native String getName ();

  public native Class getType ();

  public native int getModifiers ();

  public int hashCode()
  {
    return (declaringClass.hashCode() ^ offset);
  }

  public boolean getBoolean (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getBoolean(null, obj);
  }
  public char getChar (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getChar(null, obj);
  }

  public byte getByte (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getByte(null, obj);
  }

  public short getShort (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getShort(null, obj);
  }

  public int getInt (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getInt(null, obj);
  }

  public long getLong (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getLong(null, obj);
  }

  public float getFloat (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getFloat(null, obj);
  }

  public double getDouble (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return getDouble(null, obj);
  }

  public Object get (Object obj)
    throws IllegalArgumentException, IllegalAccessException
  {
    return get(null, obj);
  }

  private native boolean getBoolean (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native char getChar (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native byte getByte (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native short getShort (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native int getInt (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native long getLong (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native float getFloat (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

  private native double getDouble (Class caller, Object obj)
    throws IllegalArgumentException, IllegalAccessException;

136
  private native Object get (Class caller, Object obj)
Tom Tromey committed
137 138 139 140 141
    throws IllegalArgumentException, IllegalAccessException;

  public void setByte (Object obj, byte b)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
142
    setByte(null, obj, b, true);
Tom Tromey committed
143 144 145 146 147
  }

  public void setShort (Object obj,  short s)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
148
    setShort(null, obj, s, true);
Tom Tromey committed
149 150 151 152 153
  }

  public void setInt (Object obj, int i)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
154
    setInt(null, obj, i, true);
Tom Tromey committed
155 156 157 158 159
  }

  public void setLong (Object obj, long l)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
160
    setLong(null, obj, l, true);
Tom Tromey committed
161 162 163 164 165
  }

  public void setFloat (Object obj, float f)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
166
    setFloat(null, obj, f, true);
Tom Tromey committed
167 168 169 170 171
  }

  public void setDouble (Object obj, double d)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
172
    setDouble(null, obj, d, true);
Tom Tromey committed
173 174 175 176 177
  }

  public void setChar (Object obj, char c)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
178
    setChar(null, obj, c, true);
Tom Tromey committed
179 180 181 182 183
  }

  public void setBoolean (Object obj, boolean b)
    throws IllegalArgumentException, IllegalAccessException
  {
Bryce McKinlay committed
184
    setBoolean(null, obj, b, true);
Tom Tromey committed
185 186
  }

Bryce McKinlay committed
187
  native void setByte (Class caller, Object obj, byte b, boolean checkFinal)
Tom Tromey committed
188 189
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
190
  native void setShort (Class caller, Object obj, short s, boolean checkFinal)
Tom Tromey committed
191 192
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
193
  native void setInt (Class caller, Object obj, int i, boolean checkFinal)  
Tom Tromey committed
194 195
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
196
  native void setLong (Class caller, Object obj, long l, boolean checkFinal)
Tom Tromey committed
197 198
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
199
  native void setFloat (Class caller, Object obj, float f, boolean checkFinal)
Tom Tromey committed
200 201
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
202 203
  native void setDouble (Class caller, Object obj, double d,
			 boolean checkFinal)
Tom Tromey committed
204 205
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
206
  native void setChar (Class caller, Object obj, char c, boolean checkFinal)
Tom Tromey committed
207 208
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
209 210
  native void setBoolean (Class caller, Object obj, boolean b,
			  boolean checkFinal)
Tom Tromey committed
211 212
    throws IllegalArgumentException, IllegalAccessException;

Bryce McKinlay committed
213 214
  native void set (Class caller, Object obj, Object val, Class type, 
		   boolean checkFinal)
Tom Tromey committed
215 216 217 218 219 220 221 222
    throws IllegalArgumentException, IllegalAccessException;

  public void set (Object object, Object value)
    throws IllegalArgumentException, IllegalAccessException
  {
    set(null, object, value);
  }

223
  private void set (Class caller, Object object, Object value)
Tom Tromey committed
224 225 226 227
    throws IllegalArgumentException, IllegalAccessException
  {
    Class type = getType();
    if (! type.isPrimitive())
Bryce McKinlay committed
228
      set(caller, object, value, type, true);
Tom Tromey committed
229
    else if (value instanceof Byte)
Bryce McKinlay committed
230
      setByte(caller, object, ((Byte) value).byteValue(), true);
Tom Tromey committed
231
    else if (value instanceof Short)
Bryce McKinlay committed
232
      setShort (caller, object, ((Short) value).shortValue(), true);
Tom Tromey committed
233
    else if (value instanceof Integer)
Bryce McKinlay committed
234
      setInt(caller, object, ((Integer) value).intValue(), true);
Tom Tromey committed
235
    else if (value instanceof Long)
Bryce McKinlay committed
236
      setLong(caller, object, ((Long) value).longValue(), true);
Tom Tromey committed
237
    else if (value instanceof Float)
Bryce McKinlay committed
238
      setFloat(caller, object, ((Float) value).floatValue(), true);
Tom Tromey committed
239
    else if (value instanceof Double)
Bryce McKinlay committed
240
      setDouble(caller, object, ((Double) value).doubleValue(), true);
Tom Tromey committed
241
    else if (value instanceof Character)
Bryce McKinlay committed
242
      setChar(caller, object, ((Character) value).charValue(), true);
Tom Tromey committed
243
    else if (value instanceof Boolean)
Bryce McKinlay committed
244
      setBoolean(caller, object, ((Boolean) value).booleanValue(), true);
Tom Tromey committed
245 246 247 248 249 250 251 252 253
    else
      throw new IllegalArgumentException();
  }

  public String toString ()
  {
    StringBuffer sbuf = new StringBuffer ();
    int mods = getModifiers();
    if (mods != 0)
254 255 256 257
      {
	Modifier.toString(mods, sbuf);
	sbuf.append(' ');
      }
258
    Method.appendClassName (sbuf, getType ());
Tom Tromey committed
259
    sbuf.append(' ');
260
    Method.appendClassName (sbuf, getDeclaringClass());
Tom Tromey committed
261 262 263 264 265
    sbuf.append('.');
    sbuf.append(getName());
    return sbuf.toString();
  }
}