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