Revision 44243 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

View differences:

CallMethodFunction.java
1 1
package org.gvsig.expressionevaluator.impl.function.programming;
2 2

  
3
import java.lang.reflect.Method;
4 3
import org.apache.commons.lang3.Range;
5 4
import org.gvsig.expressionevaluator.Function;
6 5
import org.gvsig.expressionevaluator.Interpreter;
6
import org.gvsig.expressionevaluator.impl.InstanceUtils;
7 7
import org.gvsig.expressionevaluator.spi.AbstractFunction;
8 8

  
9 9
public class CallMethodFunction extends AbstractFunction {
10 10

  
11 11
    public static final String NAME = "CALL_METHOD";
12
    
13
    private final Object obj;
14
    private final String methodname;
15

  
16
    public CallMethodFunction(Object obj, String methodName) {
17
        super(Function.GROUP_PROGRAMMING, 
18
            NAME, 
19
            Range.between(0, Integer.MAX_VALUE),
20
            null,
21
            null,
22
            null,
23
            "Object",
24
            false
25
        );
26
        this.obj = obj;
27
        this.methodname = methodName;
28
    }
29
    
12
        
30 13
    public CallMethodFunction() {
31 14
        super(Function.GROUP_PROGRAMMING, 
32 15
            NAME, 
......
37 20
            "Object",
38 21
            false
39 22
        );
40
        this.obj = null;
41
        this.methodname = null;
42 23
    }
43
    
24

  
44 25
    @Override
45 26
    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
        }
54
        if( theMethodName == null ) {
55
            throw new NullPointerException("A method name was expected to invoke and a null was received");
56
        }
57
        if( theObj==null ) {
58
            throw new NullPointerException("An object pointer was expected to invoke method "+theMethodName+" and a null was received");
59
        }
60
        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
        }
71
        Class<?> theClass = theObj.getClass();
72
        Method method = theClass.getMethod(theMethodName, parameterTypes);
73
        Object value = method.invoke(theObj, parameters);
27
        Object instance = getObject(args, 0);
28
        String methodName = getStr(args, 1);
29
        Object value = InstanceUtils.callmethod(instance, methodName, args);
74 30
        return value;
75 31
    }
76
    
77 32
}

Also available in: Unified diff