Property access syntax and elvis operators. Because.

This commit is contained in:
Shadowfacts 2016-08-06 16:35:47 -04:00
parent 3c23a62aad
commit f342ce7b03
1 changed files with 8 additions and 14 deletions

View File

@ -16,11 +16,7 @@ import java.lang.reflect.Method
* *
* @author Arkan <arkan@drakon.io> * @author Arkan <arkan@drakon.io>
*/ */
public class KotlinAdapter : ILanguageAdapter { class KotlinAdapter : ILanguageAdapter {
companion object metadata {
public final val ADAPTER_VERSION: String = "@VERSION@-@KOTLIN@"
}
private val log = LogManager.getLogger("ILanguageAdapter/Kotlin") private val log = LogManager.getLogger("ILanguageAdapter/Kotlin")
@ -29,8 +25,8 @@ public 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.getDeclaringClass().getSimpleName(), target.getName(), proxy) log.debug("Setting proxy: {}.{} -> {}", target.declaringClass.simpleName, target.name, proxy)
if (proxyTarget.getFields().any { x -> x.getName().equals("INSTANCE") }) { if (proxyTarget.fields.any { x -> x.name.equals("INSTANCE") }) {
// Singleton // Singleton
try { try {
log.debug("Setting proxy on INSTANCE; singleton target.") log.debug("Setting proxy on INSTANCE; singleton target.")
@ -46,22 +42,20 @@ public class KotlinAdapter : ILanguageAdapter {
} }
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 {} to be constructed...", objectClass.getSimpleName()) log.debug("FML has asked for {} to be constructed...", objectClass.simpleName)
try { try {
// Try looking for an object type // Try looking for an object type
val f = objectClass.getField("INSTANCE") val f = objectClass.getField("INSTANCE")
val obj = f.get(null) val obj = f.get(null) ?: throw NullPointerException()
if (obj == null) throw NullPointerException() log.debug("Found an object INSTANCE reference in {}, using that. ({})", objectClass.simpleName, obj)
log.debug("Found an object INSTANCE reference in {}, using that. ({})", objectClass.getSimpleName(), obj)
return obj return obj
} catch (ex: Exception) { } catch (ex: Exception) {
// Try looking for a class type // Try looking for a class type
log.debug("Failed to get object reference, trying class construction.") log.debug("Failed to get object reference, trying class construction.")
try { try {
val obj = objectClass.newInstance() val obj = objectClass.newInstance() ?: throw NullPointerException()
if (obj == null) throw NullPointerException()
log.debug("Constructed an object from a class type ({}), using that. ({})", objectClass, obj) log.debug("Constructed an object from a class type ({}), using that. ({})", objectClass, obj)
log.warn("Hey, you, modder who owns {} - you should be using 'object' instead of 'class' on your @Mod class.", objectClass.getSimpleName()) log.warn("Hey, you, modder who owns {} - you should be using 'object' instead of 'class' on your @Mod class.", objectClass.simpleName)
return obj return obj
} catch (ex: Exception) { } catch (ex: Exception) {
throw KotlinAdapterException(ex) throw KotlinAdapterException(ex)