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
* provided this class can only get values from static fields and only invoke
* static methods
* A <code>RefClass</code> 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
*
@@ -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
* @param instance instance of this class.
* 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) {
if (instance == null) {
this.instance = null;
return;
}
@@ -54,7 +57,8 @@ public class RefClass extends RefModifiers {
/**
* 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() {
return this.instance;
@@ -83,6 +87,7 @@ public class RefClass extends RefModifiers {
/**
* Gets the type of this class
*
* @return type of this class
*/
public org.objectweb.asm.Type getASMType() {
@@ -91,20 +96,22 @@ public class RefClass extends RefModifiers {
/**
* 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() {
ArrayList<RefField> fields = new ArrayList<RefField>();
// add all static fields
for(Field f : clazz.getDeclaredFields()) {
if(Modifier.isStatic(f.getModifiers())) {
for (Field f : clazz.getDeclaredFields()) {
if (Modifier.isStatic(f.getModifiers())) {
fields.add(new RefField(f, instance));
}
}
if(this.instance != null) {
if (this.instance != null) {
// add all non static fields
for(Field f : clazz.getDeclaredFields()) {
if(!Modifier.isStatic(f.getModifiers())) {
for (Field f : clazz.getDeclaredFields()) {
if (!Modifier.isStatic(f.getModifiers())) {
fields.add(new RefField(f, instance));
}
}
@@ -114,7 +121,9 @@ public class RefClass extends RefModifiers {
/**
* Gets field by field name
* @param name name of the field
*
* @param name
* name of the field
* @return the field if found
*/
public RefField getField(String name) {
@@ -123,18 +132,21 @@ public class RefClass extends RefModifiers {
/**
* 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
*/
public RefField getField(String name, String desc) {
RefField[] fields = getFields();
for(RefField f : fields) {
if(f.getName().equals(name)) {
if(desc == null) {
for (RefField f : fields) {
if (f.getName().equals(name)) {
if (desc == null) {
return f;
}
if(desc.equals(f.getTypeDesc())) {
if (desc.equals(f.getTypeDesc())) {
return f;
}
}
@@ -144,7 +156,9 @@ public class RefClass extends RefModifiers {
/**
* 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() {
return hasSuperclass(true);
@@ -152,11 +166,15 @@ public class RefClass extends RefModifiers {
/**
* 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) {
if(!ignoreObjectClass) {
if (!ignoreObjectClass) {
return !clazz.equals(Object.class);
}
Class<?> superClass = clazz.getSuperclass();
@@ -165,6 +183,7 @@ public class RefClass extends RefModifiers {
/**
* Returns a new RefClass representing the superclass of this RefClass
*
* @return superclass of this RefClass
*/
public RefClass getSuperclass() {
@@ -173,12 +192,13 @@ public class RefClass extends RefModifiers {
/**
* Creates a new instance of this class
*
* @return a RefClass representing a fresh created instance of that class
*/
public RefClass newInstance() {
try {
return new RefClass(clazz.newInstance());
} catch(Throwable t) {
} catch (Throwable t) {
t.printStackTrace();
}
return null;
@@ -186,15 +206,18 @@ public class RefClass extends RefModifiers {
/**
* Gets the empty (without parameters) constructor of this class if any
*
* @return empty constructor if there, otherwise <code>null</code>
*/
public RefConstructor getConstructor() {
return getConstructor(new Class<?>[] { });
return getConstructor(new 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
*/
public RefConstructor getConstructor(Class<?>[] parameters) {
@@ -208,12 +231,13 @@ public class RefClass extends RefModifiers {
/**
* Gets all constructors of this class
*
* @return an array with all the constructors in this class
*/
public RefConstructor[] getConstructors() {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
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]);
}
return refConstructors;
@@ -221,20 +245,22 @@ public class RefClass extends RefModifiers {
/**
* 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() {
ArrayList<RefMethod> methods = new ArrayList<RefMethod>();
// add all static methods
for(Method m : clazz.getDeclaredMethods()) {
if(Modifier.isStatic(m.getModifiers())) {
for (Method m : clazz.getDeclaredMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
methods.add(new RefMethod(m, instance));
}
}
if(this.instance != null) {
if (this.instance != null) {
// add all non static methods
for(Method m : clazz.getDeclaredMethods()) {
if(!Modifier.isStatic(m.getModifiers())) {
for (Method m : clazz.getDeclaredMethods()) {
if (!Modifier.isStatic(m.getModifiers())) {
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
* @param name method its name
*
* @param name
* method its name
* @return the first match, or if not found <code>null</code>
*/
public RefMethod getMethod(String name) {
@@ -253,18 +281,21 @@ public class RefClass extends RefModifiers {
/**
* 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>
*/
public RefMethod getMethod(String name, Class<?>[] parameters) {
try {
for(RefMethod method : getMethods()) {
if(method.getName().equals(name)) {
if(parameters == null) {
for (RefMethod method : getMethods()) {
if (method.getName().equals(name)) {
if (parameters == null) {
return method;
}
if(method.getParameterTypes().equals(parameters)) {
if (method.getParameterTypes().equals(parameters)) {
return method;
}
}
@@ -276,8 +307,9 @@ public class RefClass extends RefModifiers {
}
public String toString() {
if(this.instance != null) {
return new StringBuilder().append(this.instance.toString()).append(" : ").append(this.clazz.toString()).toString();
if (this.instance != null) {
return new StringBuilder().append(this.instance.toString())
.append(" : ").append(this.clazz.toString()).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
*
@@ -19,25 +20,28 @@ public class RefConstructor extends RefModifiers {
/**
* Creates a new instance of this class by invoking this constructor
*
* @return the instance of the class
*/
public RefClass newInstance() {
return newInstance(new Object[] { });
return newInstance(new Object[] {});
}
/**
* 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
*/
public RefClass newInstance(Object... args) {
if(!constructor.isAccessible()) {
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
try {
Object instance = constructor.newInstance(args);
return new RefClass(instance);
} catch(Throwable t) {
} catch (Throwable t) {
t.printStackTrace();
}
return null;
@@ -53,11 +57,11 @@ public class RefConstructor extends RefModifiers {
}
/**
* Returns <code>true</code> if this constructor is a synthetic constructor; returns
* <code>false</code> otherwise.
* Returns <code>true</code> if this constructor is a synthetic constructor;
* returns <code>false</code> otherwise.
*
* @return <code>true</code> if this constructor is a synthetic constructor; returns
* <code>false</code> otherwise
* @return <code>true</code> if this constructor is a synthetic constructor;
* returns <code>false</code> otherwise
*/
public boolean isSynthetic() {
return constructor.isSynthetic();
@@ -74,6 +78,7 @@ public class RefConstructor extends RefModifiers {
/**
* Returns an array of the parameter types of this constructor
*
* @return an array of the parameter types of this constructor
*/
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
*
@@ -35,7 +35,8 @@ public class RefMethod extends RefModifiers {
/**
* 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() {
return method.isBridge();
@@ -43,7 +44,9 @@ public class RefMethod extends RefModifiers {
/**
* 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() {
return method.isVarArgs();
@@ -71,6 +74,7 @@ public class RefMethod extends RefModifiers {
/**
* Returns an array of the parameter types of this method
*
* @return an array of the parameter types of this method
*/
public Class<?>[] getParameterTypes() {
@@ -79,6 +83,7 @@ public class RefMethod extends RefModifiers {
/**
* Gets the return type of this class
*
* @return return type of this class
*/
public Class<?> getReturnType() {
@@ -87,6 +92,7 @@ public class RefMethod extends RefModifiers {
/**
* Gets the return type of this class
*
* @return return type of this class
*/
public org.objectweb.asm.Type getASMReturnType() {
@@ -108,19 +114,21 @@ public class RefMethod extends RefModifiers {
* @return object returned by the method
*/
public Object invoke() {
return invoke(new Object[] { });
return invoke(new 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
*/
public Object invoke(Object... args) {
if(!isStatic() && instance == null) {
throw new IllegalStateException("Can not invoke non static method without an instance.");
if (!isStatic() && instance == null) {
throw new IllegalStateException(
"Can not invoke non static method without an instance.");
}
try {
Object retObject = method.invoke(instance, args);