Jibin TJ
Jibin TJ

Reputation: 446

Converting Generics from Java to Kotlin

I am trying to port some code from Java to Kotlin and came across a line in Java as

CsrfConfigurer<HttpSecurity> csrfConfigurer = http.getConfigurer(CsrfConfigurer.class);

The method is from this class line 250

But when I translated it to Kotlin language

val csrfConfigurer: CsrfConfigurer<HttpSecurity> = http.getConfigurer(CsrfConfigurer::class.java)

I get an error like enter image description here

Is this related to generics? How do I correct it?

Upvotes: 0

Views: 99

Answers (1)

Eamon Scullion
Eamon Scullion

Reputation: 1384

Able to get this working with an explicit cast from the java class type to the class parameter type:

override fun configure(http: HttpSecurity) {
    val csrfConfigurer: CsrfConfigurer<HttpSecurity> = http.getConfigurer(CsrfConfigurer::class.java as Class<CsrfConfigurer<HttpSecurity>>)
}

Upvotes: 2

Related Questions