GeezerVison is showing github.com — [java] Fix #6537: StaticEJBFieldShouldBeFinal detects @Stateless/@Stateful/@Singleton/@MessageDriven EJB classes by fudianchn · Pull Request #6945 · pmd/pmd
Conversation
fudianchn
commented
Aug 6, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Describe the PR
StaticEJBFieldShouldBeFinal now also flags non-final static fields in EJB component classes declared via the @Stateless / @Stateful / @Singleton / @MessageDriven annotations, in both the javax.ejb and jakarta.ejb namespaces.
Previously the rule only matched classes implementing one of the five javax.ejb interfaces (SessionBean, EJBHome, EJBLocalObject, EJBLocalHome, EJBObject), causing a false negative for EJB classes declared via annotations, the canonical way to declare enterprise beans since EJB 3.0. The interface check now also accepts the jakarta.ejb counterparts of those five interfaces, matching the dual-namespace annotation support (Jakarta EE 9+).
Root cause: the XPath predicate only inspected the ImplementsList and ignored the modifier annotations. The fix adds an annotation-based branch (pmd-java:hasAnnotation) alongside the existing ImplementsList branch, which is left untouched, so EJB 2.x style declarations are still detected. The fully-qualified form is used instead of matching the annotation simple name, because the simple name Singleton would also match unrelated annotations such as javax.inject.Singleton (CDI), which is out of scope for this rule.
@MessageDriven (message-driven beans) is included as well: per the EJB specification the "no read/write static fields" programming restriction applies to message-driven beans just as it does to session beans, so the rule would otherwise stay inconsistent (flagging session beans but not MDBs). Happy to drop it in a follow-up if session beans only are preferred.
New test cases cover @Stateless/@Stateful (both namespaces), @Singleton (both namespaces), @MessageDriven (both javax.ejb and jakarta.ejb), a class implementing a jakarta.ejb interface, a negative case with final/instance fields, and a negative case asserting javax.inject.Singleton (CDI) is not flagged. The @MessageDriven cases fail on the old XPath (expected: <1> but was: <0>) and pass after the fix.
Local verification:
./mvnw -pl pmd-java -am test -Dtest=StaticEJBFieldShouldBeFinalTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false -> Tests run: 9, Failures: 0, Errors: 0.
./mvnw clean verify -> BUILD SUCCESS.
Regression note: this broadens detection to annotation-declared EJB classes, so any newly reported violations on classes annotated @Stateless/@Stateful/@Singleton/@MessageDriven with writable static fields are expected true positives. The standard regression corpus (openjdk-11, spring-framework, checkstyle, ...) does not declare EJB session/message-driven beans, so no delta is expected there; I'll confirm against the regression tester report.
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)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
fudianchn
changed the title
[java] Fix #6537: StaticEJBFieldShouldBeFinal detects @Stateless/@Stateful/@Singleton EJB classes
[java] Fix #6537: StaticEJBFieldShouldBeFinal detects @Stateless/@Stateful/@Singleton/@MessageDriven EJB classes
Aug 12, 2026
|
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, 16 errors and 9 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-20 19:05:40+00:00 for 7d9a5dc)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…Stateful/@Singleton/@MessageDriven EJB classes
The rule only matched classes implementing one of the five javax.ejb
interfaces (SessionBean, EJBHome, EJBLocalObject, EJBLocalHome, EJBObject),
causing a false negative for EJB component classes declared via the
@Stateless/@Stateful/@Singleton/@MessageDriven annotations, which is the
canonical way to declare enterprise beans since EJB 3.0.
Root cause: the XPath predicate only inspected the ImplementsList and ignored
the modifier annotations. We add an annotation-based branch using
pmd-java:hasAnnotation for both the javax.ejb and jakarta.ejb namespaces
(jakarta replaces javax in Jakarta EE 9+).
hasAnnotation is preferred over matching the annotation simple name, as the
latter would also match unrelated annotations sharing the same simple name,
e.g. javax.inject.Singleton (CDI), which is out of scope for this rule.
@MessageDriven (message-driven beans) is included as well: the EJB "no
read/write static fields" programming restriction applies to message-driven
beans just as it does to session beans, so leaving them out would keep the
rule inconsistent (flagging session beans but not MDBs). The ImplementsList
branch is left untouched, so EJB 2.x style declarations are still detected.
Test cases cover @Stateless/@stateful (javax.ejb), @singleton (jakarta.ejb),
@MessageDriven (both javax.ejb and jakarta.ejb), a negative case with
final/instance fields, and a negative case ensuring javax.inject.Singleton
(CDI) is not flagged.
Signed-off-by: 付典 <[email protected]>
…6537
# Conflicts:
# docs/pages/release_notes.md
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.
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.
|