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 @ 44750

History | View | Annotate | Download (1.8 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 44750 jjdelcerro
import org.gvsig.expressionevaluator.AttributeHandler;
6 44748 jjdelcerro
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR;
7 43939 jjdelcerro
import org.gvsig.expressionevaluator.Function;
8 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
9 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10 44366 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
11 43512 jjdelcerro
12 43939 jjdelcerro
public class GetattrFunction extends AbstractFunction {
13 43512 jjdelcerro
14 43939 jjdelcerro
    public GetattrFunction() {
15 44750 jjdelcerro
        super(
16
                Function.GROUP_PROGRAMMING,
17
                FUNCTION_GETATTR,
18
                Range.is(2),
19
                null,
20
                null,
21
                null,
22
                null,
23
                true
24
        );
25 43512 jjdelcerro
    }
26 44138 jjdelcerro
27 43512 jjdelcerro
    @Override
28 44138 jjdelcerro
    public boolean allowConstantFolding() {
29
        return false;
30
    }
31
32
    @Override
33 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
34 43939 jjdelcerro
        Object obj = getObject(args, 0);
35
        String attrname = getStr(args,1);
36
        if( attrname == null ) {
37
            throw new NullPointerException("A attribute name was expected and a null was received");
38 43512 jjdelcerro
        }
39 43939 jjdelcerro
        if( obj==null ) {
40
            throw new NullPointerException("An object pointer was expected to access attribute "+attrname+" and a null was received");
41
        }
42 44366 jjdelcerro
        Object value;
43
        if( obj instanceof DynObject ) {
44
            value = ((DynObject) obj).getDynValue(attrname);
45 44750 jjdelcerro
        } else if( obj instanceof AttributeHandler ) {
46
            value = ((AttributeHandler) obj).get(attrname);
47 44366 jjdelcerro
        } else {
48
            Class<?> theClass = obj.getClass();
49
            Field field = theClass.getField(attrname);
50
            value = field.get(obj);
51
        }
52 43939 jjdelcerro
        return value;
53 43512 jjdelcerro
    }
54
55
}