Home | History | Annotate | Download | only in processors
      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.JavadocUtils;
     21 import com.google.currysrc.api.process.Processor;
     22 
     23 import org.eclipse.jdt.core.dom.ASTVisitor;
     24 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
     25 import org.eclipse.jdt.core.dom.CompilationUnit;
     26 import org.eclipse.jdt.core.dom.EnumDeclaration;
     27 import org.eclipse.jdt.core.dom.TypeDeclaration;
     28 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
     29 
     30 import java.util.List;
     31 
     32 /**
     33  * Inserts the tag text to the Javadoc for any type declaration that matches
     34  * {@link #mustTag(AbstractTypeDeclaration)}.
     35  */
     36 public abstract class BaseJavadocTagClasses implements Processor {
     37 
     38   private final String tagText;
     39 
     40   protected BaseJavadocTagClasses(String tagText) {
     41     this.tagText = tagText;
     42   }
     43 
     44   @Override public final void process(Context context, CompilationUnit cu) {
     45     final List<AbstractTypeDeclaration> toHide = Lists.newArrayList();
     46     cu.accept(new ASTVisitor() {
     47       @Override
     48       public boolean visit(TypeDeclaration node) {
     49         return visitAbstract(node);
     50       }
     51 
     52       @Override
     53       public boolean visit(EnumDeclaration node) {
     54         return visitAbstract(node);
     55       }
     56 
     57       private boolean visitAbstract(AbstractTypeDeclaration node) {
     58         if (mustTag(node)) {
     59           toHide.add(node);
     60         }
     61         return false;
     62       }
     63     });
     64     ASTRewrite rewrite = context.rewrite();
     65     for (AbstractTypeDeclaration node : Lists.reverse(toHide)) {
     66       JavadocUtils.addJavadocTag(rewrite, node, tagText);
     67     }
     68   }
     69 
     70   protected abstract boolean mustTag(AbstractTypeDeclaration node);
     71 }
     72