mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-04 16:49:10 +00:00
Reflection wrapper started
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package org.parabot.core.reflect;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* A RefClass represents a class or an instance of that class, if no instance is
|
||||
* provided this class can only get values from static fields and only invoke
|
||||
* static methods
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class RefClass {
|
||||
private Object instance;
|
||||
private Class<?> clazz;
|
||||
|
||||
public RefClass(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public RefClass(Object instance) {
|
||||
setInstance(instance);
|
||||
}
|
||||
|
||||
public RefClass(Class<?> clazz, Object instance) {
|
||||
this.clazz = clazz;
|
||||
setInstance(instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the instance of this class so now non static fields values can be retrieved and non static methods can be invoked
|
||||
* @param instance instance of this class.
|
||||
*/
|
||||
public void setInstance(Object instance) {
|
||||
if(instance == null) {
|
||||
this.instance = null;
|
||||
return;
|
||||
}
|
||||
if (this.clazz != null) {
|
||||
if (!clazz.isInstance(instance)) {
|
||||
throw new IllegalArgumentException(instance
|
||||
+ " is not an instance of the class " + clazz);
|
||||
}
|
||||
}
|
||||
this.instance = instance;
|
||||
if (this.clazz == null) {
|
||||
this.clazz = instance.getClass();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets the instance of this class
|
||||
*
|
||||
* @return if an instance of this class is known it will return that instance, otherwise it will return null.
|
||||
*/
|
||||
public Object getInstance() {
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the class' fields
|
||||
* @return all fields if an instance is provided, otherwise only static fields
|
||||
*/
|
||||
public RefField[] getFields() {
|
||||
ArrayList<RefField> fields = new ArrayList<RefField>();
|
||||
if(this.instance == null) {
|
||||
return null;
|
||||
}
|
||||
return fields.toArray(new RefField[fields.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package org.parabot.core.reflect;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
*
|
||||
* A RefField represents a field in a class
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class RefField {
|
||||
private Field field;
|
||||
private Object instance;
|
||||
|
||||
public RefField(Field field) {
|
||||
this(field, null);
|
||||
}
|
||||
|
||||
public RefField(Field field, Object instance) {
|
||||
this.field = field;
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
public Object asObject() {
|
||||
if(instance == null && !isStatic()) {
|
||||
throw new IllegalStateException("Non static field cannot be fetched without an instance");
|
||||
}
|
||||
try {
|
||||
if(!isAccessible()) {
|
||||
field.setAccessible(true);
|
||||
}
|
||||
return field.get(instance);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int asInt() {
|
||||
return (int) asObject();
|
||||
}
|
||||
|
||||
public long asLong() {
|
||||
return (long) asObject();
|
||||
}
|
||||
|
||||
public double asDouble() {
|
||||
return (double) asObject();
|
||||
}
|
||||
|
||||
public float asFloat() {
|
||||
return (float) asObject();
|
||||
}
|
||||
|
||||
public boolean asBoolean() {
|
||||
return (boolean) asObject();
|
||||
}
|
||||
|
||||
public short asShort() {
|
||||
return (short) asObject();
|
||||
}
|
||||
|
||||
public byte asByte() {
|
||||
return (byte) asObject();
|
||||
}
|
||||
|
||||
public String asString() {
|
||||
return (String) asObject();
|
||||
}
|
||||
|
||||
public char asChar() {
|
||||
return (char) asObject();
|
||||
}
|
||||
|
||||
public Class<?> getType() {
|
||||
return field.getType();
|
||||
}
|
||||
|
||||
public Type getGenericType() {
|
||||
return field.getGenericType();
|
||||
}
|
||||
|
||||
public boolean isPrimitiveType() {
|
||||
return RefUtils.isPrimitive(getType());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public boolean isEnumConstants() {
|
||||
return field.isEnumConstant();
|
||||
}
|
||||
|
||||
public boolean isAccessible() {
|
||||
return field.isAccessible();
|
||||
}
|
||||
|
||||
public boolean isSynthetic() {
|
||||
return field.isSynthetic();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
public int getModifiers() {
|
||||
return field.getModifiers();
|
||||
}
|
||||
|
||||
public Field getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public String toGenericString() {
|
||||
return field.toGenericString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return field.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.parabot.core.reflect;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class RefUtils {
|
||||
|
||||
public static final Set<Class<?>> PRIMITIVE_TYPES = new HashSet<Class<?>>(
|
||||
Arrays.asList(Boolean.class, Character.class, Byte.class,
|
||||
Short.class, Integer.class, Long.class, Float.class,
|
||||
Double.class, Void.class));
|
||||
|
||||
public static boolean isPrimitive(Class<?> clazz) {
|
||||
return PRIMITIVE_TYPES.contains(clazz);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user