001/** 002 * Copyright 2005-2015 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.devtools.jpa.eclipselink.conv.parser; 017 018import japa.parser.ast.ImportDeclaration; 019import japa.parser.ast.body.BodyDeclaration; 020import japa.parser.ast.body.ClassOrInterfaceDeclaration; 021import japa.parser.ast.body.FieldDeclaration; 022import japa.parser.ast.body.VariableDeclarator; 023 024import java.util.ArrayList; 025import java.util.Collection; 026import java.util.Collections; 027import java.util.Comparator; 028import java.util.List; 029 030public final class ParserUtil { 031 032 private ParserUtil() { 033 throw new UnsupportedOperationException("do not call"); 034 } 035 036 /** 037 * In Java variables can be defined like the following: 038 * int i, j, k; 039 * 040 * When mapping fields in xml this is not a problem. However when using annotation on a field, 041 * Each field should be defined separately. This helper will deconstruct these fields such 042 * that later AST analysis will not need to account for field defined on a separate line. 043 */ 044 public static void deconstructMultiDeclarations(Collection<FieldDeclaration> fields) { 045 046 for (FieldDeclaration field : fields) { 047 048 ClassOrInterfaceDeclaration parent = (ClassOrInterfaceDeclaration) field.getParentNode(); 049 050 //these are chained together 051 if (field.getVariables().size() > 1) { 052 int index = parent.getMembers().indexOf(field); 053 parent.getMembers().remove(index); 054 List<FieldDeclaration> deconstructed = new ArrayList<FieldDeclaration>(); 055 for (VariableDeclarator v : field.getVariables()) { 056 FieldDeclaration f = new FieldDeclaration(field.getJavaDoc(), field.getModifiers(), field.getAnnotations(), field.getType(), Collections.singletonList(v)); 057 f.setComment(field.getComment()); 058 f.setParentNode(field.getParentNode()); 059 deconstructed.add(f); 060 } 061 parent.getMembers().addAll(index, deconstructed); 062 } 063 } 064 } 065 066 public static List<FieldDeclaration> getFieldMembers(List<BodyDeclaration> members) { 067 if (members != null) { 068 List<FieldDeclaration> fields = new ArrayList<FieldDeclaration>(); 069 for (BodyDeclaration member : members) { 070 if (member instanceof FieldDeclaration) { 071 fields.add((FieldDeclaration) member); 072 } 073 } 074 return fields; 075 } 076 return Collections.emptyList(); 077 } 078 079 public static String getFieldName(FieldDeclaration field) { 080 if (field.getVariables().size() > 1) { 081 throw new IllegalArgumentException("cannot handle multiple variable declarations on a single line. This should have been cleaned up earlier."); 082 } 083 084 return field.getVariables().get(0).getId().getName(); 085 } 086 087 public static void sortImports(List<ImportDeclaration> imports) { 088 Collections.sort(imports, new Comparator<ImportDeclaration>() { 089 @Override 090 public int compare(ImportDeclaration o1, ImportDeclaration o2) { 091 return o1.toString().compareTo(o2.toString()); 092 } 093 }); 094 } 095}