Skip to content

[core] Fix #5039: Refactor RuleContext to use builder pattern#6260

Open
oowekyala wants to merge 28 commits into
pmd:mainfrom
oowekyala:issue5039-rulecontext-fluent-api
Open

[core] Fix #5039: Refactor RuleContext to use builder pattern#6260
oowekyala wants to merge 28 commits into
pmd:mainfrom
oowekyala:issue5039-rulecontext-fluent-api

Conversation

@oowekyala

@oowekyala oowekyala commented Nov 25, 2025

Copy link
Copy Markdown
Member

Describe the PR

Add new API to RuleContext to report violations. The goal is to replace the set of addViolation* methods with a more flexible 2-stage builder pattern. This allows specifying the violation location and the message parameters independently and increases the readability and usability of this API.

I removed the more complex overloads already in this PR (those that were marked @Experimental) and replace their usages. Should we deprecate the simpler methods now, later, never? I think we should do so, but maybe in a separate PR.

API Changes

New Experimental APIs

  • core
    • Node#atLocation
    • Node#atToken
    • CannotBeSuppressed

Removed Experimental APIs

  • core
    • RuleContext#addViolationWithPosition(Node node, JavaccToken token, String message, Object... formatArgs) (introduced in 7.17.0)
    • RuleContext#addViolationWithPosition(Reportable reportable, AstInfo<?> astInfo, FileLocation location, String message, Object... formatArgs) (introduced in 7.9.0)
    • RuleContext#addViolationNoSuppress(Reportable reportable, AstInfo<?> astInfo, String message, Object... formatArgs) (introduced in 7.14.0)

Deprecated APIs

  • core
    • RuleContext#addViolationWithPosition(Node node, int beginLine, int endLine, String message, Object... formatArgs)

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)
  • Update release notes with deprecations

@oowekyala oowekyala added this to the 7.20.0 milestone Nov 25, 2025
This has been a TODO for a long time. Compared to
automatically finding the suppression node, this
saves time, as we don't have to explore the whole
tree when reporting something.
parse default message ahead of time
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

Documentation Preview

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

Regression Tester Report

(comment created at 2026-03-04 13:34:46+00:00 for 621cb8b)

@oowekyala oowekyala marked this pull request as ready for review November 26, 2025 22:57
@oowekyala oowekyala added the an:enhancement An improvement on existing features / rules label Nov 27, 2025
@adangel adangel modified the milestones: 7.20.0, 7.21.0 Dec 30, 2025
@oowekyala oowekyala requested a review from jsotuyod January 5, 2026 11:44

@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, I like the new API (almost 😄 )

I would really name the verb report() instead of warn().

If we introduce the new API with the violation builder without marking it Experimental (like it is right now in this PR), then I'd deprecate all the old methods addViolation... right now. No need to wait, because we have then already a stable alternative API.

I've created an "API Changes" section in the description of this PR to keep track of the changes for the release notes so far.

We also need to update the documentation then on how to report a violation: https://docs.pmd-code.org/latest/pmd_userdocs_extending_writing_java_rules.html#reporting-violations

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/reporting/RuleContext.java Outdated
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java Outdated
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/reporting/RuleContext.java Outdated

/** Marker annotation for Intellij inspection to warn on unused return value. */
@Documented
@interface CheckReturnValue {

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.

We probably should move this annotation to net.sourceforge.pmd.annotation.

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.

Or did you mean to use com.google.errorprone.annotations.CheckReturnValue? We get it currently transitively via com.google.code.gson:gson, but if we use it ourselves, we should at a explicit dependency.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The intellij inspection handles any annotation called CheckReturnValue. It doesn't even need to be public (which is why I kept this annotation package private). If we really want to use a well-known annotation then I would suggest using jetbrains annotations instead, because that package is small and would give us @NotNull/@Nullable (#6134).

@adangel adangel changed the title [core] Refactor RuleContext to use builder pattern [core] Fix #5039: Refactor RuleContext to use builder pattern Jan 8, 2026
@oowekyala

Copy link
Copy Markdown
Member Author

If we introduce the new API with the violation builder without marking it Experimental (like it is right now in this PR), then I'd deprecate all the old methods addViolation... right now. No need to wait, because we have then already a stable alternative API.

Are you ok with doing this in a companion PR? I would prefer this PR to be only about the new API and the other being just mechanical translation.

@adangel adangel modified the milestones: 7.21.0, 7.22.0 Jan 30, 2026
@adangel adangel modified the milestones: 7.22.0, 7.23.0 Feb 26, 2026
@oowekyala oowekyala requested a review from adangel March 5, 2026 10:38
@adangel adangel modified the milestones: 7.23.0, 7.24.0 Mar 24, 2026
@adangel adangel modified the milestones: 7.24.0, 7.25.0 Apr 24, 2026
@adangel adangel removed this from the 7.25.0 milestone May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

an:enhancement An improvement on existing features / rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core] Add fluent API to report violations

2 participants