Android: Add inspection & quickfix to convert findViewById to api 26

#KT-19940 Fixed Target Version 1.1.5
This commit is contained in:
Vyacheslav Gerasimov
2017-08-30 18:19:58 +03:00
parent be900a76d8
commit 5220dfc0ad
15 changed files with 303 additions and 13 deletions
@@ -533,6 +533,15 @@ fun KtCallExpression.getOrCreateValueArgumentList(): KtValueArgumentList {
typeArgumentList ?: calleeExpression) as KtValueArgumentList
}
fun KtCallExpression.addTypeArgument(typeArgument: KtTypeProjection) {
if (typeArgumentList != null) {
typeArgumentList?.addArgument(typeArgument)
}
else {
addAfter(KtPsiFactory(this).createTypeArguments("<${typeArgument.text}>"), calleeExpression)
}
}
fun KtDeclaration.hasBody() = when (this) {
is KtFunction -> hasBody()
is KtProperty -> hasBody()
+1 -1
View File
@@ -34,7 +34,7 @@
<mkdir dir="${android.sdk.dir}/build-tools"/>
<download_android_platform android.versioncode="23_r01" android.sdk.version="23" android.full.version="6.0" />
<download_android_platform_new android.versioncode="25_r01" android.sdk.version="25" android.full.version="7.1.1" />
<download_android_platform_new android.versioncode="26_r02" android.sdk.version="26" android.full.version="8.0.0" />
<download_support_repository android.repo.version="44" android.repo.last="25.2.0"/>
@@ -0,0 +1,79 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.android.inspection
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.android.facet.AndroidFacet
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtPsiUtil.isUnsafeCast
import org.jetbrains.kotlin.psi.psiUtil.addTypeArgument
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
class TypeParameterFindViewByIdInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
val compileSdk = AndroidFacet.getInstance(session.file)?.androidModuleInfo?.buildSdkVersion?.apiLevel
if (compileSdk == null || compileSdk < 26) {
return KtVisitorVoid()
}
return object : KtVisitorVoid() {
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
if (expression.calleeExpression?.text != "findViewById" || expression.typeArguments.isNotEmpty()) {
return
}
val parentCast = (expression.parent as? KtBinaryExpressionWithTypeRHS)?.takeIf { isUnsafeCast(it) } ?: return
val typeText = parentCast.right?.getTypeTextWithoutQuestionMark() ?: return
val callableDescriptor = expression.getResolvedCall(expression.analyze())?.resultingDescriptor ?: return
if (callableDescriptor.name.asString() != "findViewById" || callableDescriptor.typeParameters.size != 1) {
return
}
holder.registerProblem(
parentCast,
"Can be converted to findViewById<$typeText>(...)",
ConvertCastToFindViewByIdWithTypeParameter())
}
}
}
class ConvertCastToFindViewByIdWithTypeParameter : LocalQuickFix {
override fun getFamilyName(): String = "Convert cast to findViewById with type parameter"
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val cast = descriptor.psiElement as? KtBinaryExpressionWithTypeRHS ?: return
val typeText = cast.right?.getTypeTextWithoutQuestionMark() ?: return
val call = cast.left as? KtCallExpression ?: return
val newCall = call.copy() as KtCallExpression
val typeArgument = KtPsiFactory(call).createTypeArgument(typeText)
newCall.addTypeArgument(typeArgument)
cast.replace(newCall)
}
}
companion object {
fun KtTypeReference.getTypeTextWithoutQuestionMark(): String? =
(typeElement as? KtNullableType)?.innerType?.text ?: typeElement?.text
}
}
@@ -34,6 +34,6 @@ public class TestUtils {
@NonNull
public static String getLatestAndroidPlatform() {
return "android-25";
return "android-26";
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.android.lint
import com.intellij.codeInspection.InspectionProfileEntry
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl
import com.intellij.util.PathUtil
import org.jetbrains.android.inspections.klint.AndroidLintInspectionBase
@@ -53,7 +54,7 @@ abstract class AbstractKotlinLintTest : KotlinAndroidTestCase() {
myFixture.enableInspections(*inspectionClassNames.map { className ->
val inspectionClass = Class.forName(className)
inspectionClass.newInstance() as AndroidLintInspectionBase
inspectionClass.newInstance() as InspectionProfileEntry
}.toTypedArray())
val additionalResourcesDir = File(ktFile.parentFile, getTestName(true))
@@ -76,6 +77,6 @@ abstract class AbstractKotlinLintTest : KotlinAndroidTestCase() {
myFixture.copyFileToProject("${PathUtil.getParentPath(path)}/$dependencyFile", "src/$dependencyTargetPath")
}
myFixture.checkHighlighting(true, false, false)
myFixture.checkHighlighting(true, false, true)
}
}
@@ -66,6 +66,12 @@ public class KotlinLintTestGenerated extends AbstractKotlinLintTest {
doTest(fileName);
}
@TestMetadata("findViewById.kt")
public void testFindViewById() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/lint/findViewById.kt");
doTest(fileName);
}
@TestMetadata("javaPerformance.kt")
public void testJavaPerformance() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/lint/javaPerformance.kt");
@@ -36,6 +36,27 @@ public class AndroidLintQuickfixTestGenerated extends AbstractAndroidLintQuickfi
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/android/lintQuickfix"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/android/lintQuickfix/findViewById")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class FindViewById extends AbstractAndroidLintQuickfixTest {
public void testAllFilesPresentInFindViewById() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/android/lintQuickfix/findViewById"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("nullableType.kt")
public void testNullableType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/lintQuickfix/findViewById/nullableType.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/android/lintQuickfix/findViewById/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/android/lintQuickfix/parcelable")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -0,0 +1,5 @@
<html>
<body>
Reports <code>findViewById</code> calls with type cast which can be converted to <code>findViewById</code> with type parameter from Android 8.0 (API level 26)
</body>
</html>
+9 -1
View File
@@ -12,10 +12,18 @@
<localInspection implementationClass="org.jetbrains.kotlin.android.inspection.IllegalIdentifierInspection"
displayName="Illegal Android Identifier"
groupName="Kotlin"
groupName="Kotlin Android"
enabledByDefault="true"
level="ERROR"/>
<localInspection implementationClass="org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection"
displayName="Cast can be converted to findViewById with type parameter"
groupName="Kotlin Android"
enabledByDefault="true"
cleanupTool="true"
level="WEAK WARNING"
language="kotlin" />
<editorNotificationProvider implementation="org.jetbrains.kotlin.android.actions.KotlinNewActivityNotification"/>
<codeInsight.lineMarkerProvider language="kotlin" implementationClass="org.jetbrains.kotlin.android.KotlinAndroidLineMarkerProvider"/>
+49
View File
@@ -0,0 +1,49 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
@Suppress(
"TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER",
"UNUSED_VARIABLE"
)
class OtherActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
<weak_warning descr="Can be converted to findViewById<TextView>(...)">findViewById(R.id.tvHello) as TextView</weak_warning>
val tvHello = <weak_warning descr="Can be converted to findViewById<TextView>(...)">findViewById(R.id.tvHello) as TextView</weak_warning>
val btnGo = <weak_warning descr="Can be converted to findViewById<Button>(...)">findViewById(R.id.btnGo) as Button?</weak_warning>
// should be ok, already has type parameter
val tvHello2 = findViewById<TextView>(R.id.tvHello) as TextView
// ok, we can't convert safe cast because semantic will be changed
val tvHello3 = findViewById(R.id.tvHello) as? TextView
// ok, no cast
foo(findViewById(R.id.tvHello))
// ok, no cast
findViewById(R.id.tvHello) is TextView
}
fun foo(view: TextView) {
view.text = "foo"
}
}
class R {
object layout {
val activity_other = 100500
}
object id {
val tvHello = 0
val btnGo = 1
}
}
+8 -8
View File
@@ -29,7 +29,7 @@ abstract class ViewHolderTest : BaseAdapter() {
// Should use View Holder pattern here
convertView = mInflater.<warning descr="Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling">inflate(R.layout.your_layout, null)</warning>
val text = convertView.findViewById(R.id.text) as TextView
val text: TextView = convertView.findViewById(R.id.text)
text.text = "Position " + position
return convertView
@@ -46,7 +46,7 @@ abstract class ViewHolderTest : BaseAdapter() {
convertView = mInflater.inflate(R.layout.your_layout, null)
}
val text = convertView!!.findViewById(R.id.text) as TextView
val text: TextView = convertView!!.findViewById(R.id.text)
text.text = "Position " + position
return convertView
@@ -65,7 +65,7 @@ abstract class ViewHolderTest : BaseAdapter() {
convertView = mInflater.inflate(R.layout.your_layout, null)
}
val text = convertView!!.findViewById(R.id.text) as TextView
val text: TextView = convertView!!.findViewById(R.id.text)
text.text = "Position " + position
return convertView
@@ -80,7 +80,7 @@ abstract class ViewHolderTest : BaseAdapter() {
// Already using View Holder pattern
convertView = if (convertView == null) mInflater.inflate(R.layout.your_layout, null) else convertView
val text = convertView!!.findViewById(R.id.text) as TextView
val text: TextView = convertView!!.findViewById(R.id.text)
text.text = "Position " + position
return convertView
@@ -99,16 +99,16 @@ abstract class ViewHolderTest : BaseAdapter() {
var v: View? = convertView
if (v == null) v = mLayoutInflator!!.inflate(R.layout.your_layout, null)
val listItemHolder = v!!.findViewById(R.id.laptimes_list_item_holder) as LinearLayout
val listItemHolder: LinearLayout = v!!.findViewById(R.id.laptimes_list_item_holder)
listItemHolder.removeAllViews()
for (i in 1..5) {
val lapItemView = mLayoutInflator!!.inflate(R.layout.laptime_item, null)
val lapItemView: View = mLayoutInflator!!.inflate(R.layout.laptime_item, null)
if (i == 0) {
val t = lapItemView.findViewById(R.id.laptime_text) as TextView
val t: TextView = lapItemView.findViewById(R.id.laptime_text)
}
val t2 = lapItemView.findViewById(R.id.laptime_text2) as TextView
val t2: TextView = lapItemView.findViewById(R.id.laptime_text2)
if (i < mLapTimes.size - 1 && mLapTimes.size > 1) {
var laptime = mLapTimes[i] - mLapTimes[i + 1]
if (laptime < 0) laptime = mLapTimes[i]
@@ -0,0 +1,28 @@
// INTENTION_TEXT: Convert cast to findViewById with type parameter
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class OtherActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
val tvHello = <caret>findViewById(R.id.tvHello) as TextView?
}
}
class R {
object layout {
val activity_other = 100500
}
object id {
val tvHello = 0
}
}
@@ -0,0 +1,28 @@
// INTENTION_TEXT: Convert cast to findViewById with type parameter
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class OtherActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
val tvHello = findViewById<TextView>(R.id.tvHello)
}
}
class R {
object layout {
val activity_other = 100500
}
object id {
val tvHello = 0
}
}
@@ -0,0 +1,28 @@
// INTENTION_TEXT: Convert cast to findViewById with type parameter
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class OtherActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
val tvHello = <caret>findViewById(R.id.tvHello) as TextView
}
}
class R {
object layout {
val activity_other = 100500
}
object id {
val tvHello = 0
}
}
@@ -0,0 +1,28 @@
// INTENTION_TEXT: Convert cast to findViewById with type parameter
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class OtherActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
val tvHello = findViewById<TextView>(R.id.tvHello)
}
}
class R {
object layout {
val activity_other = 100500
}
object id {
val tvHello = 0
}
}