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 @ 47248
History | View | Annotate | Download (5.41 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.lang.reflect.Field; |
4 |
import java.lang.reflect.Method; |
5 |
import java.util.Map; |
6 |
import javax.json.JsonObject; |
7 |
import org.apache.commons.lang3.Range; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.AttributeHandler; |
10 |
import org.gvsig.expressionevaluator.Code; |
11 |
import org.gvsig.expressionevaluator.Codes; |
12 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
13 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
14 |
import org.gvsig.expressionevaluator.Formatter; |
15 |
import org.gvsig.expressionevaluator.Function; |
16 |
import org.gvsig.expressionevaluator.Interpreter; |
17 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
18 |
import org.gvsig.expressionevaluator.spi.JsonUtils; |
19 |
import org.gvsig.tools.dynobject.DynObject; |
20 |
import org.gvsig.tools.util.GetItemByKey; |
21 |
import org.gvsig.tools.util.PropertiesSupport; |
22 |
|
23 |
public class GetattrFunction extends AbstractFunction { |
24 |
|
25 |
public GetattrFunction() {
|
26 |
super(
|
27 |
Function.GROUP_PROGRAMMING, |
28 |
FUNCTION_GETATTR, |
29 |
Range.is(2),
|
30 |
null,
|
31 |
"GETATTR({{object}}, attribute_name_as_string)",
|
32 |
null,
|
33 |
null,
|
34 |
true
|
35 |
); |
36 |
} |
37 |
|
38 |
@Override
|
39 |
public boolean allowConstantFolding() { |
40 |
return false; |
41 |
} |
42 |
|
43 |
@Override
|
44 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
45 |
Object obj = getObject(args, 0); |
46 |
String attrname = getStr(args,1); |
47 |
if( StringUtils.isBlank(attrname) ) {
|
48 |
throw new NullPointerException("A attribute name was expected and a null was received"); |
49 |
} |
50 |
if( obj==null ) { |
51 |
return null; |
52 |
//throw new NullPointerException("An object pointer was expected to access attribute "+attrname+" and a null was received");
|
53 |
} |
54 |
Object value;
|
55 |
if( obj instanceof DynObject && ((DynObject) obj).hasDynValue(attrname) ) { |
56 |
value = ((DynObject) obj).getDynValue(attrname); |
57 |
} else if( obj instanceof AttributeHandler ) { |
58 |
value = ((AttributeHandler) obj).get(attrname); |
59 |
} else if( obj instanceof GetItemByKey ) { |
60 |
value = ((GetItemByKey) obj).get(attrname); |
61 |
} else {
|
62 |
Field field = this.getField(obj, attrname); |
63 |
if( field!=null ) { |
64 |
value = field.get(obj); |
65 |
} else {
|
66 |
Method method = this.getMethod(obj, "get" + StringUtils.capitalize(attrname)); |
67 |
if( method!=null ) { |
68 |
value = method.invoke(obj); |
69 |
} else if( obj instanceof PropertiesSupport ) { |
70 |
value = ((PropertiesSupport) obj).getProperty(attrname); |
71 |
} else if( obj instanceof JsonObject ) { |
72 |
JsonObject jsonobj = ((JsonObject)obj); |
73 |
value = JsonUtils.getitem(jsonobj, attrname); |
74 |
} else if( obj instanceof Map ) { |
75 |
value = ((Map)obj).get(attrname);
|
76 |
} else {
|
77 |
throw new NoSuchFieldException(attrname); |
78 |
} |
79 |
} |
80 |
} |
81 |
return value;
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public String toString(Codes args, Formatter<Code> formatter) { |
86 |
StringBuilder builder = new StringBuilder(); |
87 |
Code arg0 = args.get(0);
|
88 |
Code arg1 = args.get(1);
|
89 |
// if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Identifier ) {
|
90 |
// builder.append(arg0.toString(formatter));
|
91 |
// builder.append("->");
|
92 |
// builder.append(arg1.toString());
|
93 |
// } else
|
94 |
if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant && ((Code.Constant)arg1).value() instanceof CharSequence ) { |
95 |
builder.append(arg0.toString(formatter)); |
96 |
builder.append(".\"");
|
97 |
builder.append(((Code.Constant)arg1).value().toString()); |
98 |
builder.append("\"");
|
99 |
} else {
|
100 |
builder.append(this.name());
|
101 |
builder.append("(");
|
102 |
builder.append(arg0.toString(formatter)); |
103 |
builder.append(", ");
|
104 |
builder.append(arg1.toString(formatter)); |
105 |
builder.append(")");
|
106 |
} |
107 |
return builder.toString();
|
108 |
} |
109 |
|
110 |
private Field getField(Object obj, String attrname) { |
111 |
Class<?> theClass = obj.getClass();
|
112 |
try {
|
113 |
Field field = theClass.getField(attrname);
|
114 |
return field;
|
115 |
} catch(NoSuchFieldException e) { |
116 |
return null; |
117 |
} |
118 |
} |
119 |
|
120 |
private Method getMethod(Object obj, String methodname) { |
121 |
Class<?> theClass = obj.getClass();
|
122 |
try {
|
123 |
Method method = theClass.getMethod(methodname);
|
124 |
return method;
|
125 |
} catch(NoSuchMethodException e) { |
126 |
return null; |
127 |
} |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes parameters) {
|
132 |
|
133 |
Code arg0 = parameters.get(0);
|
134 |
Code arg1 = parameters.get(1);
|
135 |
|
136 |
if( arg0 instanceof Code.Identifier && |
137 |
arg1 instanceof Code.Constant &&
|
138 |
((Code.Constant)arg1).value() instanceof CharSequence ) { |
139 |
return builder.getattr(
|
140 |
((Code.Identifier)arg0).name(), |
141 |
((Code.Constant)arg1).value().toString() |
142 |
); |
143 |
} |
144 |
return builder.function(FUNCTION_GETATTR,arg0.toValue(), arg1.toValue());
|
145 |
} |
146 |
|
147 |
} |