autonomousapps / dependency-analysis-gradle-plugin

Gradle plugin for JVM projects written in Java, Kotlin, Groovy, or Scala; and Android projects written in Java or Kotlin. Provides advice for managing dependencies and other applied plugins
Apache License 2.0
1.66k stars 116 forks source link

Make FindDeclaredProcsTask use the JDK of the configured Java toolchain to load classes #1167

Open jjohannes opened 2 months ago

jjohannes commented 2 months ago

Is your feature request related to a problem? Please describe.

Right now, if you configure a toolchain with a Java version (e.g. 21) that is higher than the version you run Gradle with (e.g. 17) you may get the following exception for Annotation Processor class files:

Caused by: java.lang.UnsupportedClassVersionError: ...AnnotationProcessor has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 61.0
        at com.autonomousapps.tasks.FirstClassLoader.loadClass(FindDeclaredProcsTask.kt:390)
        at com.autonomousapps.tasks.FindDeclaredProcsTask.procFor(FindDeclaredProcsTask.kt:129)
        at com.autonomousapps.tasks.FindDeclaredProcsTask.procs(FindDeclaredProcsTask.kt:116)
        at com.autonomousapps.tasks.FindDeclaredProcsTask.action(FindDeclaredProcsTask.kt:93)

https://scans.gradle.com/s/bcxd6sczjyfhk/failure#1

Describe the solution you'd like

The FindDeclaredProcsTask task works in the described setup and does not throw.

Describe alternatives you've considered

The solution is to run the build itself with a JDK that corresponds to the configured toolchain or is newer.

Additional context

Do you think this can be solved somehow @autonomousapps? If yes, I can attempt to provide a fix.

autonomousapps commented 2 months ago

I'm open to any solution to this problem that works and is maintainable. In the past, I considered trying to use a worker action with process isolation; actually I vaguely recall trying this and running into a major issue that felt like a bug in that API.

The reason I'm using a custom classloader here is that i need to "ask" the annotation processor which types it supports.

I think we have pretty good regression tests around this subject, so if you have time, feel free to experiment and see what solves your problem while also keeping the existing tests passing 👍