Repository navigation
A constructor keeps the comments in front of its closing brace - #102
Merged
Merged
Conversation
A constructor's body is not a CodeBlock in the parser's tree: its '{', statements and '}' are children of the
ConstructorDeclaration itself, where a method's body is one CodeBlock child. The comments in front of a body's
closing '}' were recorded for CodeBlocks only, so in a constructor they were recorded nowhere:
`C() { // inside }` parsed and printed as `C(){}`, `D() { int i = 0; // after }` as `D(){int i=0;}`.
SourceCodeScan now records them for a constructor too, under the body's source from '{' to '}', where
ScanCompilationUnit.parseBlock looks them up.
TestCommentInEmptyConstructor: both constructor cases, and the same two bodies in a method as controls; all four
compare the whole printed member. maddi-java-openjdk: 671 tests, 0 failures, 9 skipped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015GLk2yT8ii4MQN3oTGaEXQ
Signed-off-by: Tom Tourwe <Tom.Tourwe@codelaser.io>
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.
A comment in front of a constructor's closing brace was lost:
C() { // inside }parsed to a constructor with no comment anywhere and printed asC(){}, andD() { int i = 0; // after }printed asD(){int i=0;}. The same bodies in a method keeptheir comments.
Cause. In the parser's tree a constructor's body is not a
CodeBlock: its{, statements and}are children of theConstructorDeclarationitself, where a method's body is oneCodeBlockchild.SourceCodeScanrecorded the comments infront of a closing
}forCodeBlocks only, so for a constructor it recorded nowhere.Fix.
SourceCodeScanrecords them for a constructor too, under the body's source from{to}, which is whereScanCompilationUnit.parseBlocklooks them up.Why it matters. A tool that rewrites a constructor cannot see a comment that is not in the model. A dependency-injection migration removed empty constructors whose only content was a comment (
// TODO Auto-generated constructor stub,commented-out code), because the model said the constructor was empty.
Tests.
TestCommentInEmptyConstructor: the two constructor cases, and the same two bodies in a method as controls. All four compare the whole printed member.maddi-java-openjdk: 671 tests, 0 failures, 9 skipped.