From 5e227e084ebae3aaff2ea11ca2fd9523c4d0e09d Mon Sep 17 00:00:00 2001 From: Clisprail Date: Tue, 5 Aug 2014 01:31:17 +0200 Subject: [PATCH] RefField updates --- .../org/parabot/core/reflect/RefField.java | 358 ++++++++++++++---- 1 file changed, 278 insertions(+), 80 deletions(-) diff --git a/parabotv2/src/org/parabot/core/reflect/RefField.java b/parabotv2/src/org/parabot/core/reflect/RefField.java index 96c7e1e..b3edaf8 100644 --- a/parabotv2/src/org/parabot/core/reflect/RefField.java +++ b/parabotv2/src/org/parabot/core/reflect/RefField.java @@ -1,7 +1,6 @@ package org.parabot.core.reflect; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; /** @@ -11,25 +10,32 @@ import java.lang.reflect.Type; * @author Everel * */ -public class RefField { +public class RefField extends RefModifiers { private Field field; private Object instance; - + public RefField(Field field) { this(field, null); } - + public RefField(Field field, Object instance) { + super(field.getModifiers()); this.field = field; this.instance = instance; } - + + /** + * Retrieves the field it's value as object + * + * @return the value of the field + */ public Object asObject() { - if(instance == null && !isStatic()) { - throw new IllegalStateException("Non static field cannot be fetched without an instance"); + if (instance == null && !isStatic()) { + throw new IllegalStateException( + "Non static field cannot be fetched without an instance"); } try { - if(!isAccessible()) { + if (!isAccessible()) { field.setAccessible(true); } return field.get(instance); @@ -38,135 +44,327 @@ public class RefField { } return null; } - + + /** + * Retrieves the field value as an integer + * + * @return integer value of field + */ public int asInt() { return (int) asObject(); } - + + /** + * Retrieves the field value as a long + * + * @return long value of field + */ public long asLong() { return (long) asObject(); } - + + /** + * Retrieves the field value as an double + * + * @return double value of field + */ public double asDouble() { return (double) asObject(); } - + + /** + * Retrieves the field value as a float + * + * @return float value of field + */ public float asFloat() { return (float) asObject(); } - + + /** + * Retrieves the field value as a boolean + * + * @return boolean value of field + */ public boolean asBoolean() { return (boolean) asObject(); } - + + /** + * Retrieves the field value as a short + * + * @return short value of field + */ public short asShort() { return (short) asObject(); } - + + /** + * Retrieves the field value as a byte + * + * @return byte value of field + */ public byte asByte() { return (byte) asObject(); } - + + /** + * Retrieves the field value as a java/lang/String + * + * @return String value of field + */ public String asString() { return (String) asObject(); } - + + /** + * Retrieves the field value as a character + * + * @return char value of field + */ public char asChar() { 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() { 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() { return field.getGenericType(); } - + + /** + * Determines if this field is an array + * + * @return true 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 true if the field is a primitive type, otherwise + * false + */ public boolean isPrimitiveType() { return RefUtils.isPrimitive(getType()); } - + + /** + * Determines if field type is a string type + * + * @return true if the field type is a string type, otherwise + * false + */ public boolean isString() { return getType() == String.class; } - - public boolean isStatic() { - return Modifier.isStatic(field.getModifiers()); - } - - public boolean isAbstract() { - return Modifier.isAbstract(field.getModifiers()); - } - - 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()); - } - + + /** + * Returns true if this field represents an element of an + * enumerated type; returns false otherwise. + * + * @return true if and only if this field represents an element + * of an enumerated type. + */ public boolean isEnumConstants() { 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() { return field.isAccessible(); } - + + /** + * Returns true if this field is a synthetic field; returns + * false otherwise. + * + * @return true if this field is a synthetic field; returns + * false otherwise + */ public boolean isSynthetic() { return field.isSynthetic(); } - + + /** + * Returns the name of the field. + * + * @return name of the field + */ public String getName() { return field.getName(); } - - public int getModifiers() { - return field.getModifiers(); - } - + + /** + * Gets the java reflection API field representation + * + * @return field + */ public Field getField() { return field; } - + public String toGenericString() { return field.toGenericString(); } - + public String toString() { return field.toString(); }