Skip to content

Commit 60bc35b

Browse files
committed
Implemented suppress lint intention action for android lint (KT-12020)
#KT-12020 Fixed
1 parent 6a8f78d commit 60bc35b

28 files changed

+427
-3
lines changed

generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.AbstractDataFlowValueRenderingTest
2222
import org.jetbrains.kotlin.addImport.AbstractAddImportTest
2323
import org.jetbrains.kotlin.android.*
2424
import org.jetbrains.kotlin.android.configure.AbstractConfigureProjectTest
25+
import org.jetbrains.kotlin.android.intentions.AbstractAndroidIntentionTest
2526
import org.jetbrains.kotlin.android.intentions.AbstractAndroidResourceIntentionTest
2627
import org.jetbrains.kotlin.android.quickfixes.AbstractAndroidQuickFixMultiFileTest
2728
import org.jetbrains.kotlin.annotation.AbstractAnnotationProcessorBoxTest
@@ -1161,6 +1162,10 @@ fun main(args: Array<String>) {
11611162
testClass<AbstractKotlinLintTest>() {
11621163
model("android/lint", excludeParentDirs = true)
11631164
}
1165+
1166+
testClass<AbstractAndroidIntentionTest>() {
1167+
model("android/intentions", pattern = "^([\\w\\-_]+)\\.kt$")
1168+
}
11641169
}
11651170

11661171
testGroup("plugins/plugins-tests/tests", "plugins/android-extensions/android-extensions-jps/testData") {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
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 org.jetbrains.kotlin.android.intentions
18+
19+
import com.android.SdkConstants
20+
import com.intellij.openapi.util.io.FileUtil
21+
import com.intellij.util.PathUtil
22+
import org.jetbrains.android.inspections.klint.AndroidLintInspectionBase
23+
import org.jetbrains.kotlin.android.KotlinAndroidTestCase
24+
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
25+
import org.jetbrains.kotlin.psi.KtFile
26+
import org.jetbrains.kotlin.test.InTextDirectivesUtils
27+
import org.jetbrains.kotlin.test.KotlinTestUtils
28+
import java.io.File
29+
30+
31+
abstract class AbstractAndroidIntentionTest : KotlinAndroidTestCase() {
32+
33+
fun doTest(path: String) {
34+
val fileText = FileUtil.loadFile(File(path), true)
35+
val intentionText = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// INTENTION_TEXT: ") ?: error("Empty intention text")
36+
val mainInspectionClassName = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// INSPECTION_CLASS: ") ?: error("Empty inspection class name")
37+
38+
val inspectionClass = Class.forName(mainInspectionClassName).newInstance() as AndroidLintInspectionBase
39+
40+
myFixture.enableInspections(inspectionClass)
41+
42+
val sourceFile = myFixture.copyFileToProject(path, "src/${PathUtil.getFileName(path)}")
43+
myFixture.configureFromExistingVirtualFile(sourceFile)
44+
45+
DirectiveBasedActionUtils.checkForUnexpectedErrors(myFixture.file as KtFile)
46+
47+
val intention = myFixture.getAvailableIntention(intentionText) ?: error("Failed to find intention")
48+
myFixture.launchAction(intention)
49+
50+
myFixture.checkResultByFile(path + ".expected")
51+
}
52+
53+
override fun createManifest() {
54+
myFixture.copyFileToProject("idea/testData/android/AndroidManifest.xml", SdkConstants.FN_ANDROID_MANIFEST_XML)
55+
}
56+
57+
override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory()
58+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
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 org.jetbrains.kotlin.android.intentions;
18+
19+
import com.intellij.testFramework.TestDataPath;
20+
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
21+
import org.jetbrains.kotlin.test.KotlinTestUtils;
22+
import org.jetbrains.kotlin.test.TargetBackend;
23+
import org.jetbrains.kotlin.test.TestMetadata;
24+
import org.junit.runner.RunWith;
25+
26+
import java.io.File;
27+
import java.util.regex.Pattern;
28+
29+
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
30+
@SuppressWarnings("all")
31+
@TestMetadata("idea/testData/android/intentions")
32+
@TestDataPath("$PROJECT_ROOT")
33+
@RunWith(JUnit3RunnerWithInners.class)
34+
public class AndroidIntentionTestGenerated extends AbstractAndroidIntentionTest {
35+
public void testAllFilesPresentInIntentions() throws Exception {
36+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/android/intentions"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
37+
}
38+
39+
@TestMetadata("idea/testData/android/intentions/suppressLint")
40+
@TestDataPath("$PROJECT_ROOT")
41+
@RunWith(JUnit3RunnerWithInners.class)
42+
public static class SuppressLint extends AbstractAndroidIntentionTest {
43+
@TestMetadata("activityMethod.kt")
44+
public void testActivityMethod() throws Exception {
45+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/activityMethod.kt");
46+
doTest(fileName);
47+
}
48+
49+
@TestMetadata("addToExistingAnnotation.kt")
50+
public void testAddToExistingAnnotation() throws Exception {
51+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/addToExistingAnnotation.kt");
52+
doTest(fileName);
53+
}
54+
55+
public void testAllFilesPresentInSuppressLint() throws Exception {
56+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/android/intentions/suppressLint"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
57+
}
58+
59+
@TestMetadata("constructorParameter.kt")
60+
public void testConstructorParameter() throws Exception {
61+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/constructorParameter.kt");
62+
doTest(fileName);
63+
}
64+
65+
@TestMetadata("destructuringDeclaration.kt")
66+
public void testDestructuringDeclaration() throws Exception {
67+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/destructuringDeclaration.kt");
68+
doTest(fileName);
69+
}
70+
71+
@TestMetadata("lambdaArgument.kt")
72+
public void testLambdaArgument() throws Exception {
73+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/lambdaArgument.kt");
74+
doTest(fileName);
75+
}
76+
77+
@TestMetadata("lambdaArgumentProperty.kt")
78+
public void testLambdaArgumentProperty() throws Exception {
79+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/lambdaArgumentProperty.kt");
80+
doTest(fileName);
81+
}
82+
83+
@TestMetadata("methodParameter.kt")
84+
public void testMethodParameter() throws Exception {
85+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/methodParameter.kt");
86+
doTest(fileName);
87+
}
88+
89+
@TestMetadata("propertyWithLambda.kt")
90+
public void testPropertyWithLambda() throws Exception {
91+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/propertyWithLambda.kt");
92+
doTest(fileName);
93+
}
94+
95+
@TestMetadata("simpleProperty.kt")
96+
public void testSimpleProperty() throws Exception {
97+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/intentions/suppressLint/simpleProperty.kt");
98+
doTest(fileName);
99+
}
100+
}
101+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@SuppressLint("SdCardPath")
2+
val a = "/sdcard"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val a = "/sdcard"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
This intention suppresses android lint warnings with @SuppressLint annotation added to closest parent declaration
4+
</body>
5+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
import android.app.Activity
5+
import android.os.Environment
6+
7+
8+
class MainActivity : Activity() {
9+
fun getSdCard(fromEnvironment: Boolean) = if (fromEnvironment) Environment.getExternalStorageDirectory().path else "<caret>/sdcard"
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
import android.annotation.SuppressLint
5+
import android.app.Activity
6+
import android.os.Environment
7+
8+
9+
class MainActivity : Activity() {
10+
@SuppressLint("SdCardPath")
11+
fun getSdCard(fromEnvironment: Boolean) = if (fromEnvironment) Environment.getExternalStorageDirectory().path else "/sdcard"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
import android.annotation.SuppressLint
5+
import android.app.Activity
6+
import android.os.Environment
7+
8+
9+
class MainActivity : Activity() {
10+
@SuppressLint("Something")
11+
fun getSdCard(fromEnvironment: Boolean) = if (fromEnvironment) Environment.getExternalStorageDirectory().path else "<caret>/sdcard"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
import android.annotation.SuppressLint
5+
import android.app.Activity
6+
import android.os.Environment
7+
8+
9+
class MainActivity : Activity() {
10+
@SuppressLint("Something", "SdCardPath")
11+
fun getSdCard(fromEnvironment: Boolean) = if (fromEnvironment) Environment.getExternalStorageDirectory().path else "/sdcard"
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
class SdCard(val path: String = "<caret>/sdcard")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
class SdCard(@SuppressLint("SdCardPath") val path: String = "/sdcard")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
fun foo() {
5+
val (a: String, b: String) = "<caret>/sdcard"
6+
}
7+
8+
operator fun CharSequence.component1(): String = "component1"
9+
operator fun CharSequence.component2(): String = "component2"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
@SuppressLint("SdCardPath")
7+
fun foo() {
8+
val (a: String, b: String) = "/sdcard"
9+
}
10+
11+
operator fun CharSequence.component1(): String = "component1"
12+
operator fun CharSequence.component2(): String = "component2"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
fun foo(l: Any) = l
5+
6+
fun bar() {
7+
foo() {
8+
"<caret>/sdcard"
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
fun foo(l: Any) = l
7+
8+
@SuppressLint("SdCardPath")
9+
fun bar() {
10+
foo() {
11+
"<caret>/sdcard"
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
fun foo(l: Any) = l
5+
6+
val bar = foo() { "<caret>/sdcard" }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
fun foo(l: Any) = l
7+
8+
@SuppressLint("SdCardPath")
9+
val bar = foo() { "/sdcard" }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
fun foo(path: String = "<caret>/sdcard") = path
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
fun foo(@SuppressLint("SdCardPath") path: String = "/sdcard") = path
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
val getPath = { "<caret>/sdcard" }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
@SuppressLint("SdCardPath")
7+
val getPath = { "/sdcard" }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
2+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
3+
4+
val path = "<caret>/sdcard"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import android.annotation.SuppressLint
2+
3+
// INTENTION_TEXT: Suppress: Add @SuppressLint("SdCardPath") annotation
4+
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintSdCardPathInspection
5+
6+
@SuppressLint("SdCardPath")
7+
val path = "/sdcard"

plugins/lint/lint-idea/lint-idea.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
<orderEntry type="library" name="android-plugin" level="project" />
1717
<orderEntry type="module" module-name="android-annotations" />
1818
<orderEntry type="module" module-name="frontend" />
19+
<orderEntry type="module" module-name="idea-analysis" />
20+
<orderEntry type="module" module-name="idea-core" />
1921
</component>
2022
</module>

0 commit comments

Comments
 (0)