mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-07 16:49:10 +00:00
RefMethod updates
This commit is contained in:
@@ -2,6 +2,7 @@ package org.parabot.core.reflect;
|
|||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@@ -218,6 +219,62 @@ public class RefClass extends RefModifiers {
|
|||||||
return refConstructors;
|
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() {
|
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();
|
||||||
|
|||||||
@@ -11,9 +11,16 @@ import java.lang.reflect.Method;
|
|||||||
*/
|
*/
|
||||||
public class RefMethod extends RefModifiers {
|
public class RefMethod extends RefModifiers {
|
||||||
private Method method;
|
private Method method;
|
||||||
|
private Object instance;
|
||||||
|
|
||||||
public RefMethod(Method method) {
|
public RefMethod(Method method) {
|
||||||
|
this(method, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RefMethod(Method method, Object instance) {
|
||||||
super(method.getModifiers());
|
super(method.getModifiers());
|
||||||
|
this.method = method;
|
||||||
|
this.instance = instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,6 +32,23 @@ public class RefMethod extends RefModifiers {
|
|||||||
return method.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
|
* Returns <code>true</code> if this method is a synthetic method; returns
|
||||||
* <code>false</code> otherwise.
|
* <code>false</code> otherwise.
|
||||||
@@ -78,6 +102,35 @@ public class RefMethod extends RefModifiers {
|
|||||||
return this.method;
|
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() {
|
public String toGenericString() {
|
||||||
return method.toGenericString();
|
return method.toGenericString();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user