GeezerVison is showing github.com — [core] Fix #6913: Use the configured class loader in RuleSetLoader#loadFromString by renechoi · Pull Request #6921 · pmd/pmd
Conversation
renechoi
commented
Jul 31, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Describe the PR
RuleSetLoader#loadFromString temporarily replaces the configured ResourceLoader with an anonymous wrapper that serves the XML content for the symbolic file name. That wrapper was built with the no-arg ResourceLoader() constructor, so it silently fell back to PMD's own class loader.
Resource lookups still behaved correctly, because the wrapper's loadResourceAsStream delegates to the previous loader for every other name. Everything that goes through the class loader did not:
ResourceLoader#loadRuleFromClassPath could not find rule classes that are only visible to the class loader passed to loadResourcesWith(ClassLoader), so loading the ruleset failed with Reflection error while instantiating rule (ClassNotFoundException).
loadClassPathResourceAsStream used the wrong class loader for the same reason.
new RuleSetLoader()
.loadResourcesWith(customClassLoader)
.loadFromString(location, rulesetXmlContent); // customClassLoader was ignored
The fix constructs the wrapper with the previous loader's class loader, so class and classpath resource resolution keep using what the caller configured. Passing the class loader (rather than overriding the individual class-loading methods) also keeps any future ResourceLoader method that uses the class loader correct by construction.
ResourceLoader#getClassLoader() is added for that. ResourceLoader lives in net.sourceforge.pmd.util.internal and is marked @internalApi, and japicmp reports no API change for this PR.
Related issues
Ready?
-
Added unit tests for fixed bug/feature
RuleSetFactoryTest#testLoadFromStringUsesConfiguredClassLoader loads a ruleset whose class attribute is only resolvable through the configured class loader. On main it fails with Reflection error while instantiating rule (ClassNotFoundException); the other 59 tests of that class stay green, so the new test is not covered by the existing ones.
-
Passing all unit tests
./mvnw -pl pmd-core verify on this branch: Tests run: 6441, Failures: 0, Errors: 0, Skipped: 7, with checkstyle, pmd, cpd-check and japicmp all passing.
-
Complete build ./mvnw clean verify passes (checked automatically by github actions)
GitHub Actions has not run on this PR yet, so I ran the complete build locally on b141dac, with Temurin 21.0.9 on macOS aarch64:
./mvnw --show-version --errors --batch-mode clean verify
BUILD SUCCESS -- Total time: 12:33 min
All 44 modules pass. 19188 unit tests (Failures: 0, Errors: 0, Skipped: 39) plus 31 integration tests, checkstyle:check reporting 0 violations in each of the 44 modules, and pmd:check, pmd:cpd-check, javadoc:jar and source:jar green everywhere. japicmp:cmp also runs in all 44 modules and is configured with breakBuildBasedOnSemanticVersioning, so the green build covers the API compatibility claim above. RuleSetFactoryTest itself runs Tests run: 60, Failures: 0, Errors: 0, Skipped: 0, that is the 59 existing tests plus the new one.
-
Added (in-code) documentation (if needed)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AB-xdev reacted with rocket emoji
…#loadFromString
loadFromString wrapped the configured ResourceLoader in an anonymous
subclass created with the no-arg constructor, so the wrapper fell back
to PMD's own class loader. Resource lookups still worked because
loadResourceAsStream delegates to the previous loader, but everything
that goes through the class loader did not: loadRuleFromClassPath could
not find rule classes that are only visible to the class loader passed
to loadResourcesWith, and the ruleset failed to load with a
ClassNotFoundException.
Build the wrapper with the previous loader's class loader so class and
classpath resource resolution keep using it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Documentation Preview
Compared to main:
This changeset changes 8 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.
Regression Tester Report
(comment created at 2026-08-14 18:01:57+00:00 for 569e328)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
@adangel Does this conflict with what you're doing in #6859?
No, the resource loader is separate from the auxclasspath. The resource loader uses the runtime classpath to load rulesets and rules.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|