EG-3714 reverted previous change as unnecessarily (#6773)

This commit is contained in:
Alexey Kadyrov 2020-10-15 11:03:24 +01:00 committed by GitHub
parent 8ef739b555
commit d0cfeb1fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 19 deletions

View File

@ -1,16 +0,0 @@
package net.corda.nodeapi.internal.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation can be used to provide ConfigParser for the class,
* the [parseAs] method will use the provided parser instead of data class constructs to parse the object.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomConfigParser {
Class parser();
}

View File

@ -28,6 +28,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.full.createInstance
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.jvmErasure
@ -35,6 +36,13 @@ import kotlin.reflect.jvm.jvmErasure
@Target(AnnotationTarget.PROPERTY)
annotation class OldConfig(val value: String)
/**
* This annotation can be used to provide ConfigParser for the class,
* the [parseAs] method will use the provided parser instead of data class constructs to parse the object.
*/
@Target(AnnotationTarget.CLASS)
annotation class CustomConfigParser(val parser: KClass<out ConfigParser<*>>)
interface ConfigParser<T> {
fun parse(config: Config): T
}
@ -69,9 +77,7 @@ fun <T : Any> Config.parseAs(
baseDirectory: Path? = null
): T {
// Use custom parser if provided, instead of treating the object as data class.
@Suppress("UNCHECKED_CAST")
clazz.java.getAnnotation(CustomConfigParser::class.java)?.let {
return ((it.parser.createInstance() as ConfigParser<T>).parse(this)) }
clazz.findAnnotation<CustomConfigParser>()?.let { return uncheckedCast(it.parser.createInstance().parse(this)) }
require(clazz.isData) {
"Only Kotlin data classes or class annotated with CustomConfigParser can be parsed. Offending: ${clazz.qualifiedName}"