Skip to content

[core] Fix #6913: Use the configured class loader in RuleSetLoader#loadFromString - #6921

Merged
adangel merged 2 commits into
pmd:mainfrom
renechoi:issue-6913-loadfromstring-classloader
Aug 14, 2026
Merged

adangel merged 2 commits into
pmd:mainfrom
renechoi:issue-6913-loadfromstring-classloader

Conversation

@renechoi

@renechoi renechoi commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

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)

…#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.
@UncleOwen

Copy link
Copy Markdown
Member

@adangel Does this conflict with what you're doing in #6859?

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

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)

@adangel

adangel commented Aug 14, 2026

Copy link
Copy Markdown
Member

@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.

@adangel adangel added this to the 7.27.0 milestone Aug 14, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adangel
adangel merged commit eed52f5 into pmd:main Aug 14, 2026
13 checks passed
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.

[core] RuleSetLoader#loadFromString ignores previously configured Resource/ClassLoader

3 participants