svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / obj / InvokeMethodFunction.java @ 43939
History | View | Annotate | Download (2.21 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.obj; |
---|---|
2 |
|
3 |
import java.lang.reflect.Method; |
4 |
import org.apache.commons.lang3.Range; |
5 |
import org.gvsig.expressionevaluator.Function; |
6 |
import org.gvsig.expressionevaluator.Interpreter; |
7 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
8 |
|
9 |
public class InvokeMethodFunction extends AbstractFunction { |
10 |
|
11 |
private final Object obj; |
12 |
private final String methodname; |
13 |
|
14 |
public InvokeMethodFunction(Object obj, String methodName) { |
15 |
super(
|
16 |
Function.GROUP_OTHER, "invokeMethod", Range.between(0, Integer.MAX_VALUE) |
17 |
); |
18 |
this.obj = obj;
|
19 |
this.methodname = methodName;
|
20 |
} |
21 |
|
22 |
public InvokeMethodFunction() {
|
23 |
super(
|
24 |
Function.GROUP_OTHER, "invokeMethod", Range.between(2, Integer.MAX_VALUE) |
25 |
); |
26 |
this.obj = null; |
27 |
this.methodname = null; |
28 |
} |
29 |
|
30 |
@Override
|
31 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
32 |
Object theObj = this.obj; |
33 |
String theMethodName = this.methodname; |
34 |
int firstArg = 0; |
35 |
if( theObj==null && theMethodName==null ) { |
36 |
theObj = getObject(args, 0);
|
37 |
theMethodName = getStr(args, 1);
|
38 |
firstArg = 2;
|
39 |
} |
40 |
if( theMethodName == null ) { |
41 |
throw new NullPointerException("A method name was expected to invoke and a null was received"); |
42 |
} |
43 |
if( theObj==null ) { |
44 |
throw new NullPointerException("An object pointer was expected to invoke method "+theMethodName+" and a null was received"); |
45 |
} |
46 |
Class[] parameterTypes = new Class[args.length-firstArg]; |
47 |
Object[] parameters = new Object[args.length-firstArg]; |
48 |
for (int i = 0; i < parameters.length; i++) { |
49 |
Object parameter = args[i+firstArg];
|
50 |
parameters[i] = parameter; |
51 |
if( parameter==null ) { |
52 |
parameterTypes[i] = null;
|
53 |
} else {
|
54 |
parameterTypes[i] = parameter.getClass(); |
55 |
} |
56 |
} |
57 |
Class<?> theClass = theObj.getClass();
|
58 |
Method method = theClass.getMethod(theMethodName, parameterTypes);
|
59 |
Object value = method.invoke(theObj, parameters);
|
60 |
return value;
|
61 |
} |
62 |
|
63 |
} |