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 / CallMethodFunction.java @ 44207

History | View | Annotate | Download (2.47 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 CallMethodFunction extends AbstractFunction {
10 43512 jjdelcerro
11 44138 jjdelcerro
    public static final String NAME = "CALL_METHOD";
12
13 43939 jjdelcerro
    private final Object obj;
14
    private final String methodname;
15
16 44138 jjdelcerro
    public CallMethodFunction(Object obj, String methodName) {
17
        super(Function.GROUP_PROGRAMMING,
18
            NAME,
19 44098 jjdelcerro
            Range.between(0, Integer.MAX_VALUE),
20
            null,
21
            null,
22
            null,
23
            "Object",
24
            false
25 43939 jjdelcerro
        );
26
        this.obj = obj;
27
        this.methodname = methodName;
28 43512 jjdelcerro
    }
29 43939 jjdelcerro
30 44138 jjdelcerro
    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 43939 jjdelcerro
        );
40
        this.obj = null;
41
        this.methodname = null;
42
    }
43
44 43512 jjdelcerro
    @Override
45 43939 jjdelcerro
    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 43512 jjdelcerro
        }
54 43939 jjdelcerro
        if( theMethodName == null ) {
55
            throw new NullPointerException("A method name was expected to invoke and a null was received");
56 43512 jjdelcerro
        }
57 43939 jjdelcerro
        if( theObj==null ) {
58
            throw new NullPointerException("An object pointer was expected to invoke method "+theMethodName+" and a null was received");
59 43512 jjdelcerro
        }
60 43939 jjdelcerro
        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 43512 jjdelcerro
        }
71 43939 jjdelcerro
        Class<?> theClass = theObj.getClass();
72
        Method method = theClass.getMethod(theMethodName, parameterTypes);
73
        Object value = method.invoke(theObj, parameters);
74
        return value;
75 43512 jjdelcerro
    }
76
77
}