Source for java.lang.reflect.Field

   1: /* Copyright (C) 1998, 1999, 2000, 2001, 2003  Free Software Foundation
   2: 
   3:    This file is part of libgcj.
   4: 
   5: This software is copyrighted work licensed under the terms of the
   6: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   7: details.  */
   8: 
   9: package java.lang.reflect;
  10: 
  11: /**
  12:  * @author Per Bothner <bothner@cygnus.com>
  13:  * @date September 1998;  February 1999.
  14:  */
  15: 
  16: public final class Field extends AccessibleObject implements Member
  17: {
  18:   private Class declaringClass;
  19: 
  20:   // This is filled in by getName.
  21:   private String name;
  22: 
  23:   // Offset in bytes from the start of declaringClass's fields array.
  24:   private int offset;
  25: 
  26:   // The Class (or primitive TYPE) of this field.
  27:   private Class type;
  28: 
  29:   // This is instantiated by Class sometimes, but it uses C++ and
  30:   // avoids the Java protection check.
  31:   Field ()
  32:   {
  33:   }
  34: 
  35:   public boolean equals (Object fld)
  36:   {
  37:     if (! (fld instanceof Field))
  38:       return false;
  39:     Field f = (Field) fld;
  40:     return declaringClass == f.declaringClass && offset == f.offset;
  41:   }
  42: 
  43:   public Class getDeclaringClass ()
  44:   {
  45:     return declaringClass;
  46:   }
  47: 
  48:   public native String getName ();
  49: 
  50:   public native Class getType ();
  51: 
  52:   public native int getModifiers ();
  53: 
  54:   public int hashCode()
  55:   {
  56:     return (declaringClass.hashCode() ^ offset);
  57:   }
  58: 
  59:   public boolean getBoolean (Object obj)
  60:     throws IllegalArgumentException, IllegalAccessException
  61:   {
  62:     return getBoolean(null, obj);
  63:   }
  64:   public char getChar (Object obj)
  65:     throws IllegalArgumentException, IllegalAccessException
  66:   {
  67:     return getChar(null, obj);
  68:   }
  69: 
  70:   public byte getByte (Object obj)
  71:     throws IllegalArgumentException, IllegalAccessException
  72:   {
  73:     return getByte(null, obj);
  74:   }
  75: 
  76:   public short getShort (Object obj)
  77:     throws IllegalArgumentException, IllegalAccessException
  78:   {
  79:     return getShort(null, obj);
  80:   }
  81: 
  82:   public int getInt (Object obj)
  83:     throws IllegalArgumentException, IllegalAccessException
  84:   {
  85:     return getInt(null, obj);
  86:   }
  87: 
  88:   public long getLong (Object obj)
  89:     throws IllegalArgumentException, IllegalAccessException
  90:   {
  91:     return getLong(null, obj);
  92:   }
  93: 
  94:   public float getFloat (Object obj)
  95:     throws IllegalArgumentException, IllegalAccessException
  96:   {
  97:     return getFloat(null, obj);
  98:   }
  99: 
 100:   public double getDouble (Object obj)
 101:     throws IllegalArgumentException, IllegalAccessException
 102:   {
 103:     return getDouble(null, obj);
 104:   }
 105: 
 106:   public Object get (Object obj)
 107:     throws IllegalArgumentException, IllegalAccessException
 108:   {
 109:     return get(null, obj);
 110:   }
 111: 
 112:   private native boolean getBoolean (Class caller, Object obj)
 113:     throws IllegalArgumentException, IllegalAccessException;
 114: 
 115:   private native char getChar (Class caller, Object obj)
 116:     throws IllegalArgumentException, IllegalAccessException;
 117: 
 118:   private native byte getByte (Class caller, Object obj)
 119:     throws IllegalArgumentException, IllegalAccessException;
 120: 
 121:   private native short getShort (Class caller, Object obj)
 122:     throws IllegalArgumentException, IllegalAccessException;
 123: 
 124:   private native int getInt (Class caller, Object obj)
 125:     throws IllegalArgumentException, IllegalAccessException;
 126: 
 127:   private native long getLong (Class caller, Object obj)
 128:     throws IllegalArgumentException, IllegalAccessException;
 129: 
 130:   private native float getFloat (Class caller, Object obj)
 131:     throws IllegalArgumentException, IllegalAccessException;
 132: 
 133:   private native double getDouble (Class caller, Object obj)
 134:     throws IllegalArgumentException, IllegalAccessException;
 135: 
 136:   private native Object get (Class caller, Object obj)
 137:     throws IllegalArgumentException, IllegalAccessException;
 138: 
 139:   public void setByte (Object obj, byte b)
 140:     throws IllegalArgumentException, IllegalAccessException
 141:   {
 142:     setByte(null, obj, b, true);
 143:   }
 144: 
 145:   public void setShort (Object obj,  short s)
 146:     throws IllegalArgumentException, IllegalAccessException
 147:   {
 148:     setShort(null, obj, s, true);
 149:   }
 150: 
 151:   public void setInt (Object obj, int i)
 152:     throws IllegalArgumentException, IllegalAccessException
 153:   {
 154:     setInt(null, obj, i, true);
 155:   }
 156: 
 157:   public void setLong (Object obj, long l)
 158:     throws IllegalArgumentException, IllegalAccessException
 159:   {
 160:     setLong(null, obj, l, true);
 161:   }
 162: 
 163:   public void setFloat (Object obj, float f)
 164:     throws IllegalArgumentException, IllegalAccessException
 165:   {
 166:     setFloat(null, obj, f, true);
 167:   }
 168: 
 169:   public void setDouble (Object obj, double d)
 170:     throws IllegalArgumentException, IllegalAccessException
 171:   {
 172:     setDouble(null, obj, d, true);
 173:   }
 174: 
 175:   public void setChar (Object obj, char c)
 176:     throws IllegalArgumentException, IllegalAccessException
 177:   {
 178:     setChar(null, obj, c, true);
 179:   }
 180: 
 181:   public void setBoolean (Object obj, boolean b)
 182:     throws IllegalArgumentException, IllegalAccessException
 183:   {
 184:     setBoolean(null, obj, b, true);
 185:   }
 186: 
 187:   native void setByte (Class caller, Object obj, byte b, boolean checkFinal)
 188:     throws IllegalArgumentException, IllegalAccessException;
 189: 
 190:   native void setShort (Class caller, Object obj, short s, boolean checkFinal)
 191:     throws IllegalArgumentException, IllegalAccessException;
 192: 
 193:   native void setInt (Class caller, Object obj, int i, boolean checkFinal)  
 194:     throws IllegalArgumentException, IllegalAccessException;
 195: 
 196:   native void setLong (Class caller, Object obj, long l, boolean checkFinal)
 197:     throws IllegalArgumentException, IllegalAccessException;
 198: 
 199:   native void setFloat (Class caller, Object obj, float f, boolean checkFinal)
 200:     throws IllegalArgumentException, IllegalAccessException;
 201: 
 202:   native void setDouble (Class caller, Object obj, double d,
 203:              boolean checkFinal)
 204:     throws IllegalArgumentException, IllegalAccessException;
 205: 
 206:   native void setChar (Class caller, Object obj, char c, boolean checkFinal)
 207:     throws IllegalArgumentException, IllegalAccessException;
 208: 
 209:   native void setBoolean (Class caller, Object obj, boolean b,
 210:               boolean checkFinal)
 211:     throws IllegalArgumentException, IllegalAccessException;
 212: 
 213:   native void set (Class caller, Object obj, Object val, Class type, 
 214:            boolean checkFinal)
 215:     throws IllegalArgumentException, IllegalAccessException;
 216: 
 217:   public void set (Object object, Object value)
 218:     throws IllegalArgumentException, IllegalAccessException
 219:   {
 220:     set(null, object, value);
 221:   }
 222: 
 223:   private void set (Class caller, Object object, Object value)
 224:     throws IllegalArgumentException, IllegalAccessException
 225:   {
 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();
 247:   }
 248: 
 249:   public String toString ()
 250:   {
 251:     StringBuffer sbuf = new StringBuffer ();
 252:     int mods = getModifiers();
 253:     if (mods != 0)
 254:       {
 255:     Modifier.toString(mods, sbuf);
 256:     sbuf.append(' ');
 257:       }
 258:     Method.appendClassName (sbuf, getType ());
 259:     sbuf.append(' ');
 260:     Method.appendClassName (sbuf, getDeclaringClass());
 261:     sbuf.append('.');
 262:     sbuf.append(getName());
 263:     return sbuf.toString();
 264:   }
 265: }