1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.beanfilter;
18
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class VariableValue extends EvalValue {
39
40
41
42
43 private static Log log = LogFactory.getLog(FieldValue.class);
44
45
46
47
48 private String s;
49
50
51
52
53
54
55
56
57
58 public VariableValue(Token token, String s, Class<?> parentClass)
59 throws EvalException {
60 super(token.beginLine, token.beginColumn);
61 this.s = s;
62 BeanDescriptor bd = BeanDescriptor.getInstance(parentClass);
63 Method method = bd.getMethod(s);
64 if (method == null) {
65
66 Object[] eparams = { getLinePos(), getCharPos(), s};
67 throw new EvalException("FTR005", eparams);
68 }
69 }
70
71
72
73
74 @SuppressWarnings("unchecked")
75 @Override public Object getValue(Object parentValues) throws EvalException {
76 if (parentValues == null) {
77 return null;
78 }
79 if (parentValues instanceof HashSet) {
80 ArrayList<Object> values = new ArrayList<Object>((HashSet)parentValues);
81 if (values.size() == 0) {
82 return null;
83 }
84 BeanDescriptor bd = BeanDescriptor.getInstance(values.get(0).getClass());
85 HashSet<Object> results;
86 if (parentValues instanceof AllItemsHashSet) {
87 results = new AllItemsHashSet();
88 } else {
89 results = new AnyItemsHashSet();
90 }
91
92 Method method = bd.getMethod(s);
93 if (method == null) {
94
95 Object[] eparams = { getLinePos(), getCharPos(), s };
96 throw new EvalException("FTR005", eparams);
97 }
98 try {
99 for (Object item : values) {
100 results.add(method.invoke(item));
101 }
102 } catch (Exception e) {
103 log.info(e.toString(), e);
104
105 Object[] eparams = { getLinePos(), getCharPos(), s };
106 throw new EvalException(e, "FTR005", eparams);
107 }
108
109 return results;
110 } else {
111 BeanDescriptor bd = BeanDescriptor.getInstance(parentValues.getClass());
112 Object result;
113 Method method = bd.getMethod(s);
114 if (method == null) {
115
116 Object[] eparams = { getLinePos(), getCharPos(), s};
117 throw new EvalException("FTR005", eparams);
118 }
119 try {
120 result = method.invoke(parentValues);
121 } catch (Exception e) {
122 log.info(e.toString(), e);
123
124 Object[] eparams = { getLinePos(), getCharPos(), s};
125 throw new EvalException(e, "FTR005", eparams);
126 }
127 return result;
128 }
129 }
130
131
132
133
134 @Override public String toString() {
135 return s;
136 }
137
138
139
140
141 @Override
142 public Class<?> getClassValue(Class<?> parentClass) throws EvalException {
143 BeanDescriptor bd = BeanDescriptor.getInstance(parentClass);
144 Method method = bd.getMethod(s);
145 if (method == null) {
146
147 Object[] eparams = { getLinePos(), getCharPos(), s};
148 throw new EvalException("FTR005", eparams);
149 }
150 return method.getReturnType();
151 }
152 }