https://kotlinlang.org logo
#announcements
Title
# announcements
d

dgleeson

09/05/2019, 7:05 PM
I'm trying to use Guice injection for my kotlin project. I'm declaring stuff like
Copy code
class KotlinWebService @Inject constructor(configuration: KotlinServiceConfiguration) : WebService(configuration.webServerPort, configuration.oidcBaseUrl)
that has mostly been working as expected. The problem I have is when I try to do something to the super during the init. e.g.
Copy code
init { super.setUnauthorizedUrls(setOf("/path")) }
I get
Copy code
java.lang.UnsupportedOperationException
. It seems like init is happening before injection. Does anyone have any ideas?
c

Casey Brooks

09/05/2019, 7:19 PM
Initializer blocks are called in the order they are declared in the class. If you have properties declared or other initializers after that one, it could be that the superclass is in a bad state when you call it. Does it still crash if you create the object manually? Since it’s throwing a normal Java exception (not a Kotlin or Guice-specific exception), it’s probably something to do with the class design itself and the initialization order, not necessarily Guice or Kotlin.
d

dgleeson

09/05/2019, 10:06 PM
If I remove the initializer totally the service starts up with no issue. I assume the default constructor that is in the class name definition is doing some kind of initialization of the properties it defines? Let me try running that method on the super class after all initialization has completed to be sure.
@Casey Brooks I think you are right, there is something else going on here that has nothing to do with Guice 😛
now the service starts and then exits immediately
I figured out what it was, my java code was expecting a mutable set, but the setOf was creating an immutable set
137 Views