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 @file:Suppress("UnstableApiUsage")
17 
18 package androidx.build.lint
19 
20 import com.android.tools.lint.client.api.UElementHandler
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.Incident
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.Scope
28 import com.android.tools.lint.detector.api.Severity
29 import com.intellij.psi.PsiClass
30 import com.intellij.psi.PsiField
31 import com.intellij.psi.PsiMethod
32 import org.jetbrains.uast.UElement
33 import org.jetbrains.uast.UImportStatement
34 import org.jetbrains.uast.UQualifiedReferenceExpression
35 
36 class BanConcurrentHashMap : Detector(), Detector.UastScanner {
37 
getApplicableUastTypesnull38     override fun getApplicableUastTypes(): List<Class<out UElement>> =
39         listOf(UImportStatement::class.java, UQualifiedReferenceExpression::class.java)
40 
41     override fun createUastHandler(context: JavaContext): UElementHandler =
42         object : UElementHandler() {
43 
44             /**
45              * Detect map construction using fully qualified reference if not imported. This
46              * specifically flags the constructor, and not usages of the map after it is created.
47              */
48             override fun visitQualifiedReferenceExpression(node: UQualifiedReferenceExpression) {
49                 val resolved = node.resolve()
50                 // In Kotlin, the resolved node will be a method with name ConcurrentHashMap
51                 // In Java, it will be the class itself
52                 if (
53                     (resolved is PsiMethod && resolved.isConcurrentHashMapConstructor()) ||
54                         (resolved is PsiClass && resolved.isConcurrentHashMap())
55                 ) {
56                     reportIncidentForNode(node)
57                 }
58             }
59 
60             /** Detect import. */
61             override fun visitImportStatement(node: UImportStatement) {
62                 if (node.importReference != null) {
63                     var resolved = node.resolve()
64                     if (resolved is PsiField) {
65                         resolved = resolved.containingClass
66                     } else if (resolved is PsiMethod) {
67                         resolved = resolved.containingClass
68                     }
69 
70                     if (resolved is PsiClass && resolved.isConcurrentHashMap()) {
71                         reportIncidentForNode(node)
72                     }
73                 }
74             }
75 
76             /** Reports an error for ConcurrentHashMap usage at the node's location. */
77             private fun reportIncidentForNode(node: UElement) {
78                 val incident =
79                     Incident(context)
80                         .issue(ISSUE)
81                         .location(context.getLocation(node))
82                         .message("Detected ConcurrentHashMap usage.")
83                         .scope(node)
84                 context.report(incident)
85             }
86 
87             /**
88              * Check if the method is the constructor for ConcurrentHashMap (applicable for Kotlin).
89              */
90             private fun PsiMethod.isConcurrentHashMapConstructor(): Boolean {
91                 return name == CONCURRENT_HASHMAP &&
92                     (containingClass?.isConcurrentHashMap() ?: false)
93             }
94 
95             /** Checks if the class is ConcurrentHashMap. */
96             private fun PsiClass.isConcurrentHashMap(): Boolean {
97                 return qualifiedName == CONCURRENT_HASHMAP_QUALIFIED_NAME
98             }
99         }
100 
101     companion object {
102         val ISSUE =
103             Issue.create(
104                 "BanConcurrentHashMap",
105                 "ConcurrentHashMap usage is not allowed",
106                 "ConcurrentHashMap has an issue on Android’s Lollipop release that can lead to lost" +
107                     " updates under thread contention.",
108                 Category.CORRECTNESS,
109                 5,
110                 Severity.ERROR,
111                 Implementation(BanConcurrentHashMap::class.java, Scope.JAVA_FILE_SCOPE)
112             )
113         const val CONCURRENT_HASHMAP_QUALIFIED_NAME = "java.util.concurrent.ConcurrentHashMap"
114         const val CONCURRENT_HASHMAP = "ConcurrentHashMap"
115     }
116 }
117