Skip to content

Instantly share code, notes, and snippets.

@edvin
Last active July 19, 2016 13:13
Show Gist options
  • Save edvin/166c3cccedd8b10751bdea5da92a9ac5 to your computer and use it in GitHub Desktop.
Save edvin/166c3cccedd8b10751bdea5da92a9ac5 to your computer and use it in GitHub Desktop.
Kotlin app that creates illegal class names failing the test in Class.getSimpleName()
package sample
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.scene.Scene
import javafx.scene.control.ListCell
import javafx.scene.control.ListView
import javafx.stage.Stage
import javafx.util.Callback
class Main : Application() {
override fun start(stage: Stage) {
val root = ListView<String>().apply {
items = FXCollections.observableArrayList("One", "Two", "Three")
cellFormat {
// This line crashes with java.lang.InternalError: Malformed class name
// at java.lang.Class.getSimpleName(Class.java:1330)
println(javaClass.simpleName)
text = item
}
}
stage.scene = Scene(root)
stage.show()
}
companion object {
fun main(args: Array<String>) {
Application.launch(*args)
}
}
}
// This is a convenience function that provides a cellfactory to a ListView, taken from the TornadoFX framework
// Changing 'crossinline' to 'noinline' seems to solve the problem
inline fun <T> ListView<T>.cellFormat(crossinline formatter: (ListCell<T>.(T) -> Unit)) {
cellFactory = Callback {
object : ListCell<T>() {
override fun updateItem(item: T, empty: Boolean) {
super.updateItem(item, empty)
if (item == null || empty) {
text = null
graphic = null
} else {
formatter(this, item)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment