Unwrap InvocationTargetException in dependency injection
^KT-28348 Fixed
This commit is contained in:
@@ -102,7 +102,7 @@ class StorageComponentContainer(private val id: String, parent: StorageComponent
|
||||
override fun <T> create(request: Class<T>): T {
|
||||
val constructorBinding = request.bindToConstructor(unknownContext)
|
||||
val args = constructorBinding.argumentDescriptors.map { it.getValue() }.toTypedArray()
|
||||
return constructorBinding.constructor.newInstance(*args) as T
|
||||
return runWithUnwrappingInvocationException { constructorBinding.constructor.newInstance(*args) as T }
|
||||
}
|
||||
|
||||
override fun toString() = "Container $id"
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.lang.reflect.Constructor
|
||||
import java.lang.reflect.Member
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.Type
|
||||
import java.util.ArrayList
|
||||
import java.util.*
|
||||
|
||||
interface ValueResolver {
|
||||
fun resolve(request: Type, context: ValueResolveContext): ValueDescriptor?
|
||||
@@ -45,7 +45,7 @@ class ConstructorBinding(val constructor: Constructor<*>, val argumentDescriptor
|
||||
class MethodBinding(val method: Method, private val argumentDescriptors: List<ValueDescriptor>) {
|
||||
fun invoke(instance: Any) {
|
||||
val arguments = computeArguments(argumentDescriptors).toTypedArray()
|
||||
method.invoke(instance, *arguments)
|
||||
runWithUnwrappingInvocationException { method.invoke(instance, *arguments) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.container
|
||||
|
||||
import java.io.Closeable
|
||||
import java.lang.reflect.Type
|
||||
import java.util.ArrayList
|
||||
import java.util.*
|
||||
|
||||
enum class ComponentState {
|
||||
Null,
|
||||
@@ -131,7 +131,7 @@ open class SingletonTypeComponentDescriptor(container: ComponentContainer, val k
|
||||
val constructor = binding.constructor
|
||||
val arguments = computeArguments(binding.argumentDescriptors)
|
||||
|
||||
val instance = constructor.newInstance(*arguments.toTypedArray())!!
|
||||
val instance = runWithUnwrappingInvocationException { constructor.newInstance(*arguments.toTypedArray())!! }
|
||||
state = ComponentState.Initialized
|
||||
return instance
|
||||
}
|
||||
@@ -154,4 +154,4 @@ class DefaultSingletonTypeComponentDescriptor(container: ComponentContainer, kla
|
||||
override fun toString(): String {
|
||||
return "Default: ${klass.simpleName}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.container
|
||||
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
|
||||
inline fun <T> runWithUnwrappingInvocationException(block: () -> T) =
|
||||
try {
|
||||
block()
|
||||
} catch (e: InvocationTargetException) {
|
||||
throw e.targetException ?: e
|
||||
}
|
||||
Reference in New Issue
Block a user