1 /* 2 * Copyright (C) 2017 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 import com.google.currysrc.api.process.ast.TypeLocator; 23 import java.util.ArrayList; 24 import java.util.List; 25 import org.eclipse.jdt.core.dom.AST; 26 import org.eclipse.jdt.core.dom.ASTVisitor; 27 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 28 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; 29 import org.eclipse.jdt.core.dom.CompilationUnit; 30 import org.eclipse.jdt.core.dom.EnumDeclaration; 31 import org.eclipse.jdt.core.dom.Modifier; 32 import org.eclipse.jdt.core.dom.TypeDeclaration; 33 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 34 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 35 36 /** 37 * Makes classes public. 38 */ 39 public final class MakeClassesPublic implements Processor { 40 41 private final List<TypeLocator> whitelist; 42 43 private final String reason; 44 45 public MakeClassesPublic(List<TypeLocator> whitelist, String reason) { 46 this.whitelist = whitelist; 47 this.reason = reason; 48 } 49 50 @Override public final void process(Context context, CompilationUnit cu) { 51 final List<AbstractTypeDeclaration> toMakePublic = new ArrayList<>(); 52 cu.accept(new ASTVisitor() { 53 @Override 54 public boolean visit(TypeDeclaration node) { 55 return visitAbstract(node); 56 } 57 58 @Override 59 public boolean visit(EnumDeclaration node) { 60 return visitAbstract(node); 61 } 62 63 private boolean visitAbstract(AbstractTypeDeclaration node) { 64 for (TypeLocator whitelistedType : whitelist) { 65 if (whitelistedType.matches(node)) { 66 toMakePublic.add(node); 67 break; 68 } 69 } 70 return false; 71 } 72 }); 73 ASTRewrite rewrite = context.rewrite(); 74 for (AbstractTypeDeclaration node : Lists.reverse(toMakePublic)) { 75 JavadocUtils.addJavadocTag(rewrite, node, reason); 76 ChildListPropertyDescriptor modifiersProperty = node.getModifiersProperty(); 77 AST ast = rewrite.getAST(); 78 ListRewrite listRewrite = rewrite.getListRewrite(node, modifiersProperty); 79 // This will not work properly if the node already has a modifier such as protected or 80 // private but as this is intended for handling package private classes that is not an issue. 81 listRewrite.insertAt(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), 0, null); 82 } 83 } 84 } 85