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 / GetattrFunction.java @ 44748

History | View | Annotate | Download (1.47 KB)

1 44138 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.programming;
2 43512 jjdelcerro
3 43939 jjdelcerro
import java.lang.reflect.Field;
4 43512 jjdelcerro
import org.apache.commons.lang3.Range;
5 44748 jjdelcerro
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR;
6 43939 jjdelcerro
import org.gvsig.expressionevaluator.Function;
7 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
8 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
9 44366 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
10 43512 jjdelcerro
11 43939 jjdelcerro
public class GetattrFunction extends AbstractFunction {
12 43512 jjdelcerro
13 43939 jjdelcerro
    public GetattrFunction() {
14 44748 jjdelcerro
        super(Function.GROUP_PROGRAMMING, FUNCTION_GETATTR, Range.is(2));
15 43512 jjdelcerro
    }
16 44138 jjdelcerro
17 43512 jjdelcerro
    @Override
18 44138 jjdelcerro
    public boolean allowConstantFolding() {
19
        return false;
20
    }
21
22
    @Override
23 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
24 43939 jjdelcerro
        Object obj = getObject(args, 0);
25
        String attrname = getStr(args,1);
26
        if( attrname == null ) {
27
            throw new NullPointerException("A attribute name was expected and a null was received");
28 43512 jjdelcerro
        }
29 43939 jjdelcerro
        if( obj==null ) {
30
            throw new NullPointerException("An object pointer was expected to access attribute "+attrname+" and a null was received");
31
        }
32 44366 jjdelcerro
        Object value;
33
        if( obj instanceof DynObject ) {
34
            value = ((DynObject) obj).getDynValue(attrname);
35
        } else {
36
            Class<?> theClass = obj.getClass();
37
            Field field = theClass.getField(attrname);
38
            value = field.get(obj);
39
        }
40 43939 jjdelcerro
        return value;
41 43512 jjdelcerro
    }
42
43
}