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 / InvokeStaticMethodFunction.java @ 43939
History | View | Annotate | Download (1.89 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 InvokeStaticMethodFunction extends AbstractFunction { |
10 |
|
11 |
|
12 |
public InvokeStaticMethodFunction() {
|
13 |
super(
|
14 |
Function.GROUP_OTHER, |
15 |
"invokeStaticMethod",
|
16 |
Range.between(2, Integer.MAX_VALUE), |
17 |
"The InvokeStaticMethod() function call a static mehod of a class.",
|
18 |
"InvokeStaticMethod( className, methodName, arguments... )"
|
19 |
); |
20 |
} |
21 |
|
22 |
@Override
|
23 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
24 |
String theClassName = getStr(args,0); |
25 |
String theMethodName = getStr(args,1); |
26 |
int firstArg = 1; |
27 |
if( theClassName == null ) { |
28 |
throw new NullPointerException("A method name was expected to invoke and a null class name was received"); |
29 |
} |
30 |
if( theMethodName == null ) { |
31 |
throw new NullPointerException("A method name was expected to invoke and a null methd name was received"); |
32 |
} |
33 |
Class[] parameterTypes = new Class[args.length-firstArg]; |
34 |
Object[] parameters = new Object[args.length-firstArg]; |
35 |
for (int i = 0; i < parameters.length; i++) { |
36 |
Object parameter = args[i+firstArg];
|
37 |
parameters[i] = parameter; |
38 |
if( parameter==null ) { |
39 |
parameterTypes[i] = null;
|
40 |
} else {
|
41 |
parameterTypes[i] = parameter.getClass(); |
42 |
} |
43 |
} |
44 |
Class<?> theClass = Class.forName(theClassName); |
45 |
Method method = theClass.getMethod(theMethodName, parameterTypes);
|
46 |
Object value = method.invoke(null, parameters); |
47 |
return value;
|
48 |
} |
49 |
|
50 |
} |