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
* *
@@ -26,7 +26,7 @@ public class RefClass extends RefModifiers {
public RefClass(Object instance) { public RefClass(Object instance) {
this(instance.getClass(), instance); this(instance.getClass(), instance);
} }
public RefClass(Class<?> clazz, Object instance) { public RefClass(Class<?> clazz, Object instance) {
super(clazz.getModifiers()); super(clazz.getModifiers());
this.clazz = clazz; this.clazz = clazz;
@@ -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;
} }
@@ -50,16 +53,17 @@ public class RefClass extends RefModifiers {
} }
this.instance = instance; this.instance = instance;
} }
/** /**
* 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;
} }
/** /**
* Gets the class which this RefClass is representing * Gets the class which this RefClass is representing
* *
@@ -68,133 +72,152 @@ public class RefClass extends RefModifiers {
public Class<?> getRepresentingClass() { public Class<?> getRepresentingClass() {
return this.clazz; return this.clazz;
} }
public String getClassName() { public String getClassName() {
return this.clazz.getName(); return this.clazz.getName();
} }
public String getSimpleName() { public String getSimpleName() {
return this.clazz.getSimpleName(); return this.clazz.getSimpleName();
} }
public String getCanonicalName() { public String getCanonicalName() {
return this.clazz.getCanonicalName(); return this.clazz.getCanonicalName();
} }
/** /**
* 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() {
return org.objectweb.asm.Type.getType(this.clazz); return org.objectweb.asm.Type.getType(this.clazz);
} }
/** /**
* 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));
} }
} }
} }
return fields.toArray(new RefField[fields.size()]); return fields.toArray(new RefField[fields.size()]);
} }
/** /**
* 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) {
return getField(name, null); return getField(name, null);
} }
/** /**
* 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;
} }
} }
} }
return null; return null;
} }
/** /**
* 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);
} }
/** /**
* 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();
return !superClass.equals(Object.class); return !superClass.equals(Object.class);
} }
/** /**
* 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() {
return new RefClass(clazz.getSuperclass(), instance); return new RefClass(clazz.getSuperclass(), instance);
} }
/** /**
* 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;
} }
/** /**
* 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) {
@@ -205,66 +228,74 @@ public class RefClass extends RefModifiers {
} }
return null; return null;
} }
/** /**
* 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;
} }
/** /**
* 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));
} }
} }
} }
return methods.toArray(new RefMethod[methods.size()]); return methods.toArray(new RefMethod[methods.size()]);
} }
/** /**
* 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) {
return getMethod(name, null); return getMethod(name, null);
} }
/** /**
* 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;
} }
} }
@@ -274,10 +305,11 @@ public class RefClass extends RefModifiers {
} }
return null; return null;
} }
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,45 +4,49 @@ 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
* *
*/ */
public class RefConstructor extends RefModifiers { public class RefConstructor extends RefModifiers {
private Constructor<?> constructor; private Constructor<?> constructor;
public RefConstructor(Constructor<?> constructor) { public RefConstructor(Constructor<?> constructor) {
super(constructor.getModifiers()); super(constructor.getModifiers());
this.constructor = constructor; this.constructor = constructor;
} }
/** /**
* 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;
} }
/** /**
* Get the value of the accessible flag for this object. * Get the value of the accessible flag for this object.
* *
@@ -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();
@@ -71,9 +75,10 @@ public class RefConstructor extends RefModifiers {
public String getName() { public String getName() {
return constructor.getName(); return constructor.getName();
} }
/** /**
* 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() {
@@ -88,11 +93,11 @@ public class RefConstructor extends RefModifiers {
public Constructor<?> getConstructor() { public Constructor<?> getConstructor() {
return this.constructor; return this.constructor;
} }
public String toGenericString() { public String toGenericString() {
return constructor.toGenericString(); return constructor.toGenericString();
} }
public String toString() { public String toString() {
return constructor.toString(); return constructor.toString();
} }
@@ -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
* *
@@ -7,7 +7,7 @@ import java.lang.reflect.Method;
* A <code>RefMethod</code> class represent a method of a <code>RefClass</code>. * A <code>RefMethod</code> class represent a method of a <code>RefClass</code>.
* *
* @author Everel * @author Everel
* *
*/ */
public class RefMethod extends RefModifiers { public class RefMethod extends RefModifiers {
private Method method; private Method method;
@@ -16,13 +16,13 @@ public class RefMethod extends RefModifiers {
public RefMethod(Method method) { public RefMethod(Method method) {
this(method, null); this(method, null);
} }
public RefMethod(Method method, Object instance) { public RefMethod(Method method, Object instance) {
super(method.getModifiers()); super(method.getModifiers());
this.method = method; this.method = method;
this.instance = instance; this.instance = instance;
} }
/** /**
* Get the value of the accessible flag for this object. * Get the value of the accessible flag for this object.
* *
@@ -31,19 +31,22 @@ public class RefMethod extends RefModifiers {
public boolean isAccessible() { public boolean isAccessible() {
return method.isAccessible(); return method.isAccessible();
} }
/** /**
* 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();
} }
/** /**
* 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();
@@ -68,25 +71,28 @@ public class RefMethod extends RefModifiers {
public String getName() { public String getName() {
return method.getName(); return method.getName();
} }
/** /**
* 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() {
return method.getParameterTypes(); return method.getParameterTypes();
} }
/** /**
* 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() {
return method.getReturnType(); return method.getReturnType();
} }
/** /**
* 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() {
@@ -101,26 +107,28 @@ public class RefMethod extends RefModifiers {
public Method getMethod() { public Method getMethod() {
return this.method; return this.method;
} }
/** /**
* Invokes the method and returns it returned object * Invokes the method and returns it returned object
* *
* @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);
@@ -130,11 +138,11 @@ public class RefMethod extends RefModifiers {
} }
return null; return null;
} }
public String toGenericString() { public String toGenericString() {
return method.toGenericString(); return method.toGenericString();
} }
public String toString() { public String toString() {
return method.toString(); return method.toString();
} }
@@ -5,71 +5,71 @@ import java.lang.reflect.Modifier;
/** /**
* *
* @author Everel * @author Everel
* *
*/ */
public class RefModifiers { public class RefModifiers {
private int modifiers; private int modifiers;
public RefModifiers() { public RefModifiers() {
} }
public RefModifiers(int modifiers) { public RefModifiers(int modifiers) {
setModifiers(modifiers); setModifiers(modifiers);
} }
public void setModifiers(int modifiers) { public void setModifiers(int modifiers) {
this.modifiers = modifiers; this.modifiers = modifiers;
} }
public int getModifiers() { public int getModifiers() {
return this.modifiers; return this.modifiers;
} }
public boolean isStatic() { public boolean isStatic() {
return Modifier.isStatic(modifiers); return Modifier.isStatic(modifiers);
} }
public boolean isAbstract() { public boolean isAbstract() {
return Modifier.isAbstract(modifiers); return Modifier.isAbstract(modifiers);
} }
public boolean isFinal() { public boolean isFinal() {
return Modifier.isFinal(modifiers); return Modifier.isFinal(modifiers);
} }
public boolean isInterface() { public boolean isInterface() {
return Modifier.isInterface(modifiers); return Modifier.isInterface(modifiers);
} }
public boolean isNative() { public boolean isNative() {
return Modifier.isNative(modifiers); return Modifier.isNative(modifiers);
} }
public boolean isPrivate() { public boolean isPrivate() {
return Modifier.isPrivate(modifiers); return Modifier.isPrivate(modifiers);
} }
public boolean isProtected() { public boolean isProtected() {
return Modifier.isProtected(modifiers); return Modifier.isProtected(modifiers);
} }
public boolean isPublic() { public boolean isPublic() {
return Modifier.isPublic(modifiers); return Modifier.isPublic(modifiers);
} }
public boolean isStrict() { public boolean isStrict() {
return Modifier.isStrict(modifiers); return Modifier.isStrict(modifiers);
} }
public boolean isSynchronized() { public boolean isSynchronized() {
return Modifier.isSynchronized(modifiers); return Modifier.isSynchronized(modifiers);
} }
public boolean isTransient() { public boolean isTransient() {
return Modifier.isTransient(modifiers); return Modifier.isTransient(modifiers);
} }
public boolean isVolatile() { public boolean isVolatile() {
return Modifier.isVolatile(modifiers); return Modifier.isVolatile(modifiers);
} }
@@ -7,7 +7,7 @@ import java.util.Set;
/** /**
* *
* @author Everel * @author Everel
* *
*/ */
public class RefUtils { public class RefUtils {