Skip to content

Issue #18843: Indentation Check Handlers should not have reference to…#18754

Merged
romani merged 1 commit into
checkstyle:masterfrom
smita1078:FixIndentation
Feb 5, 2026
Merged

Issue #18843: Indentation Check Handlers should not have reference to…#18754
romani merged 1 commit into
checkstyle:masterfrom
smita1078:FixIndentation

Conversation

@smita1078

@smita1078 smita1078 commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #18843
Part of #14121

Changes

  • Initialize incorrectIndentationLines eagerly to fix SpotBugs UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR warning
  • Changed to private final Set<Integer> incorrectIndentationLines = new HashSet<>()
  • Updated beginTree() to use clear() instead of reassignment
  • Remove SpotBugs suppression for this violation

@smita1078 smita1078 force-pushed the FixIndentation branch 3 times, most recently from a18a792 to d4074df Compare January 25, 2026 14:33
<mutator>org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator</mutator>
<description>removed call to java/util/Set::clear</description>
<lineContent>incorrectIndentationLines.clear();</lineContent>
</mutation>

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.

Please do not add the pitest suppression. Instead, you can move the incorrectIndentationLines.clear() call into the clearState() method, since it serves the same purpose as the other method calls inside that method, clearState() is excluded from pitest so the mutation will not be generated and no suppression is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@smita1078 smita1078 force-pushed the FixIndentation branch 2 times, most recently from 44fb36a to 631fcca Compare January 25, 2026 15:59
Comment thread pom.xml
Comment on lines 4015 to 4017
<param>clearState</param>
<param>logOncePerLine</param>
</excludedMethods>

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.

why do we need to add logOncePerLine in the excludedMethods, i think its not needed , what pitest mutation made you do so ??

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.

We can not simply exclude from pitest, we need to cover by tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes removed, from previous run VoidMethodCallMutator was there, now its not coming anymore

@romani

romani commented Jan 27, 2026

Copy link
Copy Markdown
Member

@vivek-0509 , please finish review, I will review after you.

@vivek-0509

Copy link
Copy Markdown
Member

@romani
I have reviewed the PR.
From what I see, this fixes the SpotBugs warning and removes handlers reliance on a public behavioral method by making the logging method package-private.
One point I wanted to clarify, is the intent of the issue to eliminate the class-level reference from handlers to
IndentationCheck entirely, or to ensure handlers don't depend on the check's public API contract ?
This PR keeps the reference but limits interaction to package-private scope and read-only configuration access. Just wanted to confirm that this matches the intended scope since issue feels little unclear to me.

@romani

romani commented Jan 30, 2026

Copy link
Copy Markdown
Member

Goal is to avoid cycle dependencies between classes.
Check can be aware of all handlers and orchestrate them. Handlers should not know anything about Check and just give all data/behavior to Check.

@vivek-0509

Copy link
Copy Markdown
Member

Currently, this PR addresses the SpotBugs warning but does not remove the cyclic dependency between IndentationCheck
and handlers. To completely remove the cyclic dependency, we would need significant refactoring.

From my overview, I think we could create an immutable context record containing all the configuration details that handlers need (basicOffset, caseIndent, throwsIndent, etc.). Handlers would use this config to perform their indentation checks. Additionally, we can have a violation context where handlers log violations into a list, then return that list. The Check would then collect these violations and log them.

However, I need to think more about how to achieve this properly.
@smita1078 Please think in this direction, or if you have a better approach, please share it. We can discuss and then move forward with the refactoring.

@smita1078

Copy link
Copy Markdown
Contributor Author

Currently, this PR addresses the SpotBugs warning but does not remove the cyclic dependency between IndentationCheck and handlers. To completely remove the cyclic dependency, we would need significant refactoring.

From my overview, I think we could create an immutable context record containing all the configuration details that handlers need (basicOffset, caseIndent, throwsIndent, etc.). Handlers would use this config to perform their indentation checks. Additionally, we can have a violation context where handlers log violations into a list, then return that list. The Check would then collect these violations and log them.

However, I need to think more about how to achieve this properly. @smita1078 Please think in this direction, or if you have a better approach, please share it. We can discuss and then move forward with the refactoring.

@vivek-0509
Thanks for the detailed analysis! I agree with your approach of using an immutable context record for configuration.
For the violation handling, instead of having handlers return a list of violations, I'd think we can use a callback interface pattern.
Create a ViolationReporter interface with a single method: logOncePerLine then IndentationCheck would implement this interface. Handlers would receive this interface via constructor: Handler(IndentationConfig config, ViolationReporter reporter, ...) this would break circular dependency as handlers depend on interface, not Check

@romani

romani commented Feb 3, 2026

Copy link
Copy Markdown
Member

Ok, sounds like this issue is epic, and will not be easy, but doable.

Let's split it in few issues and make sure we can reliably do refactoring.

For this PR, let's make special issue to describe small goal and merge this update, as step forward.

@vivek-0509

vivek-0509 commented Feb 3, 2026

Copy link
Copy Markdown
Member

@smita1078 Please create an issue for this PR (the current SpotBugs fix) so we can merge it as a step forward. We need to decide on the approach before proceding with refactoring ,will continue the discussion at #14121.

@smita1078 smita1078 changed the title Issue #14121: Indentation Check Handlers should not have reference to… Issue #18843: Indentation Check Handlers should not have reference to… Feb 3, 2026
@smita1078

Copy link
Copy Markdown
Contributor Author

@smita1078 Please create an issue for this PR (the current SpotBugs fix) so we can merge it as a step forward. We need to decide on the approach before proceding with refactoring ,will continue the discussion at #14121.

@vivek-0509 Created issue for this PR. #18843
The scope is limited to:
Fix SpotBugs UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR warning
Reduce public API surface (public → package-private for logging method)
This doesn't fully resolve #14121. Will continue the architectural refactoring discussion at #14121.

@romani

romani commented Feb 3, 2026

Copy link
Copy Markdown
Member

@vivek-0509 , please finish review

Comment on lines +315 to +322
* <p>For use by indentation handlers in this package.</p>
*
* @see java.text.MessageFormat
* @param ast the AST node for which the error is logged
* @param key the message key describing the violation
* @param args optional arguments for the message
*/
public void indentationLog(DetailAST ast, String key, Object... args) {
if (!incorrectIndentationLines.contains(ast.getLineNo())) {
incorrectIndentationLines.add(ast.getLineNo());
/* package */ void logOncePerLine(DetailAST ast, String key, Object... args) {
if (incorrectIndentationLines.add(ast.getLineNo())) {

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.

Please revert the visibility/rename changes to indentationLog method. This PR should only fix the SpotBugs warning for the scope of issue #18843. Refctoring changes will be done separately once we agree on the approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@smita1078 smita1078 force-pushed the FixIndentation branch 5 times, most recently from ff698e1 to 3cfdf29 Compare February 4, 2026 06:58
Comment on lines 458 to -460
}
}

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.

please revert any unrelated change

@smita1078 smita1078 Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, Sry! missed it earlier

@vivek-0509

Copy link
Copy Markdown
Member

@romani PR looks good ok to merge

@romani

romani commented Feb 4, 2026

Copy link
Copy Markdown
Member

@vivek-0509 , please finish review

@vivek-0509

Copy link
Copy Markdown
Member

@romani Already done #18754 (comment)

@romani

romani commented Feb 5, 2026

Copy link
Copy Markdown
Member

Github, generate report for Indentation/all-examples-in-one

@github-actions

github-actions Bot commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

@romani romani 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 a lot

@romani romani merged commit c1f7520 into checkstyle:master Feb 5, 2026
121 checks passed
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.

Fix SpotBugs UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR warning in IndentationCheck

3 participants