Skip to content

Commit

Permalink
Cast findViewById() result to Views safely
Browse files Browse the repository at this point in the history
(cherry picked from commit 311b3cc)
  • Loading branch information
yanex committed Mar 26, 2015
1 parent 68892e7 commit c9e43fd
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public abstract class AndroidUIXmlProcessor(protected val project: Project) {
}

private fun KotlinStringWriter.writeSyntheticProperty(receiver: String, widget: AndroidWidget, stubCall: String) {
val cast = if (widget.className == "View") ":" else "as"
val body = arrayListOf("return $stubCall $cast ${widget.className}")
val cast = if (widget.className != "View") " as? ${widget.className}" else ""
val body = arrayListOf("return $stubCall$cast")
val type = widget.className
writeImmutableExtensionProperty(receiver,
name = widget.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

val android.app.Activity.button: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

val android.view.View.button: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

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

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

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

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

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

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

val android.app.Activity.textView1: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

val android.view.View.textView1: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

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

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

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

val android.app.Activity.includeTag: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

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

val android.app.Activity.fragmentTag: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import android.support.v4.widget.*
import kotlin.internal.flexible.ft

val android.view.View.includeTag: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

val android.view.View.fragmentTag: ft<View, View?>
get() = findViewById(0) : View
get() = findViewById(0)

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

0 comments on commit c9e43fd

Please sign in to comment.