fixed bug in property generation on the JS DOM API

This commit is contained in:
James Strachan
2012-06-01 11:40:30 +01:00
parent 32b70db6b2
commit 23db5d7fc8
3 changed files with 45 additions and 39 deletions
+8 -3
View File
@@ -11,9 +11,10 @@ import java.lang.IndexOutOfBoundsException
// Properties
var Node.text : String
get() {
return textContent
/*
if (this.nodeType == Node.ELEMENT_NODE) {
val buffer = StringBuilder()
val nodeList = this.childNodes
@@ -34,9 +35,12 @@ get() {
} else {
return this.nodeValue
}
*/
}
set(value) {
if (this.nodeType == Node.ELEMENT_NODE) {
textContent = value
/*
if (nodeType == Node.ELEMENT_NODE) {
val element = this as Element
// lets remove all the previous text nodes first
for (node in element.children()) {
@@ -46,8 +50,9 @@ set(value) {
}
element.addText(value)
} else {
this.setNodeValue(value)
nodeValue = value
}
*/
}
var Element.id : String
@@ -60,7 +60,7 @@ import js.noImpl
if (methods != null) {
// lets figure out the properties versus methods
val validMethods = ArrayList<Method>()
val properties = TreeMap<String, String>()
val properties = TreeMap<String, PropertyKind>()
for (method in methods) {
if (method != null) {
val name = method.getName() ?: ""
@@ -71,18 +71,18 @@ import js.noImpl
} else answer
}
fun propertyType() = simpleTypeName(method.getReturnType())
fun propertyKind(method: Method): PropertyKind {
val propName = propertyName()
return properties.getOrPut(propName) { PropertyKind(propName, "val", method) }
}
val params = method.getParameterTypes()
val paramSize = params?.size ?: 0
val params = method.getParameterTypes()!!
val paramSize = params.size
if (name.size > 3) {
if (name.startsWith("get") && paramSize == 0) {
val propName = propertyName()
if (!properties.containsKey(propName)) {
properties.put(propName, "public val $propName: ${propertyType()}")
}
} else if (name.startsWith("set") && paramSize == 0) {
val propName = propertyName()
properties.put(propName, "public var $propName: ${propertyType()}")
propertyKind(method).typeName = propertyType()
} else if (name.startsWith("set") && paramSize == 1) {
propertyKind(method).kind = "var"
} else {
validMethods.add(method)
}
@@ -91,8 +91,17 @@ import js.noImpl
}
}
}
for (statement in properties.values()) {
println(" $statement")
for (pk in properties.values()) {
// some properties might not have a getter defined
// so lets ignore those
val typeName = pk.typeName
if (typeName == null) {
validMethods.add(pk.method)
} else {
println(" public ${pk.kind} ${pk.name}: ${typeName}")
}
}
for (method in validMethods) {
val parameterTypes = method.getParameterTypes()!!
@@ -136,6 +145,8 @@ import js.noImpl
}
}
class PropertyKind(val name: String, var kind: String, val method: Method, var typeName: String? = null)
fun write(file: File, block: PrintWriter.() -> Unit): Unit {
println("Generating file: ${file.getCanonicalPath()}")
val writer = PrintWriter(FileWriter(file))