RefField updates

This commit is contained in:
Clisprail
2014-08-05 01:31:17 +02:00
parent 1baed13f31
commit 5e227e084e
@@ -1,7 +1,6 @@
package org.parabot.core.reflect; package org.parabot.core.reflect;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type; import java.lang.reflect.Type;
/** /**
@@ -11,25 +10,32 @@ import java.lang.reflect.Type;
* @author Everel * @author Everel
* *
*/ */
public class RefField { public class RefField extends RefModifiers {
private Field field; private Field field;
private Object instance; private Object instance;
public RefField(Field field) { public RefField(Field field) {
this(field, null); this(field, null);
} }
public RefField(Field field, Object instance) { public RefField(Field field, Object instance) {
super(field.getModifiers());
this.field = field; this.field = field;
this.instance = instance; this.instance = instance;
} }
/**
* Retrieves the field it's value as object
*
* @return the value of the field
*/
public Object asObject() { public Object asObject() {
if(instance == null && !isStatic()) { if (instance == null && !isStatic()) {
throw new IllegalStateException("Non static field cannot be fetched without an instance"); throw new IllegalStateException(
"Non static field cannot be fetched without an instance");
} }
try { try {
if(!isAccessible()) { if (!isAccessible()) {
field.setAccessible(true); field.setAccessible(true);
} }
return field.get(instance); return field.get(instance);
@@ -38,135 +44,327 @@ public class RefField {
} }
return null; return null;
} }
/**
* Retrieves the field value as an integer
*
* @return integer value of field
*/
public int asInt() { public int asInt() {
return (int) asObject(); return (int) asObject();
} }
/**
* Retrieves the field value as a long
*
* @return long value of field
*/
public long asLong() { public long asLong() {
return (long) asObject(); return (long) asObject();
} }
/**
* Retrieves the field value as an double
*
* @return double value of field
*/
public double asDouble() { public double asDouble() {
return (double) asObject(); return (double) asObject();
} }
/**
* Retrieves the field value as a float
*
* @return float value of field
*/
public float asFloat() { public float asFloat() {
return (float) asObject(); return (float) asObject();
} }
/**
* Retrieves the field value as a boolean
*
* @return boolean value of field
*/
public boolean asBoolean() { public boolean asBoolean() {
return (boolean) asObject(); return (boolean) asObject();
} }
/**
* Retrieves the field value as a short
*
* @return short value of field
*/
public short asShort() { public short asShort() {
return (short) asObject(); return (short) asObject();
} }
/**
* Retrieves the field value as a byte
*
* @return byte value of field
*/
public byte asByte() { public byte asByte() {
return (byte) asObject(); return (byte) asObject();
} }
/**
* Retrieves the field value as a java/lang/String
*
* @return String value of field
*/
public String asString() { public String asString() {
return (String) asObject(); return (String) asObject();
} }
/**
* Retrieves the field value as a character
*
* @return char value of field
*/
public char asChar() { public char asChar() {
return (char) asObject(); return (char) asObject();
} }
/**
* Sets the field value
*
* @param object
* object to set
*/
public void set(Object object) {
if (instance == null && !isStatic()) {
throw new IllegalStateException(
"Non static field cannot be set without an instance");
}
if (!field.isAccessible()) {
field.setAccessible(true);
}
try {
field.set(instance, object);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Sets the field integer value
*
* @param i
* value to set
*/
public void setInt(int i) {
set(i);
}
/**
* Sets the field long value
*
* @param l
* value to set
*/
public void setLong(long l) {
set(l);
}
/**
* Sets the field double value
*
* @param d
* value to set
*/
public void setDouble(double d) {
set(d);
}
/**
* Sets the field float value
*
* @param f
* value to set
*/
public void setFloat(float f) {
set(f);
}
/**
* Sets the field boolean value
*
* @param b
* value to set
*/
public void setBoolean(boolean b) {
set(b);
}
/**
* Sets the field short value
*
* @param s
* value to set
*/
public void setShort(short s) {
set(s);
}
/**
* Sets the byte integer value
*
* @param b
* value to set
*/
public void setByte(byte b) {
set(b);
}
/**
* Sets the field string value
*
* @param s
* value to set
*/
public void setString(String s) {
set(s);
}
/**
* Sets the field char value
*
* @param c
* value to set
*/
public void setChar(char c) {
set(c);
}
/**
* Gets the field type
*
* @return type of field
*/
public Class<?> getType() { public Class<?> getType() {
return field.getType(); return field.getType();
} }
/**
* Gets the field type
*
* @return type of field
*/
public org.objectweb.asm.Type getASMType() {
return org.objectweb.asm.Type.getType(getType());
}
/**
* Gets the field description
*
* @return desc of field
*/
public String getTypeDesc() {
return getASMType().getDescriptor();
}
/**
* Gets the generic type of this field if any
*
* @return generic type
*/
public Type getGenericType() { public Type getGenericType() {
return field.getGenericType(); return field.getGenericType();
} }
/**
* Determines if this field is an array
*
* @return <code>true</code> if this field is an array (type)
*/
public boolean isArray() {
return getASMType().getSort() == org.objectweb.asm.Type.ARRAY;
}
/**
* Returns the number of dimensions of this array type. This method should
* only be used for an array type.
*
* @return the number of dimensions of this array type
*/
public int getArrayDimensions() {
return getASMType().getDimensions();
}
/**
* Determines if field type is a primitive type
*
* @return <code>true</code> if the field is a primitive type, otherwise
* <code>false</code>
*/
public boolean isPrimitiveType() { public boolean isPrimitiveType() {
return RefUtils.isPrimitive(getType()); return RefUtils.isPrimitive(getType());
} }
/**
* Determines if field type is a string type
*
* @return <code>true</code> if the field type is a string type, otherwise
* <code>false</code>
*/
public boolean isString() { public boolean isString() {
return getType() == String.class; return getType() == String.class;
} }
public boolean isStatic() { /**
return Modifier.isStatic(field.getModifiers()); * Returns <code>true</code> if this field represents an element of an
} * enumerated type; returns <code>false</code> otherwise.
*
public boolean isAbstract() { * @return <code>true</code> if and only if this field represents an element
return Modifier.isAbstract(field.getModifiers()); * of an enumerated type.
} */
public boolean isFinal() {
return Modifier.isFinal(field.getModifiers());
}
public boolean isInterface() {
return Modifier.isInterface(field.getModifiers());
}
public boolean isNative() {
return Modifier.isNative(field.getModifiers());
}
public boolean isPrivate() {
return Modifier.isPrivate(field.getModifiers());
}
public boolean isProtected() {
return Modifier.isProtected(field.getModifiers());
}
public boolean isPublic() {
return Modifier.isPublic(field.getModifiers());
}
public boolean isStrict() {
return Modifier.isStrict(field.getModifiers());
}
public boolean isSynchronized() {
return Modifier.isSynchronized(field.getModifiers());
}
public boolean isTransient() {
return Modifier.isTransient(field.getModifiers());
}
public boolean isVolatile() {
return Modifier.isVolatile(field.getModifiers());
}
public boolean isEnumConstants() { public boolean isEnumConstants() {
return field.isEnumConstant(); return field.isEnumConstant();
} }
/**
* Get the value of the accessible flag for this object.
*
* @return the value of the object's accessible flag
*/
public boolean isAccessible() { public boolean isAccessible() {
return field.isAccessible(); return field.isAccessible();
} }
/**
* Returns <code>true</code> if this field is a synthetic field; returns
* <code>false</code> otherwise.
*
* @return <code>true</code> if this field is a synthetic field; returns
* <code>false</code> otherwise
*/
public boolean isSynthetic() { public boolean isSynthetic() {
return field.isSynthetic(); return field.isSynthetic();
} }
/**
* Returns the name of the field.
*
* @return name of the field
*/
public String getName() { public String getName() {
return field.getName(); return field.getName();
} }
public int getModifiers() { /**
return field.getModifiers(); * Gets the java reflection API field representation
} *
* @return field
*/
public Field getField() { public Field getField() {
return field; return field;
} }
public String toGenericString() { public String toGenericString() {
return field.toGenericString(); return field.toGenericString();
} }
public String toString() { public String toString() {
return field.toString(); return field.toString();
} }