RefMethod updates

This commit is contained in:
Clisprail
2014-08-05 20:05:09 +02:00
parent e093daeaeb
commit be646c5105
2 changed files with 110 additions and 0 deletions
@@ -2,6 +2,7 @@ package org.parabot.core.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -218,6 +219,62 @@ public class RefClass extends RefModifiers {
return refConstructors;
}
/**
* Gets the class' 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())) {
methods.add(new RefMethod(m, instance));
}
}
if(this.instance != null) {
// add all non static methods
for(Method m : clazz.getDeclaredMethods()) {
if(!Modifier.isStatic(m.getModifiers())) {
methods.add(new RefMethod(m, instance));
}
}
}
return methods.toArray(new RefMethod[methods.size()]);
}
/**
* Finds and returns the first RefMethod match with given method name
* @param name method its name
* @return the first match, or if not found <code>null</code>
*/
public RefMethod getMethod(String name) {
return getMethod(name, null);
}
/**
* Finds a RefMethod in this RefClass
* @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) {
return method;
}
if(method.getParameterTypes().equals(parameters)) {
return method;
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public String toString() {
if(this.instance != null) {
return new StringBuilder().append(this.instance.toString()).append(" : ").append(this.clazz.toString()).toString();
@@ -11,9 +11,16 @@ import java.lang.reflect.Method;
*/
public class RefMethod extends RefModifiers {
private Method method;
private Object instance;
public RefMethod(Method method) {
this(method, null);
}
public RefMethod(Method method, Object instance) {
super(method.getModifiers());
this.method = method;
this.instance = instance;
}
/**
@@ -24,6 +31,23 @@ public class RefMethod extends RefModifiers {
public boolean isAccessible() {
return method.isAccessible();
}
/**
* Determines if this method is a bridge method.
*
* @return <code>true</code> if this method is a bridge method, otherwise <code>false</code>
*/
public boolean isBridge() {
return method.isBridge();
}
/**
* 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
*/
public boolean isVarArgs() {
return method.isVarArgs();
}
/**
* Returns <code>true</code> if this method is a synthetic method; returns
@@ -78,6 +102,35 @@ public class RefMethod extends RefModifiers {
return this.method;
}
/**
* Invokes the method and returns it returned object
*
* @return object returned by the method
*/
public Object invoke() {
return invoke(new Object[] { });
}
/**
*
* Invokes the method and returns it returned object
*
* @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.");
}
try {
Object retObject = method.invoke(instance, args);
return retObject;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public String toGenericString() {
return method.toGenericString();
}