1 /* 2 * Copyright 2020 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 17 @file:Suppress("UnstableApiUsage") 18 19 package androidx.work.lint 20 21 import com.android.tools.lint.detector.api.Category 22 import com.android.tools.lint.detector.api.Context 23 import com.android.tools.lint.detector.api.Detector 24 import com.android.tools.lint.detector.api.Implementation 25 import com.android.tools.lint.detector.api.Issue 26 import com.android.tools.lint.detector.api.JavaContext 27 import com.android.tools.lint.detector.api.Location 28 import com.android.tools.lint.detector.api.Scope 29 import com.android.tools.lint.detector.api.Severity 30 import com.android.tools.lint.detector.api.SourceCodeScanner 31 import com.intellij.lang.jvm.JvmModifier 32 import com.intellij.psi.PsiMethod 33 import java.util.EnumSet 34 import org.jetbrains.uast.UCallExpression 35 import org.jetbrains.uast.UClass 36 37 class WorkerHasPublicModifierDetector : Detector(), SourceCodeScanner { 38 companion object { 39 private const val DESCRIPTION = 40 "ListenableWorkers constructed using the default WorkerFactories need to be public" 41 42 val ISSUE = 43 Issue.create( 44 id = "WorkerHasAPublicModifier", 45 briefDescription = DESCRIPTION, 46 explanation = 47 """ 48 When you define a ListenableWorker which is constructed using the 49 default WorkerFactory, the ListenableWorker sub-type needs to be public. 50 """, 51 androidSpecific = true, 52 category = Category.CORRECTNESS, 53 severity = Severity.FATAL, 54 implementation = 55 Implementation( 56 WorkerHasPublicModifierDetector::class.java, 57 EnumSet.of(Scope.JAVA_FILE) 58 ) 59 ) 60 } 61 getApplicableMethodNamesnull62 override fun getApplicableMethodNames(): List<String> = listOf("setWorkerFactory") 63 64 override fun applicableSuperClasses() = listOf("androidx.work.ListenableWorker") 65 66 private var hasCustomWorkerFactory = false 67 private val workers = mutableListOf<Pair<UClass, Location>>() 68 69 override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { 70 if (context.evaluator.isMemberInClass(method, "androidx.work.Configuration.Builder")) { 71 hasCustomWorkerFactory = true 72 } 73 } 74 visitClassnull75 override fun visitClass(context: JavaContext, declaration: UClass) { 76 if (declaration.hasModifier(JvmModifier.ABSTRACT) || declaration.isInterface) { 77 // Exempt base types from analysis 78 return 79 } 80 81 if (!declaration.hasModifier(JvmModifier.PUBLIC)) { 82 workers += Pair(declaration, context.getNameLocation(declaration)) 83 } 84 } 85 afterCheckRootProjectnull86 override fun afterCheckRootProject(context: Context) { 87 if (!hasCustomWorkerFactory && workers.isNotEmpty()) { 88 for ((declaration, location) in workers) { 89 context.report( 90 issue = ISSUE, 91 location = location, 92 message = "${declaration.qualifiedName} needs to be public" 93 ) 94 } 95 } 96 } 97 } 98