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 package androidx.appcompat.widget
18 
19 import com.android.tools.lint.client.api.UElementHandler
20 import com.android.tools.lint.detector.api.Category
21 import com.android.tools.lint.detector.api.Detector
22 import com.android.tools.lint.detector.api.Implementation
23 import com.android.tools.lint.detector.api.Issue
24 import com.android.tools.lint.detector.api.JavaContext
25 import com.android.tools.lint.detector.api.Scope
26 import com.android.tools.lint.detector.api.Severity
27 import com.intellij.psi.PsiType
28 import org.jetbrains.uast.UClass
29 import org.jetbrains.uast.UElement
30 import org.jetbrains.uast.UVariable
31 
32 @Suppress("UnstableApiUsage")
33 class SwitchUsageCodeDetector : Detector(), Detector.UastScanner {
34     companion object {
35         private const val USING_CORE_SWITCH_DESCRIPTION =
36             "Use `SwitchCompat` from AppCompat or `MaterialSwitch` from Material library"
37 
38         internal val USING_CORE_SWITCH_CODE: Issue =
39             Issue.create(
40                 "UseSwitchCompatOrMaterialCode",
41                 "Replace usage of `Switch` widget",
42                 USING_CORE_SWITCH_DESCRIPTION,
43                 Category.CORRECTNESS,
44                 5,
45                 Severity.WARNING,
46                 Implementation(SwitchUsageCodeDetector::class.java, Scope.JAVA_FILE_SCOPE)
47             )
48     }
49 
getApplicableUastTypesnull50     override fun getApplicableUastTypes() =
51         listOf<Class<out UElement>>(UVariable::class.java, UClass::class.java)
52 
53     override fun createUastHandler(context: JavaContext) = SwitchUsageUastHandler(context)
54 
55     class SwitchUsageUastHandler(private val context: JavaContext) : UElementHandler() {
56         override fun visitClass(node: UClass) =
57             node.uastSuperTypes.forEach { checkAndReport(it.type, it) }
58 
59         override fun visitVariable(node: UVariable) = checkAndReport(node.type, node)
60 
61         private fun checkAndReport(type: PsiType, node: UElement) {
62             if (context.evaluator.typeMatches(type, "android.widget.Switch")) {
63                 context.report(
64                     USING_CORE_SWITCH_CODE,
65                     node,
66                     context.getLocation(node),
67                     USING_CORE_SWITCH_DESCRIPTION
68                 )
69             }
70         }
71     }
72 }
73