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 group = net.shadowfacts
archivesBaseName = Forgelin archivesBaseName = Forgelin
@ -6,4 +6,4 @@ mc_version = 1.10.2
mcp_mappings = snapshot_20160802 mcp_mappings = snapshot_20160802
forge_version = 12.18.1.2045 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) { override fun setProxy(target: Field, proxyTarget: Class<*>, proxy: Any) {
log.debug("Setting proxy: ${target.declaringClass.simpleName}.${target.name} -> $proxy") log.debug("Setting proxy: ${target.declaringClass.simpleName}.${target.name} -> $proxy")
if (proxyTarget.fields.any { x -> x.name == "INSTANCE" }) {
// Singleton // objectInstance is not null if it's a Kotlin object, so set the value on the object
try { // if it is null, set the value on the static field
log.debug("Setting proxy on INSTANCE; singleton target") target.set(proxyTarget.kotlin.objectInstance, proxy)
val obj = proxyTarget.getField("INSTANCE").get(null)
target.set(obj, proxy)
} catch (e: Exception) {
throw KotlinAdapterException(e)
}
} else {
target.set(proxyTarget, proxy)
}
} }
override fun getNewInstance(container: FMLModContainer, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any { override fun getNewInstance(container: FMLModContainer, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any {
log.debug("FML has asked for ${objectClass.simpleName} to be constructed") log.debug("FML has asked for ${objectClass.simpleName} to be constructed")
try { return objectClass.kotlin.objectInstance ?: objectClass.newInstance()
// 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)
}
}
} }
override fun setInternalProxies(mod: ModContainer?, side: Side?, loader: ClassLoader?) { override fun setInternalProxies(mod: ModContainer?, side: Side?, loader: ClassLoader?) {
// Nothing to do; FML's got this covered for Kotlin // 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.FMLLog
import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.SidedProxy
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent 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") @Mod(modid = "test", modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter")
object Test { object Test {
@SidedProxy(clientSide = "ClientProxy", serverSide = "CommonProxy")
lateinit var proxy: CommonProxy
private set
@Mod.EventHandler @Mod.EventHandler
fun preInit(event: FMLPreInitializationEvent) { fun preInit(event: FMLPreInitializationEvent) {
FMLLog.bigWarning("Hello from Kotlin!") 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")
}
}