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.currysrc.api.process.Context;
     19 import com.google.currysrc.api.process.Processor;
     20 
     21 import org.eclipse.jdt.core.dom.ASTVisitor;
     22 import org.eclipse.jdt.core.dom.CompilationUnit;
     23 import org.eclipse.jdt.core.dom.Name;
     24 import org.eclipse.jdt.core.dom.QualifiedName;
     25 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
     26 
     27 /**
     28  * Changes any qualified names in the AST that start with {@code oldPrefix} to start with
     29  * {@code newPrefix} instead.
     30  */
     31 public final class ModifyQualifiedNames implements Processor {
     32 
     33   private final String oldPrefix;
     34 
     35   private final String newPrefix;
     36 
     37   public ModifyQualifiedNames(String oldPrefix, String newPrefix) {
     38     this.oldPrefix = oldPrefix;
     39     this.newPrefix = newPrefix;
     40   }
     41 
     42   @Override
     43   public void process(Context context, CompilationUnit cu) {
     44     final ASTRewrite rewrite = context.rewrite();
     45     ASTVisitor visitor = new ASTVisitor(true /* visitDocTags */) {
     46       @Override
     47       public boolean visit(QualifiedName node) {
     48         Name qualifier = node.getQualifier();
     49         if (qualifier != null) {
     50           String fullyQualifiedName = qualifier.getFullyQualifiedName();
     51           if (fullyQualifiedName.startsWith(oldPrefix)) {
     52             String newQualifierString = newPrefix + fullyQualifiedName
     53                 .substring(oldPrefix.length());
     54             Name newQualifier = node.getAST().newName(newQualifierString);
     55             rewrite.replace(qualifier, newQualifier, null /* editGroup */);
     56           }
     57         }
     58         return false;
     59       }
     60     };
     61     cu.accept(visitor);
     62   }
     63 
     64   @Override
     65   public String toString() {
     66     return "ModifyQualifiedNames{" +
     67         "oldPrefix='" + oldPrefix + '\'' +
     68         ", newPrefix='" + newPrefix + '\'' +
     69         '}';
     70   }
     71 }
     72