8356411: Comment tree not reporting correct position for label

Reviewed-by: liach
This commit is contained in:
Hannes Wallnöfer 2025-08-20 08:38:06 +00:00
parent 169d145e99
commit 908f3c9697
4 changed files with 59 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -358,10 +358,20 @@ public class MarkdownTransformer implements JavacTrees.DocCommentTreeTransformer
}
private DCTree.DCSee transform(DCTree.DCSee tree) {
List<? extends DocTree> ref2 = transform(tree.reference);
return (equal(ref2, tree.getReference()))
// Some extra work is required to accommodate various forms of @see tags, as a
// leading reference affects the position of the label following it (JDK-8356411),
var ref = tree.reference;
var hasReference = !ref.isEmpty() && ref.getFirst().getKind() == DocTree.Kind.REFERENCE;
List<DCTree> transformed = new ArrayList<>();
if (hasReference) {
transformed.add(ref.getFirst());
transformed.addAll(transform(ref.subList(1, ref.size())));
} else {
transformed.addAll(transform(ref));
}
return (equal(ref, transformed))
? tree
: m.at(tree.pos).newSeeTree(ref2);
: m.at(tree.pos).newSeeTree(transformed);
}
private DCTree.DCSerial transform(DCTree.DCSerial tree) {

View File

@ -128,9 +128,9 @@ public class DocCommentTester {
public final boolean useIdentityTransformer;
public DocCommentTester(boolean useBreakIterator, boolean useIdentityTtransformer) {
public DocCommentTester(boolean useBreakIterator, boolean useIdentityTransformer) {
this.useBreakIterator = useBreakIterator;
this.useIdentityTransformer = useIdentityTtransformer;
this.useIdentityTransformer = useIdentityTransformer;
}
public void run(List<String> args) throws Exception {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8298405
* @bug 8298405 8356411
* @summary Markdown support in the standard doclet
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.file
@ -659,5 +659,29 @@ DocComment[DOC_COMMENT, pos:0
]
*/
/// @see Ref label
/// @see <a href="..">link<a>
/// @see "Text"
void seeTags() { }
/*
DocComment[DOC_COMMENT, pos:0
firstSentence: empty
body: empty
block tags: 3
See[SEE, pos:0
reference: 2
Reference[REFERENCE, pos:5, Ref]
RawText[MARKDOWN, pos:9, label]
]
See[SEE, pos:15
reference: 1
RawText[MARKDOWN, pos:20, <a_href="..">link<a>]
]
See[SEE, pos:41
reference: 1
Text[TEXT, pos:46, "Text"]
]
]
*/
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8332858
* @bug 8332858 8356411
* @summary test case for Markdown positions
* @run main/othervm --limit-modules jdk.compiler MarkdownTransformerPositionTest
* @run main MarkdownTransformerPositionTest links
@ -53,6 +53,7 @@ public class MarkdownTransformerPositionTest {
t.simpleTest();
t.testWithReplacements();
t.testSeeTags();
if (args.length > 0 && "links".equals(args[0])) {
t.linkWithEscapes();
@ -83,6 +84,19 @@ public class MarkdownTransformerPositionTest {
"testAuthor");
}
private void testSeeTags() throws Exception {
// @see "Text" does not produce a Markdown text
runTest("""
/// @see Ref label
/// @see <a href="..">link<a>
/// @see "Text"
public class Test {
}
""",
"label",
"<a href=\"..\">link<a>");
}
private void linkWithEscapes() throws Exception {
runConvertedLinksTest("""
/// Markdown comment.
@ -178,4 +192,4 @@ public class MarkdownTransformerPositionTest {
return source;
}
}
}
}