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.getModifiers(), field.getAnnotations(), field.getType(), Collections.singletonList(v));
057                    f.setJavaDoc(field.getJavaDoc());
058                    f.setComment(field.getComment());
059                    f.setParentNode(field.getParentNode());
060                    deconstructed.add(f);
061                }
062                parent.getMembers().addAll(index, deconstructed);
063            }
064        }
065    }
066
067    public static List<FieldDeclaration> getFieldMembers(List<BodyDeclaration> members) {
068        if (members != null) {
069            List<FieldDeclaration> fields = new ArrayList<FieldDeclaration>();
070            for (BodyDeclaration member : members) {
071                if (member instanceof FieldDeclaration) {
072                    fields.add((FieldDeclaration) member);
073                }
074            }
075            return fields;
076        }
077        return Collections.emptyList();
078    }
079
080    public static String getFieldName(FieldDeclaration field) {
081        if (field.getVariables().size() > 1) {
082            throw new IllegalArgumentException("cannot handle multiple variable declarations on a single line.  This should have been cleaned up earlier.");
083        }
084
085        return field.getVariables().get(0).getId().getName();
086    }
087
088    public static void sortImports(List<ImportDeclaration> imports) {
089        Collections.sort(imports, new Comparator<ImportDeclaration>() {
090            @Override
091            public int compare(ImportDeclaration o1, ImportDeclaration o2) {
092                return o1.toString().compareTo(o2.toString());
093            }
094        });
095    }
096}