SONARJAVA-6588 - Implement S8983: Stateless session beans should not store mutable state in instance variables#5758
Conversation
| private static boolean isSafe(VariableTree variable) { | ||
| if (ModifiersUtils.hasAnyOf(variable.modifiers(), Modifier.STATIC, Modifier.FINAL)) { | ||
| return true; | ||
| } | ||
| SymbolMetadata varMetadata = variable.symbol().metadata(); | ||
| return SAFE_FIELD_ANNOTATIONS.stream().anyMatch(varMetadata::isAnnotatedWith); | ||
| } |
There was a problem hiding this comment.
💡 Edge Case: final/static fields holding mutable objects are treated as safe
In isSafe() (StatelessBeanInstanceFieldCheck.java:78-84), any field marked static or final is considered safe and never reported. However the rule targets mutable state, and final/static only make the reference immutable, not the referenced object. A stateless bean with private final List<String> cache = new ArrayList<>(); or private static Map<...> shared = ...; still stores mutable, request-crossing state and can leak data or cause race conditions — exactly the problems the rule description warns about ("Multiple threads may access the same field concurrently"). These cases produce false negatives.
This appears partly intentional (the sample marks static final int MAX_RETRIES and static int sharedCounter as compliant), so it may match the RSPEC scope. If so, no change is needed; otherwise consider inspecting the field's declared type (e.g. mutable collections / non-final referenced objects) rather than relying solely on the static/final modifiers. At minimum, adding a test sample with a final collection field would document the intended behavior explicitly.
Was this helpful? React with 👍 / 👎
|
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsImplements rule S8983 to detect mutable instance fields in 💡 Edge Case: final/static fields holding mutable objects are treated as safe📄 java-checks/src/main/java/org/sonar/java/checks/StatelessBeanInstanceFieldCheck.java:78-84 In This appears partly intentional (the sample marks 🤖 Prompt for agentsOptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary by Gitar
S8983to detect mutable instance fields in classes annotated with@Stateless.static,final, or annotated with container-managed injection markers like@Inject,@EJB, or@PersistenceContext.StatelessBeanInstanceFieldCheckSample.javaandStatelessBeanInstanceFieldCheckJakartaSample.java.Sonar_wayprofile entry forS8983.This will update automatically on new commits.