diff --git a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUIXmlParser.kt b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUIXmlParser.kt index fe434cf8b6c..48e5f073dc3 100644 --- a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUIXmlParser.kt +++ b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUIXmlParser.kt @@ -11,10 +11,11 @@ import org.jetbrains.jet.lang.psi.JetPsiFactory class AndroidUIXmlParser(val project: Project?, val searchPaths: Collection) { - val ids: MutableCollection = ArrayList() + val ids: MutableCollection = ArrayList() val kw = KotlinStringWriter() val androidImports = arrayListOf("android.app.Activity", - "android.view.View") + "android.view.View", + "android.widget.*") public fun parse(): String { doParse() @@ -53,19 +54,23 @@ class AndroidUIXmlParser(val project: Project?, val searchPaths: Collection ids.add(AndroidID(id)) }) + val handler = AndroidXmlHandler({ id, wClass -> widgetCallback(id, wClass) }) writeImports() for (xmlStream in xmlStreams) { parser.parse(xmlStream, handler) } } + private fun widgetCallback(id: String, className: String) { + ids.add(AndroidWidget(id, className)) + } + private fun produceKotlinSignatures(): StringBuffer { for (id in ids) { - val body = arrayListOf("return findViewById(R.id.${id.toString()})!!") + val body = arrayListOf("return findViewById(R.id.${id.id}) as ${id.className}") kw.writeImmutableExtensionProperty(receiver = "Activity", - name = id.toString(), - retType = "View", + name = id.id, + retType = id.className, getterBody = body ) } return kw.output() diff --git a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUtil.kt b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUtil.kt index 3d7884877fe..a3d5949f971 100644 --- a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUtil.kt +++ b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidUtil.kt @@ -15,3 +15,4 @@ class AndroidID(val rawID: String): AndroidResource { } } +class AndroidWidget(val id: String, val className: String): AndroidResource diff --git a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidXmlHandler.kt b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidXmlHandler.kt index ea5baf6447d..87183b51cb9 100644 --- a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidXmlHandler.kt +++ b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/AndroidXmlHandler.kt @@ -4,7 +4,7 @@ import org.xml.sax.helpers.DefaultHandler import org.xml.sax.Attributes import java.util.HashMap -class AndroidXmlHandler(val idCallback: (String)-> Unit): DefaultHandler() { +class AndroidXmlHandler(val elementCallback: (String, String)-> Unit): DefaultHandler() { override fun startDocument() { super.startDocument() @@ -18,7 +18,8 @@ class AndroidXmlHandler(val idCallback: (String)-> Unit): DefaultHandler() { val hashMap = attributes.toMap() val s = hashMap["id"] val idPrefix = "@+id/" - if (s != null && s.startsWith(idPrefix)) idCallback(s.replace(idPrefix, "")) + val className = hashMap["class"] ?: localName + if (s != null && s.startsWith(idPrefix)) elementCallback(s.replace(idPrefix, ""), className) } override fun endElement(uri: String?, localName: String, qName: String) { diff --git a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/KotlinWriter.kt b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/KotlinWriter.kt index deece36773e..77ee3c1e58e 100644 --- a/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/KotlinWriter.kt +++ b/compiler/frontend.android/src/org/jetbrains/jet/lang/resolve/android/KotlinWriter.kt @@ -5,19 +5,21 @@ trait KotlinWriter class KotlinStringWriter : KotlinWriter { val ctx = Context() + val imports = ctx.fork() + val body = ctx.fork() fun writeFunction(name: String, args: Collection?, retType: String, - body: Collection) { + stmts: Collection) { val returnTerm = if (retType == "" || retType == "Unit") "" else ": $retType" val argStr = if (args != null) args.join(", ") else "" - ctx.writeln("fun $name($argStr)$returnTerm {") - ctx.incIndent() - for (stmt in body) - ctx.writeln(stmt) - ctx.decIndent() - ctx.writeln("}") + body.writeln("fun $name($argStr)$returnTerm {") + body.incIndent() + for (stmt in stmts) + body.writeln(stmt) + body.decIndent() + body.writeln("}") } fun writeExtensionFunction(receiver: String, @@ -31,24 +33,24 @@ class KotlinStringWriter : KotlinWriter { fun writeImmutableProperty(name: String, retType: String, getterBody: Collection) { - ctx.writeln("val $name: $retType") - ctx.incIndent() - ctx.write("get() ") + body.writeln("val $name: $retType") + body.incIndent() + body.write("get() ") if (getterBody.size > 1) { - ctx.writeNoIndent("{\n") - ctx.incIndent() + body.writeNoIndent("{\n") + body.incIndent() for (stmt in getterBody) { - ctx.writeln(stmt) + body.writeln(stmt) } - ctx.decIndent() - ctx.writeln("}") + body.decIndent() + body.writeln("}") } else { - ctx.writeNoIndent("=") - ctx.writeNoIndent(getterBody.join("").replace("return", "")) - ctx.newLine() + body.writeNoIndent("=") + body.writeNoIndent(getterBody.join("").replace("return", "")) + body.newLine() } - ctx.decIndent() - ctx.newLine() + body.decIndent() + body.newLine() } fun writeImmutableExtensionProperty(receiver: String, @@ -59,13 +61,16 @@ class KotlinStringWriter : KotlinWriter { } fun writeImport(what: String) { - ctx.writeln("import $what") + imports.writeln("import $what") } fun writeEmptyLine() { - ctx.newLine() + body.newLine() } - fun output() = ctx.buffer + fun output(): StringBuffer { + ctx.absorbChildren() + return ctx.buffer + } } diff --git a/compiler/testData/android/converter/singleFile/layout.kt b/compiler/testData/android/converter/singleFile/layout.kt index 5c69910f2f3..b5313bcf548 100644 --- a/compiler/testData/android/converter/singleFile/layout.kt +++ b/compiler/testData/android/converter/singleFile/layout.kt @@ -1,21 +1,16 @@ import android.app.Activity import android.view.View +import android.widget.* -val Activity.item_detail_container: View - get() = findViewById(R.id.item_detail_container)!! +val Activity.item_detail_container: FrameLayout + get() = findViewById(R.id.item_detail_container) as FrameLayout -val Activity.textView1: View - get() = findViewById(R.id.textView1)!! +val Activity.textView1: TextView + get() = findViewById(R.id.textView1) as TextView -val Activity.password: View - get() = findViewById(R.id.password)!! +val Activity.password: EditText + get() = findViewById(R.id.password) as EditText -val Activity.textView2: View - get() = findViewById(R.id.textView2)!! - -val Activity.passwordConfirmation: View - get() = findViewById(R.id.passwordConfirmation)!! - -val Activity.login: View - get() = findViewById(R.id.login)!! +val Activity.login: Button + get() = findViewById(R.id.login) as Button diff --git a/compiler/testData/android/converter/singleFile/layout.xml b/compiler/testData/android/converter/singleFile/layout.xml index 364427e595d..f4736f6937b 100644 --- a/compiler/testData/android/converter/singleFile/layout.xml +++ b/compiler/testData/android/converter/singleFile/layout.xml @@ -18,22 +18,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" - android:inputType="textPassword" > - - - - - - -