corda/classpath/java/lang/reflect/Field.java

167 lines
4.2 KiB
Java
Raw Normal View History

package java.lang.reflect;
public class Field<T> extends AccessibleObject {
private static final int VoidField = 0;
private static final int ByteField = 1;
private static final int CharField = 2;
private static final int DoubleField = 3;
private static final int FloatField = 4;
private static final int IntField = 5;
private static final int LongField = 6;
private static final int ShortField = 7;
private static final int BooleanField = 8;
private static final int ObjectField = 9;
private byte vmFlags;
private byte code;
private short flags;
private short offset;
private byte[] name;
private byte[] spec;
private Class<T> class_;
private Field() { }
public boolean isAccessible() {
return (vmFlags & Accessible) != 0;
}
public void setAccessible(boolean v) {
if (v) vmFlags |= Accessible; else vmFlags &= ~Accessible;
}
2007-07-21 20:44:39 +00:00
public Class<T> getDeclaringClass() {
return class_;
}
public int getModifiers() {
return flags;
}
public String getName() {
return new String(name, 0, name.length - 1, false);
}
2007-07-24 01:44:20 +00:00
public Class getType() {
return Class.forCanonicalName(new String(spec, 0, spec.length - 1, false));
}
public Object get(Object instance) throws IllegalAccessException {
2007-11-02 21:08:14 +00:00
Object target;
if ((flags & Modifier.STATIC) != 0) {
2007-11-02 21:08:14 +00:00
target = class_.staticTable();
} else if (class_.isInstance(instance)) {
2007-11-02 21:08:14 +00:00
target = instance;
} else {
throw new IllegalArgumentException();
}
2007-11-02 21:08:14 +00:00
switch (code) {
case ByteField:
return Byte.valueOf((byte) getPrimitive(target, code, offset));
2007-11-02 21:08:14 +00:00
case BooleanField:
return Boolean.valueOf(getPrimitive(target, code, offset) != 0);
2007-11-02 21:08:14 +00:00
case CharField:
return Character.valueOf((char) getPrimitive(target, code, offset));
2007-11-02 21:08:14 +00:00
case ShortField:
return Short.valueOf((short) getPrimitive(target, code, offset));
2007-11-02 21:08:14 +00:00
case IntField:
return Integer.valueOf((int) getPrimitive(target, code, offset));
2007-11-02 21:08:14 +00:00
case LongField:
return Long.valueOf((int) getPrimitive(target, code, offset));
2007-11-02 21:08:14 +00:00
case FloatField:
return Float.valueOf
(Float.intBitsToFloat((int) getPrimitive(target, code, offset)));
2007-11-02 21:08:14 +00:00
case DoubleField:
return Double.valueOf
(Double.longBitsToDouble(getPrimitive(target, code, offset)));
2007-11-02 21:08:14 +00:00
case ObjectField:
2007-11-02 21:42:19 +00:00
return getObject(target, offset);
2007-11-02 21:08:14 +00:00
default:
throw new Error();
}
}
public void set(Object instance, Object value)
throws IllegalAccessException
{
2007-11-02 21:08:14 +00:00
Object target;
if ((flags & Modifier.STATIC) != 0) {
2007-11-02 21:08:14 +00:00
target = class_.staticTable();
} else if (class_.isInstance(instance)) {
2007-11-02 21:08:14 +00:00
target = instance;
} else {
throw new IllegalArgumentException();
}
2007-11-02 21:08:14 +00:00
switch (code) {
case ByteField:
setPrimitive(target, code, offset, (Byte) value);
break;
case BooleanField:
setPrimitive(target, code, offset, ((Boolean) value) ? 1 : 0);
break;
case CharField:
setPrimitive(target, code, offset, (Character) value);
break;
case ShortField:
setPrimitive(target, code, offset, (Short) value);
break;
case IntField:
setPrimitive(target, code, offset, (Integer) value);
break;
case LongField:
setPrimitive(target, code, offset, (Long) value);
break;
case FloatField:
setPrimitive(target, code, offset,
Float.floatToRawIntBits((Float) value));
break;
case DoubleField:
setPrimitive(target, code, offset,
Double.doubleToRawLongBits((Double) value));
break;
case ObjectField:
if (value == null || getType().isInstance(value)) {
2007-11-02 21:08:14 +00:00
setObject(target, offset, value);
} else {
throw new IllegalArgumentException
("need " + getType() + ", got " + value.getClass().getName());
2007-11-02 21:08:14 +00:00
}
break;
default:
throw new Error();
}
}
private static native long getPrimitive
(Object instance, int code, int offset);
private static native Object getObject
(Object instance, int offset);
private static native void setPrimitive
(Object instance, int code, int offset, long value);
2007-07-27 23:56:19 +00:00
private static native void setObject
(Object instance, int offset, Object value);
}