Update to Kotlin 1.0.6, clean up language adapter

This commit is contained in:
Shadowfacts 2016-12-28 09:42:03 -05:00
parent 307b2bb8cf
commit 8a06ed849e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 26 additions and 33 deletions

View File

@ -1,4 +1,4 @@
mod_version = 1.1.0
mod_version = 1.2.0
group = net.shadowfacts
archivesBaseName = Forgelin
@ -6,4 +6,4 @@ mc_version = 1.10.2
mcp_mappings = snapshot_20160802
forge_version = 12.18.1.2045
kotlin_version = 1.0.5
kotlin_version = 1.0.6

View File

@ -23,45 +23,19 @@ class KotlinAdapter : ILanguageAdapter {
override fun setProxy(target: Field, proxyTarget: Class<*>, proxy: Any) {
log.debug("Setting proxy: ${target.declaringClass.simpleName}.${target.name} -> $proxy")
if (proxyTarget.fields.any { x -> x.name == "INSTANCE" }) {
// Singleton
try {
log.debug("Setting proxy on INSTANCE; singleton target")
val obj = proxyTarget.getField("INSTANCE").get(null)
target.set(obj, proxy)
} catch (e: Exception) {
throw KotlinAdapterException(e)
}
} else {
target.set(proxyTarget, proxy)
}
// objectInstance is not null if it's a Kotlin object, so set the value on the object
// if it is null, set the value on the static field
target.set(proxyTarget.kotlin.objectInstance, proxy)
}
override fun getNewInstance(container: FMLModContainer, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any {
log.debug("FML has asked for ${objectClass.simpleName} to be constructed")
try {
// Try looking for an object type
val f = objectClass.getField("INSTANCE")
val obj = f.get(null) ?: throw NullPointerException()
log.debug("Found an object INSTANCE reference in ${objectClass.simpleName}, using that. ${obj}")
return obj
} catch (e: Exception) {
// Try looking for a class type
log.debug("Failed to get object reference, trying class construction")
try {
val obj = objectClass.newInstance() ?: throw NullPointerException()
log.debug("Constructed an object from a class type ($objectClass), using that. $obj")
return obj
} catch (e: Exception) {
throw KotlinAdapterException(e)
}
}
return objectClass.kotlin.objectInstance ?: objectClass.newInstance()
}
override fun setInternalProxies(mod: ModContainer?, side: Side?, loader: ClassLoader?) {
// Nothing to do; FML's got this covered for Kotlin
}
private class KotlinAdapterException(e: Exception) : RuntimeException("Kotlin adapter error - do not report to Forge!", e)
}

View File

@ -1,5 +1,6 @@
import net.minecraftforge.fml.common.FMLLog
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.SidedProxy
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent
/**
@ -8,9 +9,27 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent
@Mod(modid = "test", modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter")
object Test {
@SidedProxy(clientSide = "ClientProxy", serverSide = "CommonProxy")
lateinit var proxy: CommonProxy
private set
@Mod.EventHandler
fun preInit(event: FMLPreInitializationEvent) {
FMLLog.bigWarning("Hello from Kotlin!")
proxy.someMethod()
}
}
open class CommonProxy {
open fun someMethod() {
FMLLog.bigWarning("CommonProxy.someMethod")
}
}
class ClientProxy: CommonProxy() {
override fun someMethod() {
super.someMethod()
FMLLog.bigWarning("ClientProxy.someMethod")
}
}