Skip to content

[java] Fix #7060: Fix constant classification of final variable references - #7062

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
aoto-tech:fix-java-local-constant-classification
Sep 27, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
aoto-tech:fix-java-local-constant-classification

Conversation

@aoto-tech

@aoto-tech aoto-tech commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

For final int size = 8, getConstValue() currently returns null for size * 2 despite folding it to 16. This PR recognizes final primitive and String variables initialized with constant expressions, including locals and unqualified instance fields (JLS 4.12.4).

Numeric references are converted to the declared variable type before further evaluation. This fixes final long l = 1; l << 40 producing 256, final char c = 65; "" + c producing "65", and final double d = 1; d / 2 producing 0. Boxed fields remain nonconstant with both simple and qualified access. Only type-name qualifiers are eligible for constant expressions; this.L and make().L are excluded (JLS 15.29).

Regression tests cover each declaration context, numeric conversion, qualifier, and nonconstant initializer separately. Release notes document these effects on built-in rules and custom rules using getConstValue(), isCompileTimeConstant(), or XPath @CompileTimeConstant:

  • LiteralsFirstInComparisons recognizes final local and unqualified instance String constants.
  • UnconditionalIfStatement excludes final local boolean flags used for conditional compilation.
  • UnusedNullCheckInEquals recognizes these String constants as non-null receivers.
  • AvoidReassigningLoopVariables, with forReassign=skip, accepts a final local increment constant equal to one.
  • UnusedAssignment recognizes assignments skipped by constant short-circuit conditions.

FinalFieldCouldBeStatic avoids new reports for instance-field references through an explicit guard. Excluding boxed fields from constant expressions also removes reports for casts and conditionals such as (int) BOXED and true ? BOXED : 0, where BOXED is a static final Integer. Direct static field references are still reported; a regression test covers this distinction. RedundantFieldInitializer continues to exclude named references. Tests cover both behaviors.

Maintainer decision

The #575 test for LiteralsFirstInComparisons changes from zero to one violation for an instance String constant used as the comparison argument. Please confirm whether to adopt the corrected constant classification here or preserve the historical exception explicitly in the rule.

Related issues

Validation

  • Regression tests failed before the corresponding fixes. Direct probes reproduced the incorrect numeric results and confirmed the documented rule changes.
  • Full Java-module verification at f79a12c (./mvnw -B -pl pmd-java verify): BUILD SUCCESS; 10,187 tests, zero failures/errors. The 21 skipped tests are identical to the preceding run. Checkstyle, CPD, and API compatibility checks passed; PMD still reports the same six warnings in unchanged files.
  • The additional boxed-field regression passes with all 30 FinalFieldCouldBeStatic tests and Checkstyle. A controlled comparison using the main versions of the constant folder, field symbol, and rule reports all three initializers; the current implementation reports only the direct reference.
  • The full-repository ./mvnw clean verify has not been run locally. Hosted CI requires maintainer approval; the regression tester report has not been available for review.

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)

@aoto-tech
aoto-tech force-pushed the fix-java-local-constant-classification branch from 26fa43c to bd34ad2 Compare September 11, 2026 18:07
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 18 new violations, 0 new errors and 0 new configuration errors,
removes 22 violations, 0 errors and 0 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-09-27 20:29:10+00:00 for 8b0a48c)

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great addition. Restricting constant values to just static fields was definitely a misreading of the JLS. Thank you!

@aoto-tech
aoto-tech marked this pull request as ready for review September 12, 2026 12:52
aoto-tech and others added 2 commits September 12, 2026 21:52
…ification

# Conflicts:
#	docs/pages/release_notes.md
#	pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedAssignment.xml

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 27, 2026
@UncleOwen
UncleOwen merged commit 7774e57 into pmd:main Sep 27, 2026
13 checks passed
@adangel adangel changed the title [java] Fix constant classification of final variable references [java] Fix #7060: Fix constant classification of final variable references Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] getConstValue() returns null for constant expressions referencing final local variables

3 participants