GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@fudianchn fudianchn commented Aug 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • Fix [java] StaticEJBFieldShouldBeFinal: False Negative when using @Stateless etc. #6537

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.

All reactions
@fudianchn
fudianchn marked this pull request as draft August 12, 2026 00:37
@fudianchn
fudianchn force-pushed the fix/staticejbfield-stateless-6537 branch from 9921596 to a447560 Compare August 12, 2026 16:37
@fudianchn 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
@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 16:38
@fudianchn
fudianchn force-pushed the fix/staticejbfield-stateless-6537 branch from a447560 to 91da19c Compare August 12, 2026 16:41
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@fudianchn
[java] Fix pmd#6537: StaticEJBFieldShouldBeFinal detects @Stateless/@…
c0c1a0b
…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]>
@fudianchn
fudianchn force-pushed the fix/staticejbfield-stateless-6537 branch from 91da19c to c0c1a0b Compare August 15, 2026 15:22
UncleOwen added 2 commits August 20, 2026 20:42
@UncleOwen
Merge branch 'main' into fork/fudianchn/fix/staticejbfield-stateless-…
8a7f9bf
…6537

# Conflicts:
#	docs/pages/release_notes.md
@UncleOwen
correct ticket name in release notes
7d9a5dc
UncleOwen
UncleOwen approved these changes Aug 20, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

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.

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 20, 2026
@UncleOwen
UncleOwen merged commit 89b473e into pmd:main Aug 20, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.27.0

Development

Successfully merging this pull request may close these issues.

[java] StaticEJBFieldShouldBeFinal: False Negative when using @Stateless etc.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@fudianchn @UncleOwen
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.