[java] Detect mismatch between Java language version used and version of classes on auxclasspath. #5612
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the PR
This is just an idea. With ASM, we can detect the version classes on the auxclasspath were compiled with. Here I just get the class version of java.lang.Object. Assuming every JDK version ships with compiled class files that have the corresponding internal version (meaning for instance, Java 21 jrtfs ships a java.lang.Object class that was compiled with Javac 21), we can use this information to detect that the user hasn't put the correct JDK classes on their auxclasspath. We can use this to log a warning. Wdyt?
The only problem I see is that if you use PMD's default Java version, you will most likely see this warning because it is always the newest Java version. Maybe the default java version should be the latest LTS? Would this change something?
For context, I had suggested this in #4291 (comment). Putting JDK classes on the auxclasspath is recommended on our website but realistically this information is buried very deep and most people won't do it unless they see a warning.
Related issues
Ready?
./mvnw clean verify
passes (checked automatically by github actions)