Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,7 @@ static <T extends Tree> T importComments(CompilationInfo info, T original, Compi
TokenSequence<JavaTokenId> seq = tokens.tokenSequence(JavaTokenId.language());
TreePath tp = TreePath.getPath(cut, original);
Tree toMap = original;
TreePath toMapPath = tp;
Tree mapTarget = null;

if (tp != null && original.getKind() != Kind.COMPILATION_UNIT) {
Expand Down Expand Up @@ -1579,17 +1580,33 @@ static <T extends Tree> T importComments(CompilationInfo info, T original, Compi
}
if (p2 != null) {
toMap = p2.getLeaf();
toMapPath = p2;
}
if (toMap == tp.getLeaf()) {
// go at least one level up in a hope it's sufficient.
toMap = tp.getParentPath().getLeaf();
toMapPath = tp.getParentPath();
}
}
if (mapTarget == null) {
mapTarget = original;
}

AssignComments translator = new AssignComments(info, mapTarget, seq, unit);


if (toMapPath != null &&
toMapPath.getParentPath() != null &&
TreeUtilities.CLASS_TREE_KINDS.contains(toMapPath.getParentPath().getLeaf().getKind())) {
//run the mapping the preceding member, so that its trailing comments are properly consumed:
ClassTree enclClass = (ClassTree) toMapPath.getParentPath().getLeaf();
List<? extends Tree> members = enclClass.getMembers();
int currentIndex = members.indexOf(toMap);

if (currentIndex > 0) {
translator.scan(members.get(currentIndex - 1), null);
}
}

translator.scan(toMap, null);

return original;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,4 +778,64 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void aVoid) {
}
}

public void testImproperlyMappedCommentsMembersStartFromModifiers() throws Exception {
File testFile = new File(work, "Test.java");
final String origin =
"""
public class Test {
static final int I = 1; //trailing

//preceding
@Deprecated
void test() {
}

Test() {}
}
""";
TestUtilities.copyStringToFile(testFile, origin);
JavaSource src = getJavaSource(testFile);

Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(JavaSource.Phase.PARSED);
final CommentHandlerService service = CommentHandlerService.instance(workingCopy.impl.getJavacTask().getContext());

ErrorAwareTreeScanner<Void, Void> w = new ErrorAwareTreeScanner<Void, Void>() {
@Override
public Void visitClass(ClassTree node, Void p) {
super.visitClass(node, p);

//verify the comments for the field I are not mapped:
Tree fieldMember = node.getMembers().get(0);

assertEquals(Tree.Kind.VARIABLE, fieldMember.getKind());
assertEquals("I", ((VariableTree) fieldMember).getName().toString());

CommentSetImpl fieldCommentSet = service.getComments(fieldMember);

assertFalse(fieldCommentSet.areCommentsMapped());
verify(fieldMember, CommentSet.RelativePosition.PRECEDING, service);
verify(fieldMember, CommentSet.RelativePosition.INLINE, service);
verify(fieldMember, CommentSet.RelativePosition.TRAILING, service);
return null;
}

@Override
public Void visitMethod(MethodTree node, Void p) {
if (node.getName().contentEquals("test")) {
workingCopy.getTreeUtilities().getComments(node.getModifiers(), true);
verify(node, CommentSet.RelativePosition.PRECEDING, service, "//preceding");
verify(node, CommentSet.RelativePosition.INLINE, service);
verify(node, CommentSet.RelativePosition.TRAILING, service);
}
return super.visitMethod(node, p);
}
};
w.scan(workingCopy.getCompilationUnit(), null);
}
};

src.runModificationTask(task);
}
}
Loading