Repository navigation
[java] Fix #7060: Fix constant classification of final variable references - #7062
Merged
UncleOwen merged 3 commits intoSep 27, 2026
Merged
Conversation
aoto-tech
force-pushed
the
fix-java-local-constant-classification
branch
from
September 11, 2026 18:07
26fa43c to
bd34ad2
Compare
Contributor
|
Compared to main: (comment created at 2026-09-27 20:29:10+00:00 for 8b0a48c) |
oowekyala
reviewed
Sep 12, 2026
oowekyala
left a comment
Member
There was a problem hiding this comment.
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
marked this pull request as ready for review
September 12, 2026 12:52
…ification # Conflicts: # docs/pages/release_notes.md # pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedAssignment.xml
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe the PR
For
final int size = 8,getConstValue()currently returnsnullforsize * 2despite folding it to16. 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 << 40producing256,final char c = 65; "" + cproducing"65", andfinal double d = 1; d / 2producing0. Boxed fields remain nonconstant with both simple and qualified access. Only type-name qualifiers are eligible for constant expressions;this.Landmake().Lare 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:LiteralsFirstInComparisonsrecognizes final local and unqualified instance String constants.UnconditionalIfStatementexcludes final local boolean flags used for conditional compilation.UnusedNullCheckInEqualsrecognizes these String constants as non-null receivers.AvoidReassigningLoopVariables, withforReassign=skip, accepts a final local increment constant equal to one.UnusedAssignmentrecognizes assignments skipped by constant short-circuit conditions.FinalFieldCouldBeStaticavoids 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) BOXEDandtrue ? BOXED : 0, whereBOXEDis a static final Integer. Direct static field references are still reported; a regression test covers this distinction.RedundantFieldInitializercontinues to exclude named references. Tests cover both behaviors.Maintainer decision
The
#575test forLiteralsFirstInComparisonschanges 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
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.FinalFieldCouldBeStatictests 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../mvnw clean verifyhas not been run locally. Hosted CI requires maintainer approval; the regression tester report has not been available for review.Ready?
./mvnw clean verifypasses (checked automatically by github actions)