mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Android: Add inspection & quickfix to convert findViewById to api 26
#KT-19940 Fixed Target Version 1.1.5 GitOrigin-RevId: d73ff4bcc3d27e784892aafc81f00286d1e36322
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9c5240facf
commit
c1da1ab9f4
@@ -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>
|
||||
@@ -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"/>
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
+28
@@ -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
|
||||
}
|
||||
}
|
||||
+28
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user