Statistics
| Revision:

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
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 CallStaticMethodFunction extends AbstractFunction {
10

    
11
    public static final String NAME = "CALL_STATIC";
12
    
13
    public CallStaticMethodFunction() {
14
        super(
15
            Function.GROUP_PROGRAMMING, 
16
            NAME, 
17
            Range.between(2, Integer.MAX_VALUE),
18
            "The "+NAME+"() function call a static mehod of a class.",
19
            NAME+"( className, methodName, arguments... )",
20
            null,
21
            "Object",
22
            false
23
        );
24
    }
25
    
26
    @Override
27
    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
            throw new NullPointerException("A class name name was expected to invoke and a null class name was received");
33
        }
34
        if( theMethodName == null ) {
35
            throw new NullPointerException("A method name was expected to invoke and a null methd name was received");
36
        }
37
        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
        }
48
        Class<?> theClass = Class.forName(theClassName);
49
        Method method = theClass.getMethod(theMethodName, parameterTypes);
50
        Object value = method.invoke(null, parameters);
51
        return value;
52
    }
53
    
54
}