1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.nuiton.eugene;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.nuiton.eugene.models.Model;
28 import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata;
29 import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil;
30 import org.nuiton.eugene.models.extension.tagvalue.WithTagValuesOrStereotypes;
31 import org.nuiton.eugene.models.object.ObjectModel;
32 import org.nuiton.eugene.models.object.ObjectModelAttribute;
33 import org.nuiton.eugene.models.object.ObjectModelClass;
34 import org.nuiton.eugene.models.object.ObjectModelClassifier;
35 import org.nuiton.eugene.models.object.ObjectModelElement;
36 import org.nuiton.eugene.models.object.ObjectModelOperation;
37 import org.nuiton.eugene.models.object.ObjectModelParameter;
38 import org.nuiton.eugene.models.object.SerialVersionUIDBuilder;
39 import org.nuiton.util.StringUtil;
40
41 import java.io.File;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Set;
47
48
49
50
51
52
53
54
55 public class GeneratorUtil {
56
57 public static final String SERIAL_VERSION_UID = "serialVersionUID";
58
59
60
61
62
63
64
65
66
67
68 public static String getParentPackageName(String packageName) {
69 String parentPackageName = packageName;
70 int index = packageName.lastIndexOf('.');
71 if (index != -1) {
72 parentPackageName = packageName.substring(0, index);
73 }
74 return parentPackageName;
75 }
76
77
78
79
80
81
82
83 public static String getClassNameFromQualifiedName(String qualifiedName) {
84 String className = qualifiedName;
85 int index = qualifiedName.lastIndexOf('.');
86 if (index != -1) {
87 className = qualifiedName.substring(index + 1);
88 }
89 return className;
90 }
91
92
93
94
95
96 public static String getFilenameFromQualifiedName(String qualifiedName) {
97 return qualifiedName.replace('.', File.separatorChar);
98 }
99
100
101
102
103
104
105
106
107
108 public static Collection<ObjectModelClassifier> getClassifiers(
109 ObjectModel model, String packageName) {
110 List<ObjectModelClassifier> classifiers = new ArrayList<>();
111 for (ObjectModelClassifier classifier : model.getClassifiers()) {
112 if (classifier.getPackageName().startsWith(packageName)) {
113 classifiers.add(classifier);
114 }
115 }
116 return classifiers;
117 }
118
119
120
121
122
123
124
125 public static boolean isPrimitiveAttributeType(
126 ObjectModelAttribute attribute) {
127 if (isNMultiplicity(attribute)) {
128 return false;
129 }
130 String type = attribute.getType();
131 return isPrimitiveType(type);
132
133
134
135
136 }
137
138
139
140
141
142
143
144
145 public static boolean isPrimitiveType(String type) {
146 return "byte".equals(type) || "short".equals(type)
147 || "int".equals(type) || "long".equals(type)
148 || "float".equals(type) || "double".equals(type)
149 || "char".equals(type) || "boolean".equals(type);
150 }
151
152
153
154
155
156
157 public static String getPrimitiveWrapType(String type) {
158 if ("byte".equals(type)) {
159 return "Byte";
160 }
161 if ("short".equals(type)) {
162 return "Short";
163 }
164 if ("int".equals(type)) {
165 return "Integer";
166 }
167 if ("long".equals(type)) {
168 return "Long";
169 }
170 if ("float".equals(type)) {
171 return "Float";
172 }
173 if ("double".equals(type)) {
174 return "Double";
175 }
176 if ("char".equals(type)) {
177 return "Character";
178 }
179 if ("boolean".equals(type)) {
180 return "Boolean";
181 }
182
183 return null;
184 }
185
186
187
188
189
190
191
192 public static String getInitValue(ObjectModelAttribute attribute) {
193 if (isNMultiplicity(attribute)) {
194 return "new java.util.ArrayList()";
195 }
196 return getInitValue(attribute.getType());
197 }
198
199 public static String getInitValue(String type) {
200 if ("byte".equals(type)) {
201 return "0";
202 }
203 if ("short".equals(type)) {
204 return "0";
205 }
206 if ("int".equals(type)) {
207 return "0";
208 }
209 if ("long".equals(type)) {
210 return "0";
211 }
212 if ("float".equals(type)) {
213 return "0";
214 }
215 if ("double".equals(type)) {
216 return "0";
217 }
218 if ("char".equals(type)) {
219 return "\u0000";
220 }
221 if ("boolean".equals(type)) {
222 return "false";
223 }
224 if ("java.lang.String".equals(type)) {
225 return null;
226 }
227 if ("java.lang.Date".equals(type)) {
228 return null;
229 }
230 return null;
231 }
232
233 public static String getCastValue(String type, String o) {
234 if ("byte".equals(type)) {
235 return "((Byte)" + o + ").byteValue()";
236 }
237 if ("short".equals(type)) {
238 return "((Short)" + o + ").shortValue()";
239 }
240 if ("int".equals(type)) {
241 return "((Integer)" + o + ").intValue()";
242 }
243 if ("long".equals(type)) {
244 return "((Long)" + o + ").longValue()";
245 }
246 if ("float".equals(type)) {
247 return "((Float)" + o + ").floatValue()";
248 }
249 if ("double".equals(type)) {
250 return "((Double)" + o + ").doubleValue()";
251 }
252 if ("char".equals(type)) {
253 return "((Character)" + o + ").charValue()";
254 }
255 if ("boolean".equals(type)) {
256 return "((Boolean)" + o + ").booleanValue()";
257 }
258 if ("void".equals(type)) {
259 return "";
260 }
261 return "(" + type + ")" + o;
262 }
263
264 public static String getHashCodeExpression(String type, String o) {
265 String result = o;
266 if ("byte".equals(type)) {
267 result = "new Byte(" + o + ")";
268 }
269 if ("short".equals(type)) {
270 result = "new Short(" + o + ")";
271 }
272 if ("int".equals(type)) {
273 result = "new Integer(" + o + ")";
274 }
275 if ("long".equals(type)) {
276 result = "new Long(" + o + ")";
277 }
278 if ("float".equals(type)) {
279 result = "new Float(" + o + ")";
280 }
281 if ("double".equals(type)) {
282 result = "new Double(" + o + ")";
283 }
284 if ("char".equals(type)) {
285 result = "new Character(" + o + ")";
286 }
287 if ("boolean".equals(type)) {
288 result = "new Boolean(" + o + ")";
289 }
290 return result + ".hashCode()";
291 }
292
293
294
295
296
297
298 public static boolean isNMultiplicity(ObjectModelAttribute attribute) {
299 return isNMultiplicity(attribute.getMaxMultiplicity());
300 }
301
302 public static boolean isNMultiplicity(int multiplicity) {
303 return multiplicity == -1 || multiplicity > 1;
304 }
305
306
307
308
309
310 public static boolean isOneMultiplicity(ObjectModelAttribute attribute) {
311 return attribute != null && attribute.getMinMultiplicity() == 1 &&
312 attribute.getMaxMultiplicity() == 1;
313 }
314
315
316
317
318
319 public static String toLowerCaseFirstLetter(String word) {
320 return word.substring(0, 1).toLowerCase() + word.substring(1);
321 }
322
323
324
325
326
327 public static boolean isComposition(ObjectModelAttribute attribute) {
328
329 boolean composition = false;
330 if (attribute != null && attribute.getReverseAttribute() != null) {
331 composition = attribute.getReverseAttribute().isComposite();
332 }
333 return composition;
334 }
335
336
337
338
339
340
341
342
343 public static boolean isBooleanPrimitive(ObjectModelAttribute attribute) {
344 String type = attribute.getType();
345 return isBooleanPrimitive(type);
346 }
347
348
349
350
351
352
353
354
355 public static boolean isBooleanPrimitive(String type) {
356 return "boolean".equals(type);
357 }
358
359 public static String getParsingExpression(String type,
360 String attributeStringName) {
361 if ("byte".equals(type)) {
362 return "Byte.parseByte(" + attributeStringName + ")";
363 }
364 if ("short".equals(type)) {
365 return "Short.parseShort(" + attributeStringName + ")";
366 }
367 if ("int".equals(type)) {
368 return "Integer.parseInt(" + attributeStringName + ")";
369 }
370 if ("long".equals(type)) {
371 return "Long.parseLong(" + attributeStringName + ")";
372 }
373 if ("float".equals(type)) {
374 return "Float.parseFloat(" + attributeStringName + ")";
375 }
376 if ("double".equals(type)) {
377 return "Double.parseDouble(" + attributeStringName + ")";
378 }
379 if ("char".equals(type)) {
380 return attributeStringName + ".charAt(0)";
381 }
382 if ("boolean".equals(type)) {
383 return "Boolean.parseBoolean(" + attributeStringName + ")";
384 }
385 if ("java.lang.String".equals(type)) {
386 return attributeStringName;
387 }
388 if ("java.util.Date".equals(type)) {
389 return "dateParser.parse(" + attributeStringName + ")";
390 }
391 return null;
392 }
393
394 public static String getFormatingExpression(String type,
395 String attributeStringName) {
396 if ("byte".equals(type)) {
397 return "Byte.toString(" + attributeStringName + ")";
398 }
399 if ("short".equals(type)) {
400 return "Short.toString(" + attributeStringName + ")";
401 }
402 if ("int".equals(type)) {
403 return "Integer.toString(" + attributeStringName + ")";
404 }
405 if ("long".equals(type)) {
406 return "Long.toString(" + attributeStringName + ")";
407 }
408 if ("float".equals(type)) {
409 return "Float.toString(" + attributeStringName + ")";
410 }
411 if ("double".equals(type)) {
412 return "Double.toString(" + attributeStringName + ")";
413 }
414 if ("char".equals(type)) {
415 return "Character.toString(" + attributeStringName + ")";
416 }
417 if ("boolean".equals(type)) {
418 return "Boolean.parseBoolean(" + attributeStringName + ")";
419 }
420 if ("java.lang.String".equals(type)) {
421 return attributeStringName;
422 }
423 if ("java.util.Date".equals(type)) {
424 return "dateParser.format(" + attributeStringName + ")";
425 }
426 return null;
427 }
428
429
430
431
432
433
434 static final StringUtil.ToString<ObjectModelParameter>
435 OBJECT_MODEL_PARAMETER_TO_STRING_NAME =
436 new StringUtil.ToString<ObjectModelParameter>() {
437
438 @Override
439 public String toString(ObjectModelParameter param) {
440 return param.getName();
441 }
442 };
443
444
445
446
447
448
449
450
451
452 public static String getOperationParametersListName(
453 ObjectModelOperation operation) {
454 String result = StringUtil.join(
455 operation.getParameters(),
456 OBJECT_MODEL_PARAMETER_TO_STRING_NAME,
457 ", ",
458 false);
459 return result;
460 }
461
462
463
464
465
466
467
468
469 public static boolean isFirstAttribute(ObjectModelAttribute attr) {
470 boolean firstAttribute = true;
471 if (attr.getReverseAttribute() != null) {
472 firstAttribute = attr.getName().compareTo(
473 attr.getReverseAttribute().getName()) < 0;
474 }
475 return firstAttribute;
476 }
477
478
479
480
481
482
483
484 public static boolean hasDocumentation(ObjectModelElement element) {
485 return StringUtils.isNotEmpty(element.getDocumentation());
486 }
487
488
489
490
491
492
493
494
495 @Deprecated
496 public static boolean notEmpty(String s) {
497 return s != null && !"".equals(s);
498 }
499
500
501
502
503
504
505
506
507
508
509
510 @Deprecated
511 public static String findTagValue(String tagName,
512 ObjectModelElement element,
513 Model model) {
514 if (element == null) {
515 if (model != null) {
516 String value = model.getTagValue(tagName);
517 if (!StringUtils.isEmpty(value)) {
518 return value;
519 }
520 }
521 return null;
522 }
523 String value = element.getTagValue(tagName);
524 if (!StringUtils.isEmpty(value)) {
525 return value;
526 }
527
528 return findTagValue(tagName, element.getDeclaringElement(), model);
529 }
530
531 public static boolean hasStereotype(ObjectModelElement element,
532 String stereotype) {
533 return element.hasStereotype(stereotype) ||
534 element.hasStereotype(stereotype.toLowerCase());
535 }
536
537
538
539
540
541
542
543
544
545
546
547 @Deprecated
548 public static String computeSerialVersionUID(ObjectModelClass clazz) {
549 String query = clazz.getQualifiedName() + "#";
550 for (ObjectModelAttribute attr : clazz.getAttributes()) {
551 query += "-" + attr.getType();
552 }
553 String result = query.hashCode() + "L";
554 return result;
555 }
556
557
558
559
560
561
562
563
564
565
566
567 public static String getSimpleName(String str) {
568 String result = getSimpleName(str, false);
569 return result;
570 }
571
572
573
574
575
576
577
578
579
580
581
582
583 public static String getSimpleName(String str, boolean removeGenericDefinition) {
584 if (removeGenericDefinition) {
585 str = removeGenericDefinition(str);
586 }
587
588 boolean variableArrayType = false;
589 if (str.endsWith("...")) {
590 variableArrayType = true;
591 str = str.substring(0, str.length() - 3);
592 }
593 if (str.startsWith("\"") && str.endsWith("\"")) {
594 return str;
595 }
596
597 String result = str.replaceAll("\\p{Alpha}\\w*\\.", "");
598 if (variableArrayType) {
599 result += "...";
600 }
601 return result;
602 }
603
604
605
606
607
608
609
610
611
612
613
614
615
616 public static String removeGenericDefinition(String str) {
617
618
619 String result = str.trim();
620
621 if (!result.startsWith("<")) {
622
623
624 return str;
625 }
626
627 int i = 0;
628 for (int length = result.length(), count = 0; i < length; i++) {
629 char c = result.charAt(i);
630 if ('<' == c) {
631 count++;
632 } else if ('>' == c) {
633 count--;
634 }
635 if (count == 0) {
636 break;
637 }
638 }
639
640
641
642 result = result.substring(i + 1);
643
644
645 while (result.startsWith(" ")) {
646 result = result.substring(1);
647 }
648 return result;
649 }
650
651
652
653
654
655
656
657
658
659
660
661
662
663 public static String removeAnyGenericDefinition(String str) {
664
665
666 String result = str.trim();
667
668 if (!result.contains("<")) {
669
670
671 return str;
672 }
673 result = result.substring(0, result.indexOf('<'));
674 return result;
675 }
676
677
678
679
680
681
682 public static String[] splitGenericDefinition(String str) {
683
684
685 String result = str.trim();
686
687 if (!result.startsWith("<")) {
688
689
690 return new String[]{str};
691 }
692
693 int i = 0;
694 for (int length = result.length(), count = 0; i < length; i++) {
695 char c = result.charAt(i);
696 if ('<' == c) {
697 count++;
698 } else if ('>' == c) {
699 count--;
700 }
701 if (count == 0) {
702 break;
703 }
704 }
705
706
707
708 String prefix = result.substring(0, i + 1);
709 String end = result.substring(i + 1);
710
711
712 while (end.startsWith(" ")) {
713 end = end.substring(1);
714 }
715 return new String[]{prefix, end};
716 }
717
718
719
720
721
722
723
724 public static Set<String> getTypesList(String str) {
725
726 Set<String> results = new HashSet<>();
727
728
729 String localStr = str.replaceAll("([\\w\\?]+ extends|[\\w\\?]+ super|new)", "");
730
731
732 localStr = localStr.replaceAll("[^\\w\\.]", " ");
733
734
735 String[] strArray = localStr.split("\\s+");
736 for (String strImport : strArray) {
737 String localStrImport = strImport.trim();
738 if (!localStrImport.isEmpty()) {
739 results.add(localStrImport);
740 }
741 }
742
743 return results;
744 }
745
746
747
748
749
750
751
752 public static String convertVariableNameToConstantName(String variableName) {
753
754
755
756 StringBuilder buffer = new StringBuilder();
757 boolean lastCarIsUp = false;
758 for (int i = 0, j = variableName.length(); i < j; i++) {
759 char c = variableName.charAt(i);
760 boolean carIsUp = Character.isUpperCase(c);
761 if (i > 0 && !lastCarIsUp && carIsUp) {
762
763 buffer.append('_');
764 }
765 if (carIsUp) {
766 buffer.append(c);
767 } else {
768 buffer.append(Character.toUpperCase(c));
769 }
770 lastCarIsUp = carIsUp;
771 }
772 return buffer.toString();
773 }
774
775
776
777
778
779
780
781
782 public static String convertConstantNameToVariableName(String constantName) {
783 StringBuilder buffer = new StringBuilder();
784 boolean lastCarIsUp = false;
785 for (int i = 0, j = constantName.length(); i < j; i++) {
786 char c = constantName.charAt(i);
787 if ('_' == c) {
788 lastCarIsUp = true;
789 continue;
790 }
791 if (lastCarIsUp) {
792 c = Character.toUpperCase(c);
793 lastCarIsUp = false;
794 } else {
795 c = Character.toLowerCase(c);
796 }
797 buffer.append(c);
798 }
799 return buffer.toString();
800 }
801
802
803
804
805
806
807
808
809
810
811 public static String getAssocAttrName(ObjectModelAttribute attr) {
812 String typeName = attr.getType().substring(
813 attr.getType().lastIndexOf(".") + 1);
814 String result = attr.getName();
815 if (attr.getName().equalsIgnoreCase(typeName)) {
816 result += StringUtils.capitalize(
817 attr.getAssociationClass().getName());
818 }
819 return result;
820 }
821
822
823
824
825
826
827
828 public static long generateSerialVersionUID(ObjectModelClassifier classifier) {
829 long result = SerialVersionUIDBuilder.computeDefaultSUID(classifier);
830 return result;
831 }
832
833 }