quarkus: Kotlin reflection fails in native build in some cases that Java reflection success

Kotlin method references (e.g. this::someMethod) have a s nice shorthand for retrieving the reflection java.lang.reflect.Method via the javaMethod property as in this::someMethod.javaMethod.

When using a JAX-RS UriInfo to build location headers, using the reflected method reference is a great way to keep things DRY so as not to repeat path information stated in a @Path annotation.

For example:

    @POST
    fun create(@Context uriInfo: UriInfo) =
    	Response
    		.created(
    			uriInfo.requestUriBuilder
    				.path(this::fetch.javaMethod) // Fetches path from the @Path annotation of the "fetch" method
    				.buildFromMap(mapOf(
    					"id" to "12345"
    				))
    		)
    		.entity(mapOf("id" to "12345", "name" to "test"))
    		.build()


    @GET
    @Path("{id}")
    fun fetch(@PathParam("id") id: String) =
    	Response
    		.ok()
    		.entity(mapOf("id" to "12345", "name" to "test"))
    		.build()

Using Kotlin references is a great shorthand and keeps things type safe and compile type checked as compared to using Java…

In Kotlin

path(this::fetch.javaMethod)

In Java

path(GreetingResource::class.java.getDeclaredMethod("fetch", String::class.java))

As usual, all is well in JVM mode (and both examples work correctly) but in native mode the Kotlin version throws an error deep in the bowels of its reflection library.

Expected behavior

Kotlin’s reflection tools work the same as equivalent Java reflection in both JVM and native builds.

Actual behavior

In native builds Kotlin reflection for method references fails.

To Reproduce

rest-kotlin-quickstart.zip

Environment (please complete the following information):

  • Output of uname -a or ver:

     Darwin #### 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64`
    
  • Output of java -version:

     openjdk version "14" 2020-03-17
     OpenJDK Runtime Environment (build 14+36-1461)
     OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)
    
  • Quarkus version or git rev:

     1.3.2
    
  • Build tool (ie. output of mvnw --version or gradlew --version):

     Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
     Default locale: en_US, platform encoding: UTF-8
     OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "Mac"
    

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Any progress on a fix? I’m facing the same problem…

@geoand will look into this 👍

Found the fix. just need to integrate it.

Just hit this again. Any progress on a fix? Anyway I can help?

Looks like something that would be addressed by https://github.com/quarkusio/quarkus/issues/3954#issuecomment-654154905.

@RotBolt do you want to take it?