Skip to content

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

Closed

Description

@Carlson-JLQ

Rule: StaticEJBFieldShouldBeFinal

False Negative: Mutable static EJB fields are not recognized across declaration variants.

Version
7.22.0

Checker

  • Checker id: category/java/errorprone.xml/StaticEJBFieldShouldBeFinal
  • Checker description: This checker detects static field declarations in EJB-related classes that are not declared final.

Description of the false negative

diff/output marks 1 false negatives for category/java/errorprone.xml/StaticEJBFieldShouldBeFinal. Across these samples, the benchmark is still exercising the same underlying bug pattern, but the implementation appears to lose track of it once the source deviates from one canonical shape. The recurring themes are suppression or annotations.

Affected test cases

PosCase1.java

Source: output/codeql/EjbStaticFieldNonFinal/src/main/java/scensct/core/pos/PosCase1.java

The sample keeps the benchmark's target pattern but expresses it in a slightly non-canonical source form. The risky pattern is still present in the code, so the absence of a report points to a matching gap rather than a benign variant. That is also how the benchmark classifies the sample: An EJB component accesses a non-final static field via a method call should be flagged as violation.

// An EJB component accesses a non-final static field via a method call should be flagged as violation.
package scensct.core.pos;

import javax.ejb.Stateless;

@Stateless
public class PosCase1 {
    private static int nonFinalStatic = 0; // Non-final static field

    public void targetMethod() {
        int value = nonFinalStatic; // Accessing non-final static field
    }

    public void ejbBusinessMethod() { // [REPORTED LINE]
        targetMethod(); // Calling the method that accesses the static field
    }
}

Cause analysis

The rule is documented as follows: This checker detects static field declarations in EJB-related classes that are not declared final.

In this set the problem shows up around suppression or annotations. The missed cases suggest that the implementation is narrower than the rule description and too dependent on one preferred AST shape.

Once the same behavior is expressed through a helper, an overload, a modifier such as abstract or native, a different comment position, or a slightly rearranged control-flow structure, the report disappears even though the benchmark still treats the case as in-scope.

For category/java/errorprone.xml/StaticEJBFieldShouldBeFinal, the practical improvement would be to generalize the match so it follows the underlying behavior rather than one exact spelling of it.

References

None known.

Activity

  1. added
    a:false-negative
    on Mar 23, 2026
  2. zbynek commented on Mar 25, 2026

    @zbynek
    Contributor

    I guess the issue you're actually trying to describe is that classes annotated javax.ejb.Stateless should be considered components, while the rule currently only considers classes implementing one of five interfaces.

    pmd-java:typeIs('javax.ejb.SessionBean')
         or pmd-java:typeIs('javax.ejb.EJBHome')
         or pmd-java:typeIs('javax.ejb.EJBLocalObject')
         or pmd-java:typeIs('javax.ejb.EJBLocalHome')
         or pmd-java:typeIs('javax.ejb.EJBObject')
    

    I'm not sure if that's needed, but the rule should be definitely updated to support jakarta along with javax packages.

  3. changed the title [-]False Negative: Mutable static EJB fields are not recognized across declaration variants.[/-] [+][java] False Negative: Mutable static EJB fields are not recognized across declaration variants.[/+] on May 16, 2026
  4. added 3 commits that reference this issue on Aug 12, 2026
    a447560
    91da19c
    c0c1a0b
  5. changed the title [-][java] False Negative: Mutable static EJB fields are not recognized across declaration variants.[/-] [+][java] StaticEJBFieldShouldBeFinal: False Negative when using @Stateless etc.[/+] on Aug 20, 2026
  6. added this to the 7.27.0 milestone on Aug 20, 2026
  7. added a commit that references this issue on Aug 20, 2026
    89b473e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions