1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.commons.clazz.reflect.common; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.clazz.Clazz; |
23 | |
import org.apache.commons.clazz.reflect.ReflectedClazz; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public abstract class ReflectedPropertyParseResults implements Comparable { |
33 | |
private ReflectedClazz clazz; |
34 | |
private String propertyName; |
35 | |
private List aliases; |
36 | |
|
37 | |
protected AccessorMethodParseResults readMethodParseResults; |
38 | |
protected AccessorMethodParseResults writeMethodParseResults; |
39 | |
protected List extraneousAccessors; |
40 | |
|
41 | 0 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
42 | |
|
43 | |
public ReflectedPropertyParseResults( |
44 | |
ReflectedClazz clazz, |
45 | |
String propertyName) |
46 | 0 | { |
47 | 0 | this.clazz = clazz; |
48 | 0 | this.propertyName = propertyName; |
49 | 0 | } |
50 | |
|
51 | |
public ReflectedClazz getClazz() { |
52 | 0 | return clazz; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public String getPropertyName() { |
60 | 0 | return propertyName; |
61 | |
} |
62 | |
|
63 | |
public int compareTo(Object object) { |
64 | 0 | String n1 = getPropertyName(); |
65 | 0 | String n2 = ((ReflectedPropertyParseResults) object).getPropertyName(); |
66 | 0 | return n1.compareTo(n2); |
67 | |
} |
68 | |
|
69 | |
public String[] getAliases() { |
70 | 0 | if (aliases == null) { |
71 | 0 | return EMPTY_STRING_ARRAY; |
72 | |
} |
73 | 0 | return (String[]) aliases.toArray(EMPTY_STRING_ARRAY); |
74 | |
} |
75 | |
|
76 | |
public void addAlias(String alias) { |
77 | 0 | if (!alias.equals(propertyName)) { |
78 | 0 | if (aliases == null) { |
79 | 0 | aliases = new ArrayList(); |
80 | 0 | aliases.add(alias); |
81 | |
} |
82 | 0 | else if (!aliases.contains(alias)) { |
83 | 0 | aliases.add(alias); |
84 | |
} |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
protected abstract String getPropertyCategory(); |
89 | |
|
90 | |
public void merge(ReflectedPropertyParseResults other) { |
91 | 0 | addAlias(other.getPropertyName()); |
92 | 0 | String[] aliases = other.getAliases(); |
93 | 0 | for (int i = 0; i < aliases.length; i++) { |
94 | 0 | addAlias(aliases[i]); |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
public Class getPropertyType() { |
99 | 0 | if (readMethodParseResults != null) { |
100 | 0 | return readMethodParseResults.getType(); |
101 | |
} |
102 | 0 | if (writeMethodParseResults != null) { |
103 | 0 | return writeMethodParseResults.getType(); |
104 | |
} |
105 | 0 | return null; |
106 | |
} |
107 | |
|
108 | |
public Method getReadMethod() { |
109 | 0 | if (readMethodParseResults == null) { |
110 | 0 | return null; |
111 | |
} |
112 | 0 | return readMethodParseResults.getMethod(); |
113 | |
} |
114 | |
|
115 | |
public Method getWriteMethod() { |
116 | 0 | if (writeMethodParseResults == null) { |
117 | 0 | return null; |
118 | |
} |
119 | 0 | return writeMethodParseResults.getMethod(); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public void setReadMethodParseResults( |
127 | |
AccessorMethodParseResults readMethodParseResults) |
128 | |
{ |
129 | 0 | checkForExtraneousAccessor( |
130 | |
this.readMethodParseResults, |
131 | |
readMethodParseResults); |
132 | 0 | this.readMethodParseResults = readMethodParseResults; |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public void setWriteMethodParseResults( |
140 | |
AccessorMethodParseResults writeMethodParseResults) |
141 | |
{ |
142 | 0 | checkForExtraneousAccessor( |
143 | |
this.writeMethodParseResults, |
144 | |
writeMethodParseResults); |
145 | 0 | this.writeMethodParseResults = writeMethodParseResults; |
146 | 0 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
protected void checkForExtraneousAccessor( |
153 | |
AccessorMethodParseResults currentValue, |
154 | |
AccessorMethodParseResults newValue) |
155 | |
{ |
156 | 0 | if (currentValue != null |
157 | |
&& newValue != null |
158 | |
&& currentValue != newValue) { |
159 | 0 | if (extraneousAccessors == null) { |
160 | 0 | extraneousAccessors = new ArrayList(); |
161 | 0 | extraneousAccessors.add(currentValue); |
162 | |
} |
163 | |
} |
164 | 0 | } |
165 | |
|
166 | |
public boolean checkConsistency() { |
167 | 0 | if (extraneousAccessors != null) { |
168 | 0 | return false; |
169 | |
} |
170 | |
|
171 | 0 | return true; |
172 | |
} |
173 | |
|
174 | |
public String toString() { |
175 | 0 | boolean consistent = checkConsistency(); |
176 | |
|
177 | 0 | StringBuffer buffer = new StringBuffer(); |
178 | 0 | buffer.append("["); |
179 | 0 | if (consistent) { |
180 | 0 | buffer.append(propertyName); |
181 | 0 | if (!getPropertyCategory().equals("scalar")) { |
182 | 0 | buffer.append(" ("); |
183 | 0 | buffer.append(getPropertyCategory()); |
184 | 0 | buffer.append(")"); |
185 | |
} |
186 | |
} |
187 | |
else { |
188 | 0 | buffer.append("*"); |
189 | 0 | buffer.append(propertyName); |
190 | 0 | buffer.append(" - NOT a "); |
191 | 0 | if (!getPropertyCategory().equals("scalar")) { |
192 | 0 | buffer.append(getPropertyCategory()); |
193 | |
} |
194 | 0 | buffer.append(" property"); |
195 | 0 | appendInconsistencyDescriptions(buffer); |
196 | |
} |
197 | 0 | if (aliases != null) { |
198 | 0 | buffer.append("\n [aliases: "); |
199 | 0 | for (int i = 0; i < aliases.size(); i++) { |
200 | 0 | if (i != 0) { |
201 | 0 | buffer.append(", "); |
202 | |
} |
203 | 0 | buffer.append(aliases.get(i)); |
204 | |
} |
205 | 0 | buffer.append("]"); |
206 | |
} |
207 | 0 | appendDescription(buffer); |
208 | 0 | buffer.append("\n [accessor methods:"); |
209 | 0 | appendMethodDescriptions(buffer); |
210 | 0 | buffer.append("\n ]"); |
211 | 0 | buffer.append("\n]"); |
212 | 0 | return buffer.toString(); |
213 | |
} |
214 | |
|
215 | |
protected void appendDescription(StringBuffer buffer) { |
216 | 0 | Class type = getPropertyType(); |
217 | 0 | if (type != null) { |
218 | 0 | buffer.append("\n [type] "); |
219 | 0 | buffer.append(Clazz.getCanonicalClassName(type)); |
220 | |
} |
221 | 0 | } |
222 | |
|
223 | |
protected void appendMethodDescriptions(StringBuffer buffer) { |
224 | 0 | if (readMethodParseResults != null) { |
225 | 0 | buffer.append("\n [get~()] "); |
226 | 0 | buffer.append(readMethodParseResults.getMethod()); |
227 | |
} |
228 | 0 | if (writeMethodParseResults != null) { |
229 | 0 | buffer.append("\n [set~(v)] "); |
230 | 0 | buffer.append(writeMethodParseResults.getMethod()); |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
protected boolean appendInconsistencyDescriptions(StringBuffer buffer) { |
235 | 0 | if (extraneousAccessors != null) { |
236 | 0 | buffer.append("\n - Has extraneous accessors:"); |
237 | 0 | for (int i = 0; i < extraneousAccessors.size(); i++) { |
238 | 0 | AccessorMethodParseResults results = |
239 | |
(AccessorMethodParseResults) extraneousAccessors.get(i); |
240 | 0 | buffer.append("\n "); |
241 | 0 | buffer.append(results.getMethod()); |
242 | |
} |
243 | |
} |
244 | 0 | return true; |
245 | |
} |
246 | |
} |