1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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 com.google.currysrc.processors; 17 18 import com.google.common.collect.Lists; 19 import com.google.currysrc.api.process.Context; 20 import com.google.currysrc.api.process.Processor; 21 import com.google.currysrc.api.process.Reporter; 22 23 import org.eclipse.jdt.core.dom.Comment; 24 import org.eclipse.jdt.core.dom.CompilationUnit; 25 import org.eclipse.jdt.core.dom.Javadoc; 26 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 27 28 import java.util.List; 29 30 /** 31 * A base class for processors that process Javadoc nodes and may rewrite the AST. All Javadoc nodes 32 * in a {@code CompilationUnit} are considered. 33 */ 34 public abstract class BaseJavadocNodeScanner implements Processor { 35 36 @Override public final void process(Context context, CompilationUnit cu) { 37 // This could just call cu.visit() but iterating over the comments should be more efficient. 38 List<Comment> comments = cu.getCommentList(); 39 ASTRewrite rewrite = context.rewrite(); 40 Reporter reporter = context.reporter(); 41 for (Comment comment : Lists.reverse(comments)) { 42 if (comment instanceof Javadoc) { 43 Javadoc javadoc = (Javadoc) comment; 44 visit(reporter, javadoc, rewrite); 45 } 46 } 47 } 48 49 protected abstract void visit(Reporter reporter, Javadoc javadoc, ASTRewrite rewrite); 50 } 51