1 /*
2 * Copyright 2022 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.build.lint.replacewith
18
19 import androidx.build.lint.ReplaceWithDetector
20 import com.android.tools.lint.checks.infrastructure.TestFile
21 import com.android.tools.lint.checks.infrastructure.TestFiles
22 import com.android.tools.lint.checks.infrastructure.TestLintResult
23 import com.android.tools.lint.checks.infrastructure.TestLintTask
24
checknull25 fun check(vararg testFiles: TestFile): TestLintResult {
26 return TestLintTask.lint()
27 .files(ANDROIDX_REPLACE_WITH_KT, ANDROIDX_ANY_THREAD_KT, *testFiles)
28 .issues(ReplaceWithDetector.ISSUE)
29 .run()
30 }
31
32 /** Loads a [TestFile] from Java source code included in the JAR resources. */
javaSamplenull33 fun javaSample(className: String): TestFile {
34 return TestFiles.java(
35 ReplaceWithDetectorMethodTest::class
36 .java
37 .getResource("/java/${className.replace('.', '/')}.java")!!
38 .readText()
39 )
40 }
41
42 /** Loads a [TestFile] from Kotlin source code included in the JAR resources. */
ktSamplenull43 fun ktSample(className: String): TestFile {
44 return TestFiles.kotlin(
45 ReplaceWithDetectorMethodTest::class
46 .java
47 .getResource("/java/${className.replace('.', '/')}.kt")!!
48 .readText()
49 )
50 }
51
52 /**
53 * [TestFile] containing ReplaceWith.kt from the Annotation library.
54 *
55 * This is a workaround for IntelliJ failing to recognize source files if they are also included as
56 * resources.
57 */
58 val ANDROIDX_REPLACE_WITH_KT: TestFile =
59 TestFiles.kotlin(
60 """
61 package androidx.annotation
62
63 @Retention(AnnotationRetention.BINARY)
64 @Target(
65 AnnotationTarget.CLASS,
66 AnnotationTarget.FUNCTION,
67 AnnotationTarget.PROPERTY,
68 AnnotationTarget.ANNOTATION_CLASS,
69 AnnotationTarget.CONSTRUCTOR,
70 AnnotationTarget.PROPERTY_SETTER,
71 AnnotationTarget.PROPERTY_GETTER,
72 AnnotationTarget.TYPEALIAS
73 )
74 @java.lang.annotation.Target(
75 ElementType.CONSTRUCTOR,
76 ElementType.FIELD,
77 ElementType.METHOD,
78 ElementType.TYPE,
79 )
80 annotation class ReplaceWith(
81 val expression: String,
82 vararg val imports: String
83 )
84 """
85 .trimIndent()
86 )
87
88 /** [TestFile] containing AnyThread.kt from the Annotation library. */
89 val ANDROIDX_ANY_THREAD_KT: TestFile =
90 TestFiles.kotlin(
91 """
92 package androidx.annotation
93
94 @MustBeDocumented
95 @Retention(AnnotationRetention.BINARY)
96 @Target(
97 AnnotationTarget.FUNCTION,
98 AnnotationTarget.PROPERTY_GETTER,
99 AnnotationTarget.PROPERTY_SETTER,
100 AnnotationTarget.CONSTRUCTOR,
101 AnnotationTarget.ANNOTATION_CLASS,
102 AnnotationTarget.CLASS,
103 AnnotationTarget.VALUE_PARAMETER
104 )
105 annotation class AnyThread
106 """
107 .trimIndent()
108 )
109