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 / programming / CallMethodFunction.java @ 44207
History | View | Annotate | Download (2.47 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
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 CallMethodFunction extends AbstractFunction { |
10 |
|
11 |
public static final String NAME = "CALL_METHOD"; |
12 |
|
13 |
private final Object obj; |
14 |
private final String methodname; |
15 |
|
16 |
public CallMethodFunction(Object obj, String methodName) { |
17 |
super(Function.GROUP_PROGRAMMING,
|
18 |
NAME, |
19 |
Range.between(0, Integer.MAX_VALUE), |
20 |
null,
|
21 |
null,
|
22 |
null,
|
23 |
"Object",
|
24 |
false
|
25 |
); |
26 |
this.obj = obj;
|
27 |
this.methodname = methodName;
|
28 |
} |
29 |
|
30 |
public CallMethodFunction() {
|
31 |
super(Function.GROUP_PROGRAMMING,
|
32 |
NAME, |
33 |
Range.between(0, Integer.MAX_VALUE), |
34 |
null,
|
35 |
null,
|
36 |
null,
|
37 |
"Object",
|
38 |
false
|
39 |
); |
40 |
this.obj = null; |
41 |
this.methodname = null; |
42 |
} |
43 |
|
44 |
@Override
|
45 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
46 |
Object theObj = this.obj; |
47 |
String theMethodName = this.methodname; |
48 |
int firstArg = 0; |
49 |
if( theObj==null && theMethodName==null ) { |
50 |
theObj = getObject(args, 0);
|
51 |
theMethodName = getStr(args, 1);
|
52 |
firstArg = 2;
|
53 |
} |
54 |
if( theMethodName == null ) { |
55 |
throw new NullPointerException("A method name was expected to invoke and a null was received"); |
56 |
} |
57 |
if( theObj==null ) { |
58 |
throw new NullPointerException("An object pointer was expected to invoke method "+theMethodName+" and a null was received"); |
59 |
} |
60 |
Class[] parameterTypes = new Class[args.length-firstArg]; |
61 |
Object[] parameters = new Object[args.length-firstArg]; |
62 |
for (int i = 0; i < parameters.length; i++) { |
63 |
Object parameter = args[i+firstArg];
|
64 |
parameters[i] = parameter; |
65 |
if( parameter==null ) { |
66 |
parameterTypes[i] = null;
|
67 |
} else {
|
68 |
parameterTypes[i] = parameter.getClass(); |
69 |
} |
70 |
} |
71 |
Class<?> theClass = theObj.getClass();
|
72 |
Method method = theClass.getMethod(theMethodName, parameterTypes);
|
73 |
Object value = method.invoke(theObj, parameters);
|
74 |
return value;
|
75 |
} |
76 |
|
77 |
} |