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 #4870: ArrayIsStoredDirectly checks enum constructors by lyn1dev · Pull Request #7381 · pmd/pmd

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026

Copy link
Copy Markdown

Describe the PR

ArrayIsStoredDirectly did not report an array that an enum constructor stores.
Enum constructors are implicitly private and cannot be declared with another
visibility, so the allowPrivate exemption (default true) disabled the rule for
every one of them. In the reported case an enum constant passes an array that is
declared outside the enum (C(strings)), so the stored array stays reachable and
mutable by the caller.

The rule now checks enum constructors. A varargs parameter of such a constructor
is only reported when an enum constant passes an array to it; when the constants
pass plain elements, the compiler creates the array for the call, so nothing
outside can reach the stored array. That keeps the behaviour agreed in #1413.

Related issues

  • Fix [java] ArrayIsStoredDirectly for enum constructor when array is not a compile-time constant #4870

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)

Testing

Run in pmd-java with JDK 21 (Temurin 21.0.12.1):

  • ./mvnw -pl pmd-java -Dtest=ArrayIsStoredDirectlyTest test -
    25 tests pass with the change. With the rule reverted to main, the two new
    cases fail with expected: <1> but was: <0>.
  • ./mvnw -pl pmd-java verify - 10857 tests, 0 failures, 0 Checkstyle
    violations; pmd:check and cpd-check also pass.

AI was used to write this code.

Sorry, something went wrong.

Uh oh!

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

All reactions
@lyn1dev
[java] Fix pmd#4870: ArrayIsStoredDirectly checks enum constructors
211411c
zbynek
zbynek reviewed Nov 25, 2026
View reviewed changes
Comment thread ...est/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/ArrayIsStoredDirectly.xml
<expected-linenumbers>8</expected-linenumbers>
<code><![CDATA[
public enum Foo {
A(new String[]{"a"}),

@zbynek zbynek Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

The case where the array is directly initialized in the parameter is equivalent to the varargs syntax, so it would be nice to allow it too.

Sorry, something went wrong.

Uh oh!

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

All reactions

@lyn1dev lyn1dev Nov 26, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Author

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.

Updated in d5da9de to allow inline array allocations (ASTArrayAllocation) and null literals alongside varargs expansions in enum constructors, so A(new String[]{a}) is no longer flagged while passing an external array reference (such as C(strings)) is still reported. Also scoped enum constructor detection to direct ASTEnumBody children (excluding nested classes inside enums), handled this(...) delegations within the enum body, and updated the rule description and XML test cases (ArrayIsStoredDirectlyTest: 27 tests passing, checkstyle:check: 0 violations).

Sorry, something went wrong.

Uh oh!

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

All reactions
@lyn1dev
[java] Fix pmd#4870: Allow inline array allocations in enum constructors
d5da9de
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 26, 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 11 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-11-28 00:12:41+00:00 for 80c8b27)

All reactions

Sorry, something went wrong.

Uh oh!

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

@lyn1dev
Merge branch 'main' into fix-4870-array-is-stored-directly-enum
80c8b27

This branch has not been deployed

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

Reviewers

@zbynek zbynek zbynek left review comments

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[java] ArrayIsStoredDirectly for enum constructor when array is not a compile-time constant

Uh oh!

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

2 participants

@lyn1dev @zbynek
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.