1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.devtools.jpa.eclipselink.conv.parser;
17
18 import japa.parser.ast.ImportDeclaration;
19 import japa.parser.ast.body.BodyDeclaration;
20 import japa.parser.ast.body.ClassOrInterfaceDeclaration;
21 import japa.parser.ast.body.FieldDeclaration;
22 import japa.parser.ast.body.VariableDeclarator;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
29
30 public final class ParserUtil {
31
32 private ParserUtil() {
33 throw new UnsupportedOperationException("do not call");
34 }
35
36
37
38
39
40
41
42
43
44 public static void deconstructMultiDeclarations(Collection<FieldDeclaration> fields) {
45
46 for (FieldDeclaration field : fields) {
47
48 ClassOrInterfaceDeclaration parent = (ClassOrInterfaceDeclaration) field.getParentNode();
49
50
51 if (field.getVariables().size() > 1) {
52 int index = parent.getMembers().indexOf(field);
53 parent.getMembers().remove(index);
54 List<FieldDeclaration> deconstructed = new ArrayList<FieldDeclaration>();
55 for (VariableDeclarator v : field.getVariables()) {
56 FieldDeclaration f = new FieldDeclaration(field.getJavaDoc(), field.getModifiers(), field.getAnnotations(), field.getType(), Collections.singletonList(v));
57 f.setComment(field.getComment());
58 f.setParentNode(field.getParentNode());
59 deconstructed.add(f);
60 }
61 parent.getMembers().addAll(index, deconstructed);
62 }
63 }
64 }
65
66 public static List<FieldDeclaration> getFieldMembers(List<BodyDeclaration> members) {
67 if (members != null) {
68 List<FieldDeclaration> fields = new ArrayList<FieldDeclaration>();
69 for (BodyDeclaration member : members) {
70 if (member instanceof FieldDeclaration) {
71 fields.add((FieldDeclaration) member);
72 }
73 }
74 return fields;
75 }
76 return Collections.emptyList();
77 }
78
79 public static String getFieldName(FieldDeclaration field) {
80 if (field.getVariables().size() > 1) {
81 throw new IllegalArgumentException("cannot handle multiple variable declarations on a single line. This should have been cleaned up earlier.");
82 }
83
84 return field.getVariables().get(0).getId().getName();
85 }
86
87 public static void sortImports(List<ImportDeclaration> imports) {
88 Collections.sort(imports, new Comparator<ImportDeclaration>() {
89 @Override
90 public int compare(ImportDeclaration o1, ImportDeclaration o2) {
91 return o1.toString().compareTo(o2.toString());
92 }
93 });
94 }
95 }