13b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin/*
23b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * Copyright (C) 2017 The Android Open Source Project
33b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin *
43b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
53b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * you may not use this file except in compliance with the License.
63b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * You may obtain a copy of the License at
73b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin *
83b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin *      http://www.apache.org/licenses/LICENSE-2.0
93b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin *
103b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * Unless required by applicable law or agreed to in writing, software
113b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
123b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * See the License for the specific language governing permissions and
143b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * limitations under the License.
153b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin */
163b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinpackage com.google.currysrc.processors;
173b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
183b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport com.google.common.collect.Lists;
193b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport com.google.currysrc.api.process.Context;
203b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport com.google.currysrc.api.process.JavadocUtils;
213b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport com.google.currysrc.api.process.Processor;
223b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport com.google.currysrc.api.process.ast.TypeLocator;
233b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport java.util.ArrayList;
243b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport java.util.List;
253b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.AST;
263b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.ASTVisitor;
273b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
283b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
293b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.CompilationUnit;
303b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.EnumDeclaration;
313b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.Modifier;
323b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.TypeDeclaration;
333b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
343b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinimport org.eclipse.jdt.core.dom.rewrite.ListRewrite;
353b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
363b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin/**
373b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin * Makes classes public.
383b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin */
393b84259b78fc811b14079dfde655d68a389b36dbPaul Duffinpublic final class MakeClassesPublic implements Processor {
403b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
413b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin  private final List<TypeLocator> whitelist;
423b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
433b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin  private final String reason;
443b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
453b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin  public MakeClassesPublic(List<TypeLocator> whitelist, String reason) {
463b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    this.whitelist = whitelist;
473b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    this.reason = reason;
483b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin  }
493b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
503b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin  @Override public final void process(Context context, CompilationUnit cu) {
513b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    final List<AbstractTypeDeclaration> toMakePublic = new ArrayList<>();
523b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    cu.accept(new ASTVisitor() {
533b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      @Override
543b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      public boolean visit(TypeDeclaration node) {
553b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin        return visitAbstract(node);
563b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      }
573b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
583b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      @Override
593b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      public boolean visit(EnumDeclaration node) {
603b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin        return visitAbstract(node);
613b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      }
623b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin
633b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      private boolean visitAbstract(AbstractTypeDeclaration node) {
643b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin        for (TypeLocator whitelistedType : whitelist) {
653b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin          if (whitelistedType.matches(node)) {
663b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin            toMakePublic.add(node);
673b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin            break;
683b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin          }
693b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin        }
703b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin        return false;
713b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      }
723b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    });
733b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    ASTRewrite rewrite = context.rewrite();
743b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    for (AbstractTypeDeclaration node : Lists.reverse(toMakePublic)) {
753b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      JavadocUtils.addJavadocTag(rewrite, node, reason);
763b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      ChildListPropertyDescriptor modifiersProperty = node.getModifiersProperty();
773b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      AST ast = rewrite.getAST();
783b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      ListRewrite listRewrite = rewrite.getListRewrite(node, modifiersProperty);
793b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      // This will not work properly if the node already has a modifier such as protected or
803b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      // private but as this is intended for handling package private classes that is not an issue.
813b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin      listRewrite.insertAt(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), 0, null);
823b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin    }
833b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin  }
843b84259b78fc811b14079dfde655d68a389b36dbPaul Duffin}
85