From be646c5105170fa79594822373bb09deb70af2d4 Mon Sep 17 00:00:00 2001 From: Clisprail Date: Tue, 5 Aug 2014 20:05:09 +0200 Subject: [PATCH] RefMethod updates --- .../org/parabot/core/reflect/RefClass.java | 57 +++++++++++++++++++ .../org/parabot/core/reflect/RefMethod.java | 53 +++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/parabotv2/src/org/parabot/core/reflect/RefClass.java b/parabotv2/src/org/parabot/core/reflect/RefClass.java index b40b967..1723d3f 100644 --- a/parabotv2/src/org/parabot/core/reflect/RefClass.java +++ b/parabotv2/src/org/parabot/core/reflect/RefClass.java @@ -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 methods = new ArrayList(); + // 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 null + */ + 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 null + */ + 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(); diff --git a/parabotv2/src/org/parabot/core/reflect/RefMethod.java b/parabotv2/src/org/parabot/core/reflect/RefMethod.java index 86516f0..c15b572 100644 --- a/parabotv2/src/org/parabot/core/reflect/RefMethod.java +++ b/parabotv2/src/org/parabot/core/reflect/RefMethod.java @@ -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 true if this method is a bridge method, otherwise false + */ + public boolean isBridge() { + return method.isBridge(); + } + + /** + * Determines if this method can take a variable amount of arguments + * @return true if this method can take a variable amount of arguments + */ + public boolean isVarArgs() { + return method.isVarArgs(); + } /** * Returns true 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(); }