Home | History | Annotate | Download | only in srcgen
      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.android.icu4j.srcgen;
     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 import com.google.currysrc.api.process.ast.BodyDeclarationLocator;
     23 import com.google.currysrc.api.process.ast.StartPositionComparator;
     24 
     25 import org.eclipse.jdt.core.dom.BodyDeclaration;
     26 import org.eclipse.jdt.core.dom.CompilationUnit;
     27 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
     28 
     29 import java.util.Collections;
     30 import java.util.List;
     31 
     32 /**
     33  * Adds a javadoc tag to {@link BodyDeclaration}s that match a list of locators.
     34  */
     35 public class TagMatchingDeclarations implements Processor {
     36   private final List<BodyDeclarationLocator> locatorList;
     37   private final String tagComment;
     38 
     39   public TagMatchingDeclarations(List<BodyDeclarationLocator> locatorList, String tagComment) {
     40     this.locatorList = locatorList;
     41     this.tagComment = tagComment;
     42   }
     43 
     44   @Override public void process(Context context, CompilationUnit cu) {
     45     List<BodyDeclaration> matchingNodes = Lists.newArrayList();
     46     // This is inefficient but it is very simple.
     47     for (BodyDeclarationLocator locator : locatorList) {
     48       BodyDeclaration bodyDeclaration = locator.find(cu);
     49       if (bodyDeclaration != null) {
     50         matchingNodes.add(bodyDeclaration);
     51       }
     52     }
     53     // Tackle nodes in reverse order to avoid messing up the ASTNode offsets.
     54     Collections.sort(matchingNodes, new StartPositionComparator());
     55     ASTRewrite rewrite = context.rewrite();
     56     for (BodyDeclaration bodyDeclaration : Lists.reverse(matchingNodes)) {
     57       JavadocUtils.addJavadocTag(rewrite, bodyDeclaration, tagComment);
     58     }
     59   }
     60 
     61 
     62   @Override public String toString() {
     63     return "TagDeclarations{" +
     64         "locatorList=" + locatorList +
     65         "tagComment=" + tagComment +
     66         '}';
     67   }
     68 }
     69