1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.beans.gen;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.joda.beans.PropertyReadWrite;
25
26
27
28
29
30
31 class GeneratableProperty {
32
33
34 private static final Set<String> COLLECTIONS = new HashSet<String>(
35 Arrays.asList(
36 "Collection", "Set", "SortedSet", "NavigableSet", "List",
37 "ArrayList", "LinkedList",
38 "HashSet", "LinkedHashSet", "TreeSet", "ConcurrentSkipListSet"));
39
40 private static final Set<String> MAPS = new HashSet<String>(
41 Arrays.asList(
42 "Map", "SortedMap", "NavigableMap", "ConcurrentMap", "ConcurrentNavigableMap",
43 "HashMap", "LinkedHashMap", "TreeMap", "ConcurrentHashMap", "ConcurrentSkipListMap"));
44
45
46 private final GeneratableBean bean;
47
48 private String propertyName;
49
50 private String fieldName;
51
52 private String upperName;
53
54 private String type;
55
56 private boolean isFinal;
57
58 private String getStyle;
59
60 private String setStyle;
61
62 private String validation;
63
64 private boolean deprecated;
65
66 private String firstComment;
67
68 private final List<String> comments = new ArrayList<String>();
69
70
71
72
73 GeneratableProperty(GeneratableBean bean) {
74 this.bean = bean;
75 }
76
77
78
79
80
81
82 public GeneratableBean getBean() {
83 return bean;
84 }
85
86
87
88
89
90 public String getPropertyName() {
91 return propertyName;
92 }
93
94
95
96
97
98 public void setPropertyName(String propertyName) {
99 this.propertyName = propertyName;
100 }
101
102
103
104
105
106 public String getFieldName() {
107 return fieldName;
108 }
109
110
111
112
113
114 public void setFieldName(String fieldName) {
115 this.fieldName = fieldName;
116 }
117
118
119
120
121
122 public String getUpperName() {
123 return upperName;
124 }
125
126
127
128
129
130 public void setUpperName(String upperName) {
131 this.upperName = upperName;
132 }
133
134
135
136
137
138 public String getType() {
139 return type;
140 }
141
142
143
144
145
146 public void setType(String type) {
147 this.type = type;
148 }
149
150
151
152
153
154 public boolean isFinal() {
155 return isFinal;
156 }
157
158
159
160
161
162 public void setFinal(boolean isFinal) {
163 this.isFinal = isFinal;
164 }
165
166
167
168
169
170 public String getGetStyle() {
171 return getStyle;
172 }
173
174
175
176
177
178 public void setGetStyle(String getStyle) {
179 this.getStyle = getStyle;
180 }
181
182
183
184
185
186 public String getSetStyle() {
187 return setStyle;
188 }
189
190
191
192
193
194 public void setSetStyle(String setStyle) {
195 this.setStyle = setStyle;
196 }
197
198
199
200
201
202 public String getValidation() {
203 return validation;
204 }
205
206
207
208
209
210 public void setValidation(String validation) {
211 this.validation = validation;
212 }
213
214
215
216
217
218 public boolean isDeprecated() {
219 return deprecated;
220 }
221
222
223
224
225
226 public void setDeprecated(boolean deprecated) {
227 this.deprecated = deprecated;
228 }
229
230
231
232
233
234 public String getFirstComment() {
235 return firstComment;
236 }
237
238
239
240
241
242 public void setFirstComment(String firstComment) {
243 this.firstComment = firstComment;
244 }
245
246
247
248
249
250 public List<String> getComments() {
251 return comments;
252 }
253
254
255
256
257
258
259 public boolean isGenericParamType() {
260 return type.indexOf("<") >= 0;
261 }
262
263
264
265
266
267 public boolean isGenericWildcardParamType() {
268 return type.endsWith("<?>");
269 }
270
271
272
273
274
275 public String getGenericParamType() {
276 int pos = type.indexOf("<");
277 if (pos < 0) {
278 return "";
279 }
280 return type.substring(pos + 1, type.length() - 1);
281 }
282
283
284
285
286
287 public String getRawType() {
288 int pos = type.indexOf("<");
289 return (pos < 0 ? type : type.substring(0, pos));
290 }
291
292
293
294
295
296
297 public boolean isBeanGenericType() {
298 return type.equals(bean.getTypeGenericName(false));
299 }
300
301
302
303
304
305 public boolean isGeneric() {
306 return isGenericParamType() || isBeanGenericType();
307 }
308
309
310
311
312
313 public boolean isDerived() {
314 return fieldName == null;
315 }
316
317
318
319
320
321
322
323 public boolean isCollectionType() {
324 return isGeneric() && COLLECTIONS.contains(getRawType());
325 }
326
327
328
329
330
331
332 public boolean isMapType() {
333 return "FlexiBean".equals(getType()) || (isGeneric() && MAPS.contains(getRawType()));
334 }
335
336
337
338
339
340
341 public PropertyReadWrite getReadWrite() {
342 SetterGen generator = SetterGen.of(this);
343 if (getGetStyle().length() > 0 && getSetStyle().length() > 0 && (generator.isSetterGenerated(this) || getSetStyle().equals("manual"))) {
344 return PropertyReadWrite.READ_WRITE;
345 }
346 if (getGetStyle().length() > 0) {
347 return PropertyReadWrite.READ_ONLY;
348 }
349 if (getSetStyle().length() > 0) {
350 return PropertyReadWrite.WRITE_ONLY;
351 }
352 throw new RuntimeException("Property must have a getter or setter: " + propertyName);
353 }
354
355
356
357
358
359
360 public boolean isValidated() {
361 return getValidation() != null && getValidation().length() > 0;
362 }
363
364
365
366
367
368 public boolean isNotNull() {
369 return getValidation().equals("notNull") || getValidation().equals("notEmpty");
370 }
371
372
373
374
375
376 public String getValidationMethodName() {
377 if (isValidated() == false) {
378 throw new IllegalStateException();
379 }
380 if (getValidation().equals("notNull") || getValidation().equals("notEmpty")) {
381 return "JodaBeanUtils." + getValidation();
382 }
383 return getValidation();
384 }
385
386 }