Validate protocol type and args via whitelist ahead of class loading. Also, push this work into another method on the Factory.

This commit is contained in:
Matthew Nesbit
2016-07-25 16:35:45 +01:00
parent 0542f7c509
commit 0d68523f5c
2 changed files with 19 additions and 10 deletions

View File

@ -49,6 +49,22 @@ class ProtocolLogicRefFactory(private val protocolWhitelist: Map<String, Set<Str
require(protocolWhitelist[className]!!.contains(argClassName)) { "Args to ${className} must have types on the args whitelist: $argClassName" }
}
/**
* Create a [ProtocolLogicRef] for the Kotlin primary constructor of a named [ProtocolLogic]
*/
fun createKotlin(protocolLogicClassName: String, args: Map<String,Any?>, attachments: List<SecureHash> = emptyList()): ProtocolLogicRef {
val context = AppContext(attachments)
validateProtocolClassName(protocolLogicClassName, context)
for(arg in args.values.filterNotNull()) {
validateArgClassName(protocolLogicClassName, arg.javaClass.name, context)
}
val clazz = Class.forName(protocolLogicClassName)
require(ProtocolLogic::class.java.isAssignableFrom(clazz)) { "$protocolLogicClassName is not a ProtocolLogic" }
@Suppress("UNCHECKED_CAST")
val logic = clazz as Class<ProtocolLogic<ProtocolLogic<*>>>
return createKotlin(logic, args)
}
/**
* Create a [ProtocolLogicRef] for the Kotlin primary constructor or Java constructor and the given args.
*/