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.Detector
23 import com.android.tools.lint.detector.api.Implementation
24 import com.android.tools.lint.detector.api.Issue
25 import com.android.tools.lint.detector.api.JavaContext
26 import com.android.tools.lint.detector.api.LintFix
27 import com.android.tools.lint.detector.api.Scope
28 import com.android.tools.lint.detector.api.Severity
29 import com.android.tools.lint.detector.api.SourceCodeScanner
30 import com.intellij.psi.PsiMethod
31 import java.util.EnumSet
32 import org.jetbrains.uast.UCallExpression
33 
34 class RxWorkerSetProgressDetector : Detector(), SourceCodeScanner {
35     companion object {
36         private const val SET_COMPLETABLE_PROGRESS = "setCompletableProgress"
37 
38         private const val DESCRIPTION =
39             "`setProgress` is deprecated. Use `$SET_COMPLETABLE_PROGRESS` instead."
40 
41         val ISSUE =
42             Issue.create(
43                 id = "UseRxSetProgress2",
44                 briefDescription = DESCRIPTION,
45                 explanation =
46                     """
47                 Use `$SET_COMPLETABLE_PROGRESS(...)` instead of `setProgress(...) in `RxWorker`.
48             """,
49                 androidSpecific = true,
50                 category = Category.CORRECTNESS,
51                 severity = Severity.FATAL,
52                 implementation =
53                     Implementation(
54                         RxWorkerSetProgressDetector::class.java,
55                         EnumSet.of(Scope.JAVA_FILE)
56                     )
57             )
58     }
59 
getApplicableMethodNamesnull60     override fun getApplicableMethodNames(): List<String> = listOf("setProgress")
61 
62     override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
63         if (context.evaluator.isMemberInClass(method, "androidx.work.RxWorker")) {
64             val lintFix =
65                 LintFix.create()
66                     .name("Use $SET_COMPLETABLE_PROGRESS instead")
67                     .replace()
68                     .text(method.name)
69                     .with(SET_COMPLETABLE_PROGRESS)
70                     .independent(true)
71                     .build()
72 
73             context.report(
74                 issue = ISSUE,
75                 location = context.getLocation(node),
76                 message = DESCRIPTION,
77                 quickfixData = lintFix
78             )
79         }
80     }
81 }
82