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 / CallStaticMethodFunction.java @ 44207
History | View | Annotate | Download (1.97 KB)
1 | 44138 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | 43939 | jjdelcerro | import java.lang.reflect.Method; |
4 | import org.apache.commons.lang3.Range; |
||
5 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
6 | import org.gvsig.expressionevaluator.Interpreter; |
||
7 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
8 | 43521 | jjdelcerro | |
9 | 44138 | jjdelcerro | public class CallStaticMethodFunction extends AbstractFunction { |
10 | 43512 | jjdelcerro | |
11 | 44138 | jjdelcerro | public static final String NAME = "CALL_STATIC"; |
12 | 43939 | jjdelcerro | |
13 | 44138 | jjdelcerro | public CallStaticMethodFunction() {
|
14 | 43939 | jjdelcerro | super(
|
15 | 44138 | jjdelcerro | Function.GROUP_PROGRAMMING, |
16 | NAME, |
||
17 | 43939 | jjdelcerro | Range.between(2, Integer.MAX_VALUE), |
18 | 44138 | jjdelcerro | "The "+NAME+"() function call a static mehod of a class.", |
19 | NAME+"( className, methodName, arguments... )",
|
||
20 | 44098 | jjdelcerro | null,
|
21 | "Object",
|
||
22 | false
|
||
23 | 43939 | jjdelcerro | ); |
24 | 43512 | jjdelcerro | } |
25 | 43939 | jjdelcerro | |
26 | 43512 | jjdelcerro | @Override
|
27 | 43939 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
28 | String theClassName = getStr(args,0); |
||
29 | String theMethodName = getStr(args,1); |
||
30 | int firstArg = 1; |
||
31 | if( theClassName == null ) { |
||
32 | 44038 | jjdelcerro | throw new NullPointerException("A class name name was expected to invoke and a null class name was received"); |
33 | 43512 | jjdelcerro | } |
34 | 43939 | jjdelcerro | if( theMethodName == null ) { |
35 | throw new NullPointerException("A method name was expected to invoke and a null methd name was received"); |
||
36 | 43512 | jjdelcerro | } |
37 | 43939 | jjdelcerro | Class[] parameterTypes = new Class[args.length-firstArg]; |
38 | Object[] parameters = new Object[args.length-firstArg]; |
||
39 | for (int i = 0; i < parameters.length; i++) { |
||
40 | Object parameter = args[i+firstArg];
|
||
41 | parameters[i] = parameter; |
||
42 | if( parameter==null ) { |
||
43 | parameterTypes[i] = null;
|
||
44 | } else {
|
||
45 | parameterTypes[i] = parameter.getClass(); |
||
46 | } |
||
47 | 43512 | jjdelcerro | } |
48 | 43939 | jjdelcerro | Class<?> theClass = Class.forName(theClassName); |
49 | Method method = theClass.getMethod(theMethodName, parameterTypes);
|
||
50 | Object value = method.invoke(null, parameters); |
||
51 | return value;
|
||
52 | 43512 | jjdelcerro | } |
53 | |||
54 | } |