Reflection wrapper formatting

This commit is contained in:
Clisprail
2014-08-05 20:08:40 +02:00
parent be646c5105
commit 44af24194c
6 changed files with 163 additions and 118 deletions
@@ -8,9 +8,9 @@ import java.util.ArrayList;
/** /**
* *
* A RefClass represents a class or an instance of that class, if no instance is * A <code>RefClass</code> represents a class or an instance of that class, if
* provided this class can only get values from static fields and only invoke * no instance is provided this class can only get values from static fields and
* static methods * only invoke static methods
* *
* @author Everel * @author Everel
* *
@@ -34,11 +34,14 @@ public class RefClass extends RefModifiers {
} }
/** /**
* Sets the instance of this class so now non static fields values can be retrieved and non static methods can be invoked * Sets the instance of this class so now non static fields values can be
* @param instance instance of this class. * retrieved and non static methods can be invoked
*
* @param instance
* instance of this class.
*/ */
public void setInstance(Object instance) { public void setInstance(Object instance) {
if(instance == null) { if (instance == null) {
this.instance = null; this.instance = null;
return; return;
} }
@@ -54,7 +57,8 @@ public class RefClass extends RefModifiers {
/** /**
* Gets the instance of this class * 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. * @return if an instance of this class is known it will return that
* instance, otherwise it will return null.
*/ */
public Object getInstance() { public Object getInstance() {
return this.instance; return this.instance;
@@ -83,6 +87,7 @@ public class RefClass extends RefModifiers {
/** /**
* Gets the type of this class * Gets the type of this class
*
* @return type of this class * @return type of this class
*/ */
public org.objectweb.asm.Type getASMType() { public org.objectweb.asm.Type getASMType() {
@@ -91,20 +96,22 @@ public class RefClass extends RefModifiers {
/** /**
* Gets the class' fields * Gets the class' fields
* @return all fields if an instance is provided, otherwise only static fields *
* @return all fields if an instance is provided, otherwise only static
* fields
*/ */
public RefField[] getFields() { public RefField[] getFields() {
ArrayList<RefField> fields = new ArrayList<RefField>(); ArrayList<RefField> fields = new ArrayList<RefField>();
// add all static fields // add all static fields
for(Field f : clazz.getDeclaredFields()) { for (Field f : clazz.getDeclaredFields()) {
if(Modifier.isStatic(f.getModifiers())) { if (Modifier.isStatic(f.getModifiers())) {
fields.add(new RefField(f, instance)); fields.add(new RefField(f, instance));
} }
} }
if(this.instance != null) { if (this.instance != null) {
// add all non static fields // add all non static fields
for(Field f : clazz.getDeclaredFields()) { for (Field f : clazz.getDeclaredFields()) {
if(!Modifier.isStatic(f.getModifiers())) { if (!Modifier.isStatic(f.getModifiers())) {
fields.add(new RefField(f, instance)); fields.add(new RefField(f, instance));
} }
} }
@@ -114,7 +121,9 @@ public class RefClass extends RefModifiers {
/** /**
* Gets field by field name * Gets field by field name
* @param name name of the field *
* @param name
* name of the field
* @return the field if found * @return the field if found
*/ */
public RefField getField(String name) { public RefField getField(String name) {
@@ -123,18 +132,21 @@ public class RefClass extends RefModifiers {
/** /**
* Gets field by field name and desc * Gets field by field name and desc
* @param name name of the field *
* @param desc desc type of the field * @param name
* name of the field
* @param desc
* desc type of the field
* @return the field if found * @return the field if found
*/ */
public RefField getField(String name, String desc) { public RefField getField(String name, String desc) {
RefField[] fields = getFields(); RefField[] fields = getFields();
for(RefField f : fields) { for (RefField f : fields) {
if(f.getName().equals(name)) { if (f.getName().equals(name)) {
if(desc == null) { if (desc == null) {
return f; return f;
} }
if(desc.equals(f.getTypeDesc())) { if (desc.equals(f.getTypeDesc())) {
return f; return f;
} }
} }
@@ -144,7 +156,9 @@ public class RefClass extends RefModifiers {
/** /**
* Determines if this class has a super class * Determines if this class has a super class
* @return <code>true</code> if this class has a super class and which is not the java/lang/Object class, otherwise <code>false.</code> *
* @return <code>true</code> if this class has a super class and which is
* not the java/lang/Object class, otherwise <code>false.</code>
*/ */
public boolean hasSuperclass() { public boolean hasSuperclass() {
return hasSuperclass(true); return hasSuperclass(true);
@@ -152,11 +166,15 @@ public class RefClass extends RefModifiers {
/** /**
* Determines if this class has a super class * Determines if this class has a super class
* @param ignoreObjectClass if you want this method to return false when the superclass is the java/lang/Object class *
* @return <code>true</code> if this class has a superclass, otherwise <code>false</code> * @param ignoreObjectClass
* if you want this method to return false when the superclass is
* the java/lang/Object class
* @return <code>true</code> if this class has a superclass, otherwise
* <code>false</code>
*/ */
public boolean hasSuperclass(boolean ignoreObjectClass) { public boolean hasSuperclass(boolean ignoreObjectClass) {
if(!ignoreObjectClass) { if (!ignoreObjectClass) {
return !clazz.equals(Object.class); return !clazz.equals(Object.class);
} }
Class<?> superClass = clazz.getSuperclass(); Class<?> superClass = clazz.getSuperclass();
@@ -165,6 +183,7 @@ public class RefClass extends RefModifiers {
/** /**
* Returns a new RefClass representing the superclass of this RefClass * Returns a new RefClass representing the superclass of this RefClass
*
* @return superclass of this RefClass * @return superclass of this RefClass
*/ */
public RefClass getSuperclass() { public RefClass getSuperclass() {
@@ -173,12 +192,13 @@ public class RefClass extends RefModifiers {
/** /**
* Creates a new instance of this class * Creates a new instance of this class
*
* @return a RefClass representing a fresh created instance of that class * @return a RefClass representing a fresh created instance of that class
*/ */
public RefClass newInstance() { public RefClass newInstance() {
try { try {
return new RefClass(clazz.newInstance()); return new RefClass(clazz.newInstance());
} catch(Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
return null; return null;
@@ -186,15 +206,18 @@ public class RefClass extends RefModifiers {
/** /**
* Gets the empty (without parameters) constructor of this class if any * Gets the empty (without parameters) constructor of this class if any
*
* @return empty constructor if there, otherwise <code>null</code> * @return empty constructor if there, otherwise <code>null</code>
*/ */
public RefConstructor getConstructor() { public RefConstructor getConstructor() {
return getConstructor(new Class<?>[] { }); return getConstructor(new Class<?>[] {});
} }
/** /**
* Gets a RefConstructor from this class * Gets a RefConstructor from this class
* @param parameters the constructor it's parameters *
* @param parameters
* the constructor it's parameters
* @return the retrieved constructor * @return the retrieved constructor
*/ */
public RefConstructor getConstructor(Class<?>[] parameters) { public RefConstructor getConstructor(Class<?>[] parameters) {
@@ -208,12 +231,13 @@ public class RefClass extends RefModifiers {
/** /**
* Gets all constructors of this class * Gets all constructors of this class
*
* @return an array with all the constructors in this class * @return an array with all the constructors in this class
*/ */
public RefConstructor[] getConstructors() { public RefConstructor[] getConstructors() {
Constructor<?>[] constructors = clazz.getDeclaredConstructors(); Constructor<?>[] constructors = clazz.getDeclaredConstructors();
RefConstructor[] refConstructors = new RefConstructor[constructors.length]; RefConstructor[] refConstructors = new RefConstructor[constructors.length];
for(int i = 0; i < constructors.length; i++) { for (int i = 0; i < constructors.length; i++) {
refConstructors[i] = new RefConstructor(constructors[i]); refConstructors[i] = new RefConstructor(constructors[i]);
} }
return refConstructors; return refConstructors;
@@ -221,20 +245,22 @@ public class RefClass extends RefModifiers {
/** /**
* Gets the class' methods * Gets the class' methods
* @return all methods if an instance is provided, otherwise only static methods *
* @return all methods if an instance is provided, otherwise only static
* methods
*/ */
public RefMethod[] getMethods() { public RefMethod[] getMethods() {
ArrayList<RefMethod> methods = new ArrayList<RefMethod>(); ArrayList<RefMethod> methods = new ArrayList<RefMethod>();
// add all static methods // add all static methods
for(Method m : clazz.getDeclaredMethods()) { for (Method m : clazz.getDeclaredMethods()) {
if(Modifier.isStatic(m.getModifiers())) { if (Modifier.isStatic(m.getModifiers())) {
methods.add(new RefMethod(m, instance)); methods.add(new RefMethod(m, instance));
} }
} }
if(this.instance != null) { if (this.instance != null) {
// add all non static methods // add all non static methods
for(Method m : clazz.getDeclaredMethods()) { for (Method m : clazz.getDeclaredMethods()) {
if(!Modifier.isStatic(m.getModifiers())) { if (!Modifier.isStatic(m.getModifiers())) {
methods.add(new RefMethod(m, instance)); methods.add(new RefMethod(m, instance));
} }
} }
@@ -244,7 +270,9 @@ public class RefClass extends RefModifiers {
/** /**
* Finds and returns the first RefMethod match with given method name * Finds and returns the first RefMethod match with given method name
* @param name method its name *
* @param name
* method its name
* @return the first match, or if not found <code>null</code> * @return the first match, or if not found <code>null</code>
*/ */
public RefMethod getMethod(String name) { public RefMethod getMethod(String name) {
@@ -253,18 +281,21 @@ public class RefClass extends RefModifiers {
/** /**
* Finds a RefMethod in this RefClass * Finds a RefMethod in this RefClass
* @param name the method its name *
* @param parameters the method its parameters * @param name
* the method its name
* @param parameters
* the method its parameters
* @return the matched method or if not found null <code>null</code> * @return the matched method or if not found null <code>null</code>
*/ */
public RefMethod getMethod(String name, Class<?>[] parameters) { public RefMethod getMethod(String name, Class<?>[] parameters) {
try { try {
for(RefMethod method : getMethods()) { for (RefMethod method : getMethods()) {
if(method.getName().equals(name)) { if (method.getName().equals(name)) {
if(parameters == null) { if (parameters == null) {
return method; return method;
} }
if(method.getParameterTypes().equals(parameters)) { if (method.getParameterTypes().equals(parameters)) {
return method; return method;
} }
} }
@@ -276,8 +307,9 @@ public class RefClass extends RefModifiers {
} }
public String toString() { public String toString() {
if(this.instance != null) { if (this.instance != null) {
return new StringBuilder().append(this.instance.toString()).append(" : ").append(this.clazz.toString()).toString(); return new StringBuilder().append(this.instance.toString())
.append(" : ").append(this.clazz.toString()).toString();
} }
return this.clazz.toString(); return this.clazz.toString();
} }
@@ -4,7 +4,8 @@ import java.lang.reflect.Constructor;
/** /**
* *
* A <code>RefConstructor</code> class represent a constructor method of a <code>RefClass</code>. * A <code>RefConstructor</code> class represent a constructor method of a
* <code>RefClass</code>.
* *
* @author Everel * @author Everel
* *
@@ -19,25 +20,28 @@ public class RefConstructor extends RefModifiers {
/** /**
* Creates a new instance of this class by invoking this constructor * Creates a new instance of this class by invoking this constructor
*
* @return the instance of the class * @return the instance of the class
*/ */
public RefClass newInstance() { public RefClass newInstance() {
return newInstance(new Object[] { }); return newInstance(new Object[] {});
} }
/** /**
* Creates a new instance of this class by invoking this constructor * Creates a new instance of this class by invoking this constructor
* @param args the arguments for the constructor *
* @param args
* the arguments for the constructor
* @return the instance of the class * @return the instance of the class
*/ */
public RefClass newInstance(Object... args) { public RefClass newInstance(Object... args) {
if(!constructor.isAccessible()) { if (!constructor.isAccessible()) {
constructor.setAccessible(true); constructor.setAccessible(true);
} }
try { try {
Object instance = constructor.newInstance(args); Object instance = constructor.newInstance(args);
return new RefClass(instance); return new RefClass(instance);
} catch(Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
return null; return null;
@@ -53,11 +57,11 @@ public class RefConstructor extends RefModifiers {
} }
/** /**
* Returns <code>true</code> if this constructor is a synthetic constructor; returns * Returns <code>true</code> if this constructor is a synthetic constructor;
* <code>false</code> otherwise. * returns <code>false</code> otherwise.
* *
* @return <code>true</code> if this constructor is a synthetic constructor; returns * @return <code>true</code> if this constructor is a synthetic constructor;
* <code>false</code> otherwise * returns <code>false</code> otherwise
*/ */
public boolean isSynthetic() { public boolean isSynthetic() {
return constructor.isSynthetic(); return constructor.isSynthetic();
@@ -74,6 +78,7 @@ public class RefConstructor extends RefModifiers {
/** /**
* Returns an array of the parameter types of this constructor * Returns an array of the parameter types of this constructor
*
* @return an array of the parameter types of this constructor * @return an array of the parameter types of this constructor
*/ */
public Class<?>[] getParameterTypes() { public Class<?>[] getParameterTypes() {
@@ -5,7 +5,7 @@ import java.lang.reflect.Type;
/** /**
* *
* A RefField represents a field in a class * A <code>RefField</code> represents a field in a <code>RefClass</code>
* *
* @author Everel * @author Everel
* *
@@ -35,7 +35,8 @@ public class RefMethod extends RefModifiers {
/** /**
* Determines if this method is a bridge method. * Determines if this method is a bridge method.
* *
* @return <code>true</code> if this method is a bridge method, otherwise <code>false</code> * @return <code>true</code> if this method is a bridge method, otherwise
* <code>false</code>
*/ */
public boolean isBridge() { public boolean isBridge() {
return method.isBridge(); return method.isBridge();
@@ -43,7 +44,9 @@ public class RefMethod extends RefModifiers {
/** /**
* Determines if this method can take a variable amount of arguments * Determines if this method can take a variable amount of arguments
* @return <code>true</code> if this method can take a variable amount of arguments *
* @return <code>true</code> if this method can take a variable amount of
* arguments
*/ */
public boolean isVarArgs() { public boolean isVarArgs() {
return method.isVarArgs(); return method.isVarArgs();
@@ -71,6 +74,7 @@ public class RefMethod extends RefModifiers {
/** /**
* Returns an array of the parameter types of this method * Returns an array of the parameter types of this method
*
* @return an array of the parameter types of this method * @return an array of the parameter types of this method
*/ */
public Class<?>[] getParameterTypes() { public Class<?>[] getParameterTypes() {
@@ -79,6 +83,7 @@ public class RefMethod extends RefModifiers {
/** /**
* Gets the return type of this class * Gets the return type of this class
*
* @return return type of this class * @return return type of this class
*/ */
public Class<?> getReturnType() { public Class<?> getReturnType() {
@@ -87,6 +92,7 @@ public class RefMethod extends RefModifiers {
/** /**
* Gets the return type of this class * Gets the return type of this class
*
* @return return type of this class * @return return type of this class
*/ */
public org.objectweb.asm.Type getASMReturnType() { public org.objectweb.asm.Type getASMReturnType() {
@@ -108,19 +114,21 @@ public class RefMethod extends RefModifiers {
* @return object returned by the method * @return object returned by the method
*/ */
public Object invoke() { public Object invoke() {
return invoke(new Object[] { }); return invoke(new Object[] {});
} }
/** /**
* *
* Invokes the method and returns it returned object * Invokes the method and returns it returned object
* *
* @param args arguments for the invokable method * @param args
* arguments for the invokable method
* @return object returned by the method * @return object returned by the method
*/ */
public Object invoke(Object... args) { public Object invoke(Object... args) {
if(!isStatic() && instance == null) { if (!isStatic() && instance == null) {
throw new IllegalStateException("Can not invoke non static method without an instance."); throw new IllegalStateException(
"Can not invoke non static method without an instance.");
} }
try { try {
Object retObject = method.invoke(instance, args); Object retObject = method.invoke(instance, args);