Skip to content

Commit c9e43fd

Browse files
committed
Cast findViewById() result to Views safely
(cherry picked from commit 311b3cc)
1 parent 68892e7 commit c9e43fd

File tree

17 files changed

+62
-62
lines changed

17 files changed

+62
-62
lines changed

plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidUIXmlProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ public abstract class AndroidUIXmlProcessor(protected val project: Project) {
140140
}
141141

142142
private fun KotlinStringWriter.writeSyntheticProperty(receiver: String, widget: AndroidWidget, stubCall: String) {
143-
val cast = if (widget.className == "View") ":" else "as"
144-
val body = arrayListOf("return $stubCall $cast ${widget.className}")
143+
val cast = if (widget.className != "View") " as? ${widget.className}" else ""
144+
val body = arrayListOf("return $stubCall$cast")
145145
val type = widget.className
146146
writeImmutableExtensionProperty(receiver,
147147
name = widget.id,

plugins/android-compiler-plugin/testData/android/converter/simple/fqNameInAttr/layout.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.MyButton: ft<org.my.cool.Button, org.my.cool.Button?>
16-
get() = findViewById(0) as org.my.cool.Button
16+
get() = findViewById(0) as? org.my.cool.Button
1717

1818
val android.app.Fragment.MyButton: ft<org.my.cool.Button, org.my.cool.Button?>
19-
get() = getView().findViewById(0) as org.my.cool.Button
19+
get() = getView().findViewById(0) as? org.my.cool.Button
2020

plugins/android-compiler-plugin/testData/android/converter/simple/fqNameInAttr/layout1.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.MyButton: ft<org.my.cool.Button, org.my.cool.Button?>
16-
get() = findViewById(0) as org.my.cool.Button
16+
get() = findViewById(0) as? org.my.cool.Button
1717

plugins/android-compiler-plugin/testData/android/converter/simple/fqNameInTag/layout.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.MyButton: ft<org.my.cool.Button, org.my.cool.Button?>
16-
get() = findViewById(0) as org.my.cool.Button
16+
get() = findViewById(0) as? org.my.cool.Button
1717

1818
val android.app.Fragment.MyButton: ft<org.my.cool.Button, org.my.cool.Button?>
19-
get() = getView().findViewById(0) as org.my.cool.Button
19+
get() = getView().findViewById(0) as? org.my.cool.Button
2020

plugins/android-compiler-plugin/testData/android/converter/simple/fqNameInTag/layout1.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.MyButton: ft<org.my.cool.Button, org.my.cool.Button?>
16-
get() = findViewById(0) as org.my.cool.Button
16+
get() = findViewById(0) as? org.my.cool.Button
1717

plugins/android-compiler-plugin/testData/android/converter/simple/layoutVariants/layout.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.button: ft<View, View?>
16-
get() = findViewById(0) : View
16+
get() = findViewById(0)
1717

1818
val android.app.Fragment.button: ft<View, View?>
19-
get() = getView().findViewById(0) : View
19+
get() = getView().findViewById(0)
2020

plugins/android-compiler-plugin/testData/android/converter/simple/layoutVariants/layout1.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.button: ft<View, View?>
16-
get() = findViewById(0) : View
16+
get() = findViewById(0)
1717

plugins/android-compiler-plugin/testData/android/converter/simple/multiFile/layout.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.item_detail_container: ft<FrameLayout, FrameLayout?>
16-
get() = findViewById(0) as FrameLayout
16+
get() = findViewById(0) as? FrameLayout
1717

1818
val android.app.Fragment.item_detail_container: ft<FrameLayout, FrameLayout?>
19-
get() = getView().findViewById(0) as FrameLayout
19+
get() = getView().findViewById(0) as? FrameLayout
2020

2121
val android.app.Activity.textView1: ft<TextView, TextView?>
22-
get() = findViewById(0) as TextView
22+
get() = findViewById(0) as? TextView
2323

2424
val android.app.Fragment.textView1: ft<TextView, TextView?>
25-
get() = getView().findViewById(0) as TextView
25+
get() = getView().findViewById(0) as? TextView
2626

2727
val android.app.Activity.password: ft<EditText, EditText?>
28-
get() = findViewById(0) as EditText
28+
get() = findViewById(0) as? EditText
2929

3030
val android.app.Fragment.password: ft<EditText, EditText?>
31-
get() = getView().findViewById(0) as EditText
31+
get() = getView().findViewById(0) as? EditText
3232

3333
val android.app.Activity.login: ft<Button, Button?>
34-
get() = findViewById(0) as Button
34+
get() = findViewById(0) as? Button
3535

3636
val android.app.Fragment.login: ft<Button, Button?>
37-
get() = getView().findViewById(0) as Button
37+
get() = getView().findViewById(0) as? Button
3838

plugins/android-compiler-plugin/testData/android/converter/simple/multiFile/layout1.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.item_detail_container: ft<FrameLayout, FrameLayout?>
16-
get() = findViewById(0) as FrameLayout
16+
get() = findViewById(0) as? FrameLayout
1717

1818
val android.view.View.textView1: ft<TextView, TextView?>
19-
get() = findViewById(0) as TextView
19+
get() = findViewById(0) as? TextView
2020

2121
val android.view.View.password: ft<EditText, EditText?>
22-
get() = findViewById(0) as EditText
22+
get() = findViewById(0) as? EditText
2323

2424
val android.view.View.login: ft<Button, Button?>
25-
get() = findViewById(0) as Button
25+
get() = findViewById(0) as? Button
2626

plugins/android-compiler-plugin/testData/android/converter/simple/multiFile/layout2.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.frameLayout: ft<FrameLayout, FrameLayout?>
16-
get() = findViewById(0) as FrameLayout
16+
get() = findViewById(0) as? FrameLayout
1717

1818
val android.app.Fragment.frameLayout: ft<FrameLayout, FrameLayout?>
19-
get() = getView().findViewById(0) as FrameLayout
19+
get() = getView().findViewById(0) as? FrameLayout
2020

2121
val android.app.Activity.passwordField: ft<TextView, TextView?>
22-
get() = findViewById(0) as TextView
22+
get() = findViewById(0) as? TextView
2323

2424
val android.app.Fragment.passwordField: ft<TextView, TextView?>
25-
get() = getView().findViewById(0) as TextView
25+
get() = getView().findViewById(0) as? TextView
2626

2727
val android.app.Activity.passwordCaption: ft<EditText, EditText?>
28-
get() = findViewById(0) as EditText
28+
get() = findViewById(0) as? EditText
2929

3030
val android.app.Fragment.passwordCaption: ft<EditText, EditText?>
31-
get() = getView().findViewById(0) as EditText
31+
get() = getView().findViewById(0) as? EditText
3232

3333
val android.app.Activity.loginButton: ft<Button, Button?>
34-
get() = findViewById(0) as Button
34+
get() = findViewById(0) as? Button
3535

3636
val android.app.Fragment.loginButton: ft<Button, Button?>
37-
get() = getView().findViewById(0) as Button
37+
get() = getView().findViewById(0) as? Button
3838

plugins/android-compiler-plugin/testData/android/converter/simple/multiFile/layout3.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.frameLayout: ft<FrameLayout, FrameLayout?>
16-
get() = findViewById(0) as FrameLayout
16+
get() = findViewById(0) as? FrameLayout
1717

1818
val android.view.View.passwordField: ft<TextView, TextView?>
19-
get() = findViewById(0) as TextView
19+
get() = findViewById(0) as? TextView
2020

2121
val android.view.View.passwordCaption: ft<EditText, EditText?>
22-
get() = findViewById(0) as EditText
22+
get() = findViewById(0) as? EditText
2323

2424
val android.view.View.loginButton: ft<Button, Button?>
25-
get() = findViewById(0) as Button
25+
get() = findViewById(0) as? Button
2626

plugins/android-compiler-plugin/testData/android/converter/simple/sameIds/layout.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.textView1: ft<View, View?>
16-
get() = findViewById(0) : View
16+
get() = findViewById(0)
1717

1818
val android.app.Fragment.textView1: ft<View, View?>
19-
get() = getView().findViewById(0) : View
19+
get() = getView().findViewById(0)
2020

2121
val android.app.Activity.textView2: ft<TextView, TextView?>
22-
get() = findViewById(0) as TextView
22+
get() = findViewById(0) as? TextView
2323

2424
val android.app.Fragment.textView2: ft<TextView, TextView?>
25-
get() = getView().findViewById(0) as TextView
25+
get() = getView().findViewById(0) as? TextView
2626

plugins/android-compiler-plugin/testData/android/converter/simple/sameIds/layout1.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.textView1: ft<View, View?>
16-
get() = findViewById(0) : View
16+
get() = findViewById(0)
1717

1818
val android.view.View.textView2: ft<TextView, TextView?>
19-
get() = findViewById(0) as TextView
19+
get() = findViewById(0) as? TextView
2020

plugins/android-compiler-plugin/testData/android/converter/simple/singleFile/layout.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.item_detail_container: ft<FrameLayout, FrameLayout?>
16-
get() = findViewById(0) as FrameLayout
16+
get() = findViewById(0) as? FrameLayout
1717

1818
val android.app.Fragment.item_detail_container: ft<FrameLayout, FrameLayout?>
19-
get() = getView().findViewById(0) as FrameLayout
19+
get() = getView().findViewById(0) as? FrameLayout
2020

2121
val android.app.Activity.textView1: ft<TextView, TextView?>
22-
get() = findViewById(0) as TextView
22+
get() = findViewById(0) as? TextView
2323

2424
val android.app.Fragment.textView1: ft<TextView, TextView?>
25-
get() = getView().findViewById(0) as TextView
25+
get() = getView().findViewById(0) as? TextView
2626

2727
val android.app.Activity.password: ft<EditText, EditText?>
28-
get() = findViewById(0) as EditText
28+
get() = findViewById(0) as? EditText
2929

3030
val android.app.Fragment.password: ft<EditText, EditText?>
31-
get() = getView().findViewById(0) as EditText
31+
get() = getView().findViewById(0) as? EditText
3232

3333
val android.app.Activity.login: ft<Button, Button?>
34-
get() = findViewById(0) as Button
34+
get() = findViewById(0) as? Button
3535

3636
val android.app.Fragment.login: ft<Button, Button?>
37-
get() = getView().findViewById(0) as Button
37+
get() = getView().findViewById(0) as? Button
3838

plugins/android-compiler-plugin/testData/android/converter/simple/singleFile/layout1.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.item_detail_container: ft<FrameLayout, FrameLayout?>
16-
get() = findViewById(0) as FrameLayout
16+
get() = findViewById(0) as? FrameLayout
1717

1818
val android.view.View.textView1: ft<TextView, TextView?>
19-
get() = findViewById(0) as TextView
19+
get() = findViewById(0) as? TextView
2020

2121
val android.view.View.password: ft<EditText, EditText?>
22-
get() = findViewById(0) as EditText
22+
get() = findViewById(0) as? EditText
2323

2424
val android.view.View.login: ft<Button, Button?>
25-
get() = findViewById(0) as Button
25+
get() = findViewById(0) as? Button
2626

plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.app.Activity.includeTag: ft<View, View?>
16-
get() = findViewById(0) : View
16+
get() = findViewById(0)
1717

1818
val android.app.Fragment.includeTag: ft<View, View?>
19-
get() = getView().findViewById(0) : View
19+
get() = getView().findViewById(0)
2020

2121
val android.app.Activity.fragmentTag: ft<View, View?>
22-
get() = findViewById(0) : View
22+
get() = findViewById(0)
2323

2424
val android.app.Fragment.fragmentTag: ft<View, View?>
25-
get() = getView().findViewById(0) : View
25+
get() = getView().findViewById(0)
2626

2727
val android.app.Activity.`fun`: ft<TextView, TextView?>
28-
get() = findViewById(0) as TextView
28+
get() = findViewById(0) as? TextView
2929

3030
val android.app.Fragment.`fun`: ft<TextView, TextView?>
31-
get() = getView().findViewById(0) as TextView
31+
get() = getView().findViewById(0) as? TextView
3232

plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout1.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import android.support.v4.widget.*
1313
import kotlin.internal.flexible.ft
1414

1515
val android.view.View.includeTag: ft<View, View?>
16-
get() = findViewById(0) : View
16+
get() = findViewById(0)
1717

1818
val android.view.View.fragmentTag: ft<View, View?>
19-
get() = findViewById(0) : View
19+
get() = findViewById(0)
2020

2121
val android.view.View.`fun`: ft<TextView, TextView?>
22-
get() = findViewById(0) as TextView
22+
get() = findViewById(0) as? TextView
2323

0 commit comments

Comments
 (0)