Describe the bug
Spoon first tries to attach a source comment to the most specific matching AST child. For some valid comment positions, the specialized placement visitor does not find a child that accepts the comment. When that happens, JDTCommentBuilder.java logs an error and leaves the comment without a parent, so the comment is missing from the model.
The complete minimal examples are shown below: an annotation with no value expression, a module with no directives, an array access, and a binary operator.
Spoon issue #2482 originally reported comments assigned to the wrong type and a missing end-of-type comment. Spoon PR #2636 fixed that original type-header reproduction by handling comments in declaration modifier ranges. Later comments on #2482 reported the array-access and binary-operator cases covered here. This report covers the remaining path shared by those cases and the annotation and module examples: specialized placement runs, but no child accepts the comment.
A compatible fix must continue to use the specialized placement logic first. If no child accepts the comment, Spoon must attach it to the enclosing AST element instead of dropping it.
Source code you are trying to analyze/transform
Annotation with no value expression:
@Deprecated(/* marker */)
class CommentInEmptyAnnotation {}
Module with no directives:
module comment.fallback {
// exports disabled
}
Comment between an array expression and its index:
class CommentInArrayAccess {
void update(int[] values) {
values // index comment
[1] = 0;
}
}
Comment inside a binary expression:
class CommentInBinaryOperator {
void test(int value) {
if (!(value < 0
// comparison comment
)) {
}
}
}
The same missing fallback produced 21 comment-placement errors while StoneDetector used Spoon and the Eclipse Java compiler (ECJ) to analyze these OpenJDK files:
Source code for your Spoon processing
Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setCommentEnabled(true);
launcher.addInputResource("module-info.java");
launcher.buildModel();
Actual output
"// exports disabled" cannot be added into the AST, with parent class spoon.support.reflect.declaration.CtModuleImpl ...
The comment has no parent and is absent from the enclosing element's comments.
Expected output
Spoon first uses the specialized placement logic. When no suitable child exists, it retains the comment on the enclosing AST element and logs no error.
Spoon Version
Reproduced on Spoon master at 06b187819380700384762416d15db2cc4f112175.
JVM Version
OpenJDK 25.0.3
What operating system are you using?
Linux x86-64
Describe the bug
Spoon first tries to attach a source comment to the most specific matching AST child. For some valid comment positions, the specialized placement visitor does not find a child that accepts the comment. When that happens,
JDTCommentBuilder.javalogs an error and leaves the comment without a parent, so the comment is missing from the model.The complete minimal examples are shown below: an annotation with no value expression, a module with no directives, an array access, and a binary operator.
Spoon issue #2482 originally reported comments assigned to the wrong type and a missing end-of-type comment. Spoon PR #2636 fixed that original type-header reproduction by handling comments in declaration modifier ranges. Later comments on #2482 reported the array-access and binary-operator cases covered here. This report covers the remaining path shared by those cases and the annotation and module examples: specialized placement runs, but no child accepts the comment.
A compatible fix must continue to use the specialized placement logic first. If no child accepts the comment, Spoon must attach it to the enclosing AST element instead of dropping it.
Source code you are trying to analyze/transform
Annotation with no value expression:
Module with no directives:
Comment between an array expression and its index:
Comment inside a binary expression:
The same missing fallback produced 21 comment-placement errors while StoneDetector used Spoon and the Eclipse Java compiler (ECJ) to analyze these OpenJDK files:
WalkerFactory.javacom.foos/module-info.javaNullPointerExceptionTest.javaFdlibmTranslit.javaNumberTest.javaTestGetElementReferenceData.javaapi/mod/module-info.javamVIII/module-info.javaSource code for your Spoon processing
Actual output
The comment has no parent and is absent from the enclosing element's comments.
Expected output
Spoon first uses the specialized placement logic. When no suitable child exists, it retains the comment on the enclosing AST element and logs no error.
Spoon Version
Reproduced on Spoon
masterat06b187819380700384762416d15db2cc4f112175.JVM Version
OpenJDK 25.0.3
What operating system are you using?
Linux x86-64