8000 [java] Detect mismatch between Java language version used and version of classes on auxclasspath. by oowekyala · Pull Request #5612 · pmd/pmd · GitHub
[go: up one dir, main page]

Skip to content

[java] Detect mismatch between Java language version used and version of classes on auxclasspath. #5612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

oowekyala
Copy link
Member

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?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Copy link
github-actions bot commented Mar 20, 2025

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.

Regression Tester Report

@oowekyala oowekyala force-pushed the java-detect-version-problems branch from 10eba22 to 333482a Compare March 20, 2025 12:36
@oowekyala oowekyala force-pushed the java-detect-version-problems branch from 333482a to 2a071b9 Compare March 20, 2025 12:38
@adangel
Copy link
Member
adangel commented Mar 20, 2025

FYI - I started a while ago something similar -> #5299

@adangel
Copy link
Member
adangel commented Mar 20, 2025

Also note that PMD is not called correctly from maven-pmd-plugin AFIK: The JDK is never added to the auxclasspath, the current runtime is always used. Maven supports toolchains, so you can select a specific JDK if you want, but it's probably not used very often.

@oowekyala
Copy link
Member Author

FYI - I started a while ago something similar -> #5299

You're right, I forgot about this. I will close this PR then. FWIW I think doing the check in JavaLanguageProcessor and not ClasspathClassLoader is a better way since it doesn't need a specific classloader implementation. But we can do that in your PR.

@oowekyala oowekyala closed this Mar 20, 2025
@adangel
Copy link
Member
adangel commented Mar 20, 2025

I didn't read yet what your solution looks like in detail - I just read the summary 😄

Also, I don't remember anymore what the status of this old PR is and what's missing....

@oowekyala
Copy link
Member Author

We basically implemented exactly the same thing, except I did it in JavaLanguageProcessor and you did it in ClasspathClassLoader. But you added tests so I think your PR is probably a better starting point :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0