Skip to content

Commit 2e97d0e

Browse files
committed
When mapping comments for modifiers of class members, make sure trailing comments for the preceding member are ignored.
1 parent 233a70f commit 2e97d0e

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

java/java.source.base/src/org/netbeans/api/java/source/GeneratorUtilities.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ static <T extends Tree> T importComments(CompilationInfo info, T original, Compi
15471547
TokenSequence<JavaTokenId> seq = tokens.tokenSequence(JavaTokenId.language());
15481548
TreePath tp = TreePath.getPath(cut, original);
15491549
Tree toMap = original;
1550+
TreePath toMapPath = tp;
15501551
Tree mapTarget = null;
15511552

15521553
if (tp != null && original.getKind() != Kind.COMPILATION_UNIT) {
@@ -1579,17 +1580,33 @@ static <T extends Tree> T importComments(CompilationInfo info, T original, Compi
15791580
}
15801581
if (p2 != null) {
15811582
toMap = p2.getLeaf();
1583+
toMapPath = p2;
15821584
}
15831585
if (toMap == tp.getLeaf()) {
15841586
// go at least one level up in a hope it's sufficient.
15851587
toMap = tp.getParentPath().getLeaf();
1588+
toMapPath = tp.getParentPath();
15861589
}
15871590
}
15881591
if (mapTarget == null) {
15891592
mapTarget = original;
15901593
}
1594+
15911595
AssignComments translator = new AssignComments(info, mapTarget, seq, unit);
1592-
1596+
1597+
if (toMapPath != null &&
1598+
toMapPath.getParentPath() != null &&
1599+
TreeUtilities.CLASS_TREE_KINDS.contains(toMapPath.getParentPath().getLeaf().getKind())) {
1600+
//run the mapping the preceding member, so that its trailing comments are properly consumed:
1601+
ClassTree enclClass = (ClassTree) toMapPath.getParentPath().getLeaf();
1602+
List<? extends Tree> members = enclClass.getMembers();
1603+
int currentIndex = members.indexOf(toMap);
1604+
1605+
if (currentIndex > 0) {
1606+
translator.scan(members.get(currentIndex - 1), null);
1607+
}
1608+
}
1609+
15931610
translator.scan(toMap, null);
15941611

15951612
return original;

java/java.source.base/test/unit/src/org/netbeans/api/java/source/CommentCollectorTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,4 +778,64 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void aVoid) {
778778
}
779779
}
780780

781+
public void testImproperlyMappedCommentsMembersStartFromModifiers() throws Exception {
782+
File testFile = new File(work, "Test.java");
783+
final String origin =
784+
"""
785+
public class Test {
786+
static final int I = 1; //trailing
787+
788+
//preceding
789+
@Deprecated
790+
void test() {
791+
}
792+
793+
Test() {}
794+
}
795+
""";
796+
TestUtilities.copyStringToFile(testFile, origin);
797+
JavaSource src = getJavaSource(testFile);
798+
799+
Task<WorkingCopy> task = new Task<WorkingCopy>() {
800+
public void run(final WorkingCopy workingCopy) throws Exception {
801+
workingCopy.toPhase(JavaSource.Phase.PARSED);
802+
final CommentHandlerService service = CommentHandlerService.instance(workingCopy.impl.getJavacTask().getContext());
803+
804+
ErrorAwareTreeScanner<Void, Void> w = new ErrorAwareTreeScanner<Void, Void>() {
805+
@Override
806+
public Void visitClass(ClassTree node, Void p) {
807+
super.visitClass(node, p);
808+
809+
//verify the comments for the field I are not mapped:
810+
Tree fieldMember = node.getMembers().get(0);
811+
812+
assertEquals(Tree.Kind.VARIABLE, fieldMember.getKind());
813+
assertEquals("I", ((VariableTree) fieldMember).getName().toString());
814+
815+
CommentSetImpl fieldCommentSet = service.getComments(fieldMember);
816+
817+
assertFalse(fieldCommentSet.areCommentsMapped());
818+
verify(fieldMember, CommentSet.RelativePosition.PRECEDING, service);
819+
verify(fieldMember, CommentSet.RelativePosition.INLINE, service);
820+
verify(fieldMember, CommentSet.RelativePosition.TRAILING, service);
821+
return null;
822+
}
823+
824+
@Override
825+
public Void visitMethod(MethodTree node, Void p) {
826+
if (node.getName().contentEquals("test")) {
827+
workingCopy.getTreeUtilities().getComments(node.getModifiers(), true);
828+
verify(node, CommentSet.RelativePosition.PRECEDING, service, "//preceding");
829+
verify(node, CommentSet.RelativePosition.INLINE, service);
830+
verify(node, CommentSet.RelativePosition.TRAILING, service);
831+
}
832+
return super.visitMethod(node, p);
833+
}
834+
};
835+
w.scan(workingCopy.getCompilationUnit(), null);
836+
}
837+
};
838+
839+
src.runModificationTask(task);
840+
}
781841
}

0 commit comments

Comments
 (0)