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 / obj / InvokeStaticMethodFunction.java @ 44098

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