| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| *******************************************************************************/ |
| package org.eclipse.apogy.tools; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.jdt.core.dom.ASTVisitor; |
| import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; |
| import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; |
| import org.eclipse.jdt.core.dom.BlockComment; |
| import org.eclipse.jdt.core.dom.BodyDeclaration; |
| import org.eclipse.jdt.core.dom.EnumConstantDeclaration; |
| import org.eclipse.jdt.core.dom.EnumDeclaration; |
| import org.eclipse.jdt.core.dom.FieldDeclaration; |
| import org.eclipse.jdt.core.dom.Initializer; |
| import org.eclipse.jdt.core.dom.Javadoc; |
| import org.eclipse.jdt.core.dom.MethodDeclaration; |
| import org.eclipse.jdt.core.dom.PackageDeclaration; |
| import org.eclipse.jdt.core.dom.TagElement; |
| |
| public class CustomASTVisitor extends ASTVisitor { |
| |
| private final List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>(); |
| private final List<FieldDeclaration> fields = new ArrayList<FieldDeclaration>(); |
| private final List<BodyDeclaration> otherBodyDeclarations = new ArrayList<BodyDeclaration>(); |
| private final List<PackageDeclaration> packageDeclarations = new ArrayList<PackageDeclaration>(); |
| private final List<Javadoc> javadocDeclarations = new ArrayList<Javadoc>(); |
| private final List<BlockComment> blockComments = new ArrayList<BlockComment>(); |
| |
| public static boolean contains(Javadoc javadoc, String key) { |
| boolean result = false; |
| if (javadoc != null) { |
| @SuppressWarnings("unchecked") |
| List<TagElement> tags = javadoc.tags(); |
| int counter = 0; |
| while (!result && counter < tags.size()) { |
| if (tags.get(counter).getTagName() != null) { |
| result = tags.get(counter).getTagName().equals(key); |
| } |
| counter++; |
| } |
| } |
| return result; |
| } |
| |
| public CustomASTVisitor() { |
| super(true); |
| } |
| |
| public List<BodyDeclaration> getOtherBodyDeclarations() { |
| return this.otherBodyDeclarations; |
| } |
| |
| @Override |
| public boolean visit(AnnotationTypeDeclaration node) { |
| getOtherBodyDeclarations().add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(EnumDeclaration node) { |
| getOtherBodyDeclarations().add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(AnnotationTypeMemberDeclaration node) { |
| getOtherBodyDeclarations().add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(EnumConstantDeclaration node) { |
| getOtherBodyDeclarations().add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(Initializer node) { |
| getOtherBodyDeclarations().add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(MethodDeclaration node) { |
| this.methods.add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(FieldDeclaration node) { |
| this.fields.add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(PackageDeclaration node) { |
| this.packageDeclarations.add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(Javadoc node) { |
| this.javadocDeclarations.add(node); |
| return super.visit(node); |
| } |
| |
| @Override |
| public boolean visit(BlockComment node) { |
| this.blockComments.add(node); |
| return super.visit(node); |
| } |
| |
| public List<MethodDeclaration> getMethods() { |
| return this.methods; |
| } |
| |
| public List<BlockComment> getBlockComments() { |
| return this.blockComments; |
| } |
| |
| public List<MethodDeclaration> getGeneratedMethods() { |
| List<MethodDeclaration> generatedMethods = new ArrayList<MethodDeclaration>(); |
| |
| for (MethodDeclaration generatedMethod : getMethods()) { |
| if (contains(generatedMethod.getJavadoc(), "@generated")) { |
| generatedMethods.add(generatedMethod); |
| } |
| } |
| return generatedMethods; |
| } |
| |
| public List<PackageDeclaration> getPackageDeclarations() { |
| return this.packageDeclarations; |
| } |
| |
| public List<Javadoc> getJavadocDeclarations() { |
| return this.javadocDeclarations; |
| } |
| |
| public List<MethodDeclaration> getGenMethods() { |
| List<MethodDeclaration> genMethods = new ArrayList<MethodDeclaration>(); |
| |
| for (MethodDeclaration genMethod : getMethods()) { |
| if (genMethod.getName().getIdentifier().toUpperCase().endsWith("GEN")) { |
| genMethods.add(genMethod); |
| } |
| } |
| return genMethods; |
| } |
| |
| public List<FieldDeclaration> getFields() { |
| return this.fields; |
| } |
| |
| public List<FieldDeclaration> getGeneratedFields() { |
| List<FieldDeclaration> generatedFields = new ArrayList<FieldDeclaration>(); |
| |
| for (FieldDeclaration generatedField : getFields()) { |
| if (contains(generatedField.getJavadoc(), "@generated")) { |
| generatedFields.add(generatedField); |
| } |
| } |
| return generatedFields; |
| } |
| |
| public void print(List<? extends BodyDeclaration> bodies) { |
| int counter = 1; |
| for (BodyDeclaration body : bodies) { |
| System.out.println("CustomASTVisitor.print() [" + counter + "] = " + body.toString()); |
| } |
| } |
| } |