1 /* 2 * Copyright (C) 2018 The Dagger Authors. 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 17 package dagger.errorprone; 18 19 import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; 20 21 import com.google.errorprone.BugPattern; 22 import com.google.errorprone.VisitorState; 23 import com.google.errorprone.bugpatterns.BugChecker; 24 import com.google.errorprone.bugpatterns.BugChecker.MemberSelectTreeMatcher; 25 import com.google.errorprone.fixes.SuggestedFix; 26 import com.google.errorprone.matchers.Description; 27 import com.google.errorprone.matchers.Matcher; 28 import com.google.errorprone.matchers.Matchers; 29 import com.google.errorprone.util.ASTHelpers; 30 import com.sun.source.tree.ExpressionTree; 31 import com.sun.source.tree.MemberSelectTree; 32 import com.sun.tools.javac.code.Symbol; 33 34 /** A refactoring to update AndroidInjector bindings to their new form. */ 35 @BugPattern( 36 name = "AndroidSupportInjectionModuleMigrator", 37 summary = "Inlines usages of AndroidSupportInjectionModule to AndroidInjectionModule", 38 explanation = 39 "AndroidSupportInjectionModule is now an empty module and acts as an alias for " 40 + "AndroidInjectionModule. This migration rewrites usages of the former to the latter.", 41 severity = SUGGESTION) 42 public final class AndroidSupportInjectionModuleMigrator extends BugChecker 43 implements MemberSelectTreeMatcher { 44 private static final Matcher<ExpressionTree> MODULE_CLASS_LITERAL = 45 Matchers.classLiteral( 46 (ExpressionTree expressionTree, VisitorState state) -> { 47 Symbol symbol = ASTHelpers.getSymbol(expressionTree); 48 if (symbol == null) { 49 return false; 50 } 51 return symbol 52 .getQualifiedName() 53 .contentEquals("dagger.android.support.AndroidSupportInjectionModule"); 54 }); 55 56 @Override matchMemberSelect(MemberSelectTree tree, VisitorState state)57 public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { 58 if (MODULE_CLASS_LITERAL.matches(tree, state)) { 59 return describeMatch( 60 tree, 61 SuggestedFix.builder() 62 .replace(tree, "AndroidInjectionModule.class") 63 .addImport("dagger.android.AndroidInjectionModule") 64 .build()); 65 } 66 return Description.NO_MATCH; 67 } 68 } 69