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>
*/
public class KotlinAdapter : ILanguageAdapter {
companion object metadata {
public final val ADAPTER_VERSION: String = "@VERSION@-@KOTLIN@"
}
class KotlinAdapter : ILanguageAdapter {
private val log = LogManager.getLogger("ILanguageAdapter/Kotlin")
@ -29,8 +25,8 @@ public class KotlinAdapter : ILanguageAdapter {
}
override fun setProxy(target: Field, proxyTarget: Class<*>, proxy: Any) {
log.debug("Setting proxy: {}.{} -> {}", target.getDeclaringClass().getSimpleName(), target.getName(), proxy)
if (proxyTarget.getFields().any { x -> x.getName().equals("INSTANCE") }) {
log.debug("Setting proxy: {}.{} -> {}", target.declaringClass.simpleName, target.name, proxy)
if (proxyTarget.fields.any { x -> x.name.equals("INSTANCE") }) {
// Singleton
try {
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? {
log.debug("FML has asked for {} to be constructed...", objectClass.getSimpleName())
log.debug("FML has asked for {} to be constructed...", objectClass.simpleName)
try {
// Try looking for an object type
val f = objectClass.getField("INSTANCE")
val obj = f.get(null)
if (obj == null) throw NullPointerException()
log.debug("Found an object INSTANCE reference in {}, using that. ({})", objectClass.getSimpleName(), obj)
val obj = f.get(null) ?: throw NullPointerException()
log.debug("Found an object INSTANCE reference in {}, using that. ({})", objectClass.simpleName, obj)
return obj
} catch (ex: Exception) {
// Try looking for a class type
log.debug("Failed to get object reference, trying class construction.")
try {
val obj = objectClass.newInstance()
if (obj == null) throw NullPointerException()
val obj = objectClass.newInstance() ?: throw NullPointerException()
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
} catch (ex: Exception) {
throw KotlinAdapterException(ex)