1 /*
2  * Copyright (C) 2018 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.build.lint
20 
21 import com.android.tools.lint.client.api.UElementHandler
22 import com.android.tools.lint.detector.api.Category
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.Incident
26 import com.android.tools.lint.detector.api.Issue
27 import com.android.tools.lint.detector.api.JavaContext
28 import com.android.tools.lint.detector.api.Scope
29 import com.android.tools.lint.detector.api.Severity
30 import org.jetbrains.uast.UAnnotation
31 import org.jetbrains.uast.namePsiElement
32 
33 /** Enforces policy banning use of the `@TargetApi` annotation. */
34 class TargetApiAnnotationUsageDetector : Detector(), Detector.UastScanner {
35 
getApplicableUastTypesnull36     override fun getApplicableUastTypes() = listOf(UAnnotation::class.java)
37 
38     override fun createUastHandler(context: JavaContext): UElementHandler {
39         return AnnotationChecker(context)
40     }
41 
42     private inner class AnnotationChecker(val context: JavaContext) : UElementHandler() {
visitAnnotationnull43         override fun visitAnnotation(node: UAnnotation) {
44             if (node.qualifiedName == "android.annotation.TargetApi") {
45 
46                 // To support Kotlin's type aliases, we need to check the pattern against the symbol
47                 // instead of a constant ("TargetApi") to pass Lint's IMPORT_ALIAS test mode. In the
48                 // case where namePsiElement returns null (which shouldn't happen), fall back to the
49                 // RegEx check.
50                 val searchPattern =
51                     node.namePsiElement?.text ?: "(?:android\\.annotation\\.)?TargetApi"
52 
53                 val lintFix =
54                     fix()
55                         .name("Replace with `@RequiresApi`")
56                         .replace()
57                         .pattern(searchPattern)
58                         .with("androidx.annotation.RequiresApi")
59                         .shortenNames()
60                         .autoFix(true, true)
61                         .build()
62                 val incident =
63                     Incident(context)
64                         .fix(lintFix)
65                         .issue(ISSUE)
66                         .location(context.getNameLocation(node))
67                         .message("Use `@RequiresApi` instead of `@TargetApi`")
68                         .scope(node)
69                 context.report(incident)
70             }
71         }
72     }
73 
74     companion object {
75         val ISSUE =
76             Issue.create(
77                 "BanTargetApiAnnotation",
78                 "Replace usage of `@TargetApi` with `@RequiresApi`",
79                 "The `@TargetApi` annotation satisfies the `NewApi` lint check, but it does " +
80                     "not ensure that calls to the annotated API are correctly guarded on an `SDK_INT`" +
81                     " (or equivalent) check. Instead, use the `@RequiresApi` annotation to ensure " +
82                     "that all calls are correctly guarded.",
83                 Category.CORRECTNESS,
84                 5,
85                 Severity.ERROR,
86                 Implementation(TargetApiAnnotationUsageDetector::class.java, Scope.JAVA_FILE_SCOPE)
87             )
88     }
89 }
90