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 @ 46831
History | View | Annotate | Download (3.18 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 | 45011 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
7 | import org.gvsig.expressionevaluator.Codes; |
||
8 | 44748 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
9 | 45011 | jjdelcerro | import org.gvsig.expressionevaluator.Formatter; |
10 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
11 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
12 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
13 | 44366 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
14 | 45114 | jjdelcerro | import org.gvsig.tools.util.GetItemByKey; |
15 | 43512 | jjdelcerro | |
16 | 43939 | jjdelcerro | public class GetattrFunction extends AbstractFunction { |
17 | 43512 | jjdelcerro | |
18 | 43939 | jjdelcerro | public GetattrFunction() {
|
19 | 44750 | jjdelcerro | super(
|
20 | Function.GROUP_PROGRAMMING, |
||
21 | FUNCTION_GETATTR, |
||
22 | Range.is(2),
|
||
23 | null,
|
||
24 | 44924 | jjdelcerro | "GETATTR({{object}}, attribute_name_as_string)",
|
25 | 44750 | jjdelcerro | null,
|
26 | null,
|
||
27 | true
|
||
28 | ); |
||
29 | 43512 | jjdelcerro | } |
30 | 44138 | jjdelcerro | |
31 | 43512 | jjdelcerro | @Override
|
32 | 44138 | jjdelcerro | public boolean allowConstantFolding() { |
33 | return false; |
||
34 | } |
||
35 | |||
36 | @Override
|
||
37 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
38 | 43939 | jjdelcerro | Object obj = getObject(args, 0); |
39 | String attrname = getStr(args,1); |
||
40 | if( attrname == null ) { |
||
41 | throw new NullPointerException("A attribute name was expected and a null was received"); |
||
42 | 43512 | jjdelcerro | } |
43 | 43939 | jjdelcerro | if( obj==null ) { |
44 | 45236 | omartinez | return null; |
45 | //throw new NullPointerException("An object pointer was expected to access attribute "+attrname+" and a null was received");
|
||
46 | 43939 | jjdelcerro | } |
47 | 44366 | jjdelcerro | Object value;
|
48 | if( obj instanceof DynObject ) { |
||
49 | value = ((DynObject) obj).getDynValue(attrname); |
||
50 | 44750 | jjdelcerro | } else if( obj instanceof AttributeHandler ) { |
51 | value = ((AttributeHandler) obj).get(attrname); |
||
52 | 45114 | jjdelcerro | } else if( obj instanceof GetItemByKey ) { |
53 | value = ((GetItemByKey) obj).get(attrname); |
||
54 | 44366 | jjdelcerro | } else {
|
55 | Class<?> theClass = obj.getClass();
|
||
56 | Field field = theClass.getField(attrname);
|
||
57 | value = field.get(obj); |
||
58 | } |
||
59 | 43939 | jjdelcerro | return value;
|
60 | 43512 | jjdelcerro | } |
61 | 45011 | jjdelcerro | |
62 | @Override
|
||
63 | public String toString(Codes args, Formatter<Code> formatter) { |
||
64 | StringBuilder builder = new StringBuilder(); |
||
65 | Code arg0 = args.get(0);
|
||
66 | Code arg1 = args.get(1);
|
||
67 | 46010 | jjdelcerro | // if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Identifier ) {
|
68 | // builder.append(arg0.toString(formatter));
|
||
69 | // builder.append("->");
|
||
70 | // builder.append(arg1.toString());
|
||
71 | // } else
|
||
72 | if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant && ((Code.Constant)arg1).value() instanceof CharSequence ) { |
||
73 | 45011 | jjdelcerro | builder.append(arg0.toString(formatter)); |
74 | 46010 | jjdelcerro | builder.append(".\"");
|
75 | builder.append(((Code.Constant)arg1).value().toString()); |
||
76 | builder.append("\"");
|
||
77 | 45011 | jjdelcerro | } else {
|
78 | builder.append(this.name());
|
||
79 | builder.append("(");
|
||
80 | builder.append(arg0.toString(formatter)); |
||
81 | builder.append(", ");
|
||
82 | builder.append(arg1.toString(formatter)); |
||
83 | builder.append(")");
|
||
84 | } |
||
85 | return builder.toString();
|
||
86 | } |
||
87 | 43512 | jjdelcerro | |
88 | } |