Skip to content

Use targetJdk to find Java platform classes from toolchain for type resolution - #726

Merged
adangel merged 5 commits into
apache:masterfrom
adangel:targetJdk-auxclasspath
Sep 10, 2026
Merged

Use targetJdk to find Java platform classes from toolchain for type resolution#726
adangel merged 5 commits into
apache:masterfrom
adangel:targetJdk-auxclasspath

Conversation

@adangel

@adangel adangel commented Aug 20, 2026

Copy link
Copy Markdown
Member

This change improves the correctness of PMD violations: For type resolution, PMD loads the classes to see the available type hierarchy, methods, return types etc. The project's dependencies are already provided to PMD by the plugin with the aux classpath. But the Java platform classes where still resolved from the current Java runtime. This can lead for specific cases in either false positives or false negatives. Examples are: ExecutorService is AutoCloseable since Java 19. If maven/PMD is executed with Java 25 but the targetJdk is Java 17, then PMD's CloseResource would wrongly suggest to use try-with-resources for ExecutorServices. Another example is ByteBuffer#flip: In Java 8, it returns Buffer (inherited), in Java 9+ it returns the more specific ByteBuffer. In Java 8, one must cast, in Java 9, the cast might be unnecessary, triggering PMD's UnnecessaryCast rule.

Historically, PMD 6.x loaded the types via reflection from a custom classloader - so it was not able to load classes from newer runtimes then it was running at. Hence the solution with the toolchains (MPMD-304 #277). With PMD 7, the classes are loaded via ASM, which allows to decouple the Java version used for executing PMD (runtime) from the Java version of the project under analysis (targetJdk). Since PMD 7, PMD running under Java 8 can analyze e.g. Java 25 projects. But one crucial step was missing: The Java platform classes.

To resolve Java platform classes, PMD by default used the current runtime. With the next PMD version 7.27.0, we'll log a warning, if this happens.

This change prepares for that and tries to add the correct Java platform classes to the aux classpath for PMD by using an appropriate toolchain (selected by targetJdk). It will add either rt.jar or jrt-fs.jar (depending on the Java version) to the aux classpath automatically.

Note: This change is independent of the PMD version. Providing the jrt-fs.jar or rt.jar on the aux classpath was supported since PMD 7.0.0, but just not used. The only change in PMD 7.27.0 in that regard is, that a warning is logged, if neither jrt-fs.jar or rt.jar is found on the aux classpath.

The examples can be seen here: https://github.com/adangel/scratchpad/tree/auxclasspath-java-runtime


Following this checklist to help us incorporate your
contribution quickly and easily:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the integration tests successfully (mvn -Prun-its verify).

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

Comment thread src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
Comment thread src/main/java/org/apache/maven/plugins/pmd/PmdReport.java Outdated
Comment thread src/main/java/org/apache/maven/plugins/pmd/PmdReport.java Outdated
…esolution

Uses targetJdk to find a matching toolchain and add its java platform
classes to the aux classpath.

If there is no matching toolchain, log a warning and use the current
java runtime. This preserves the old behavior, when you configured
the toolchain to run PMD with.
@adangel
adangel force-pushed the targetJdk-auxclasspath branch from 0942b54 to c1e2bea Compare September 8, 2026 18:27
@adangel
adangel merged commit 7e1fbfe into apache:master Sep 10, 2026
19 of 34 checks passed
@adangel
adangel deleted the targetJdk-auxclasspath branch September 10, 2026 07:51
adangel added a commit that referenced this pull request Sep 12, 2026
Changes since 3.28.0 rule out a patch: Thread configuration (#700), Update PMD to 7.27.0 (#729), targetJdk/toolchain/type resolution (#726)
Aligns the pom with the existing 3.29.0 milestone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants