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 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.lang.reflect.Field; |
4 |
import org.apache.commons.lang3.Range; |
5 |
import org.gvsig.expressionevaluator.AttributeHandler; |
6 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
7 |
import org.gvsig.expressionevaluator.Function; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
10 |
import org.gvsig.tools.dynobject.DynObject; |
11 |
|
12 |
public class GetattrFunction extends AbstractFunction { |
13 |
|
14 |
public GetattrFunction() {
|
15 |
super(
|
16 |
Function.GROUP_PROGRAMMING, |
17 |
FUNCTION_GETATTR, |
18 |
Range.is(2),
|
19 |
null,
|
20 |
null,
|
21 |
null,
|
22 |
null,
|
23 |
true
|
24 |
); |
25 |
} |
26 |
|
27 |
@Override
|
28 |
public boolean allowConstantFolding() { |
29 |
return false; |
30 |
} |
31 |
|
32 |
@Override
|
33 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
34 |
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 |
} |
39 |
if( obj==null ) { |
40 |
throw new NullPointerException("An object pointer was expected to access attribute "+attrname+" and a null was received"); |
41 |
} |
42 |
Object value;
|
43 |
if( obj instanceof DynObject ) { |
44 |
value = ((DynObject) obj).getDynValue(attrname); |
45 |
} else if( obj instanceof AttributeHandler ) { |
46 |
value = ((AttributeHandler) obj).get(attrname); |
47 |
} else {
|
48 |
Class<?> theClass = obj.getClass();
|
49 |
Field field = theClass.getField(attrname);
|
50 |
value = field.get(obj); |
51 |
} |
52 |
return value;
|
53 |
} |
54 |
|
55 |
} |