View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * In Java variables can be defined like the following:
38       * int i, j, k;
39       *
40       * When mapping fields in xml this is not a problem.  However when using annotation on a field,
41       * Each field should be defined separately.  This helper will deconstruct these fields such
42       * that later AST analysis will not need to account for field defined on a separate line.
43       */
44      public static void deconstructMultiDeclarations(Collection<FieldDeclaration> fields) {
45  
46          for (FieldDeclaration field : fields) {
47  
48              ClassOrInterfaceDeclaration parent = (ClassOrInterfaceDeclaration) field.getParentNode();
49  
50              //these are chained together
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  }