Skip to content
Permalink
Browse files
8250766: javadoc adds redundant spaces when @see program element is w…
…rapped

Reviewed-by: prappo
  • Loading branch information
hns committed May 5, 2021
1 parent 61bb6ec commit f07bb2f4b986103bba975de29324c7219c14628d
@@ -35,6 +35,7 @@
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -1024,7 +1025,6 @@ public Content seeTagToContent(Element element, DocTree see, TagletWriterImpl.Co
case REFERENCE -> {
// @see reference label...
label = ref.subList(1, ref.size());
break;
}
default ->
throw new IllegalStateException(ref.get(0).getKind().toString());
@@ -1039,8 +1039,9 @@ public Content seeTagToContent(Element element, DocTree see, TagletWriterImpl.Co
Content labelContent = plainOrCode(isLinkPlain,
commentTagsToContent(see, element, label, context));

//The text from the @see tag. We will output this text when a label is not specified.
Content text = plainOrCode(kind == LINK_PLAIN, Text.of(removeTrailingSlash(seeText)));
// The signature from the @see tag. We will output this text when a label is not specified.
Content text = plainOrCode(isLinkPlain,
Text.of(Objects.requireNonNullElse(ch.getReferencedSignature(see), "")));

TypeElement refClass = ch.getReferencedClass(see);
Element refMem = ch.getReferencedMember(see);
@@ -1149,10 +1150,10 @@ public Content seeTagToContent(Element element, DocTree see, TagletWriterImpl.Co
}
}

text = plainOrCode(kind == LINK_PLAIN, Text.of(refMemName));

return getDocLink(HtmlLinkInfo.Kind.SEE_TAG, containing,
refMem, (labelContent.isEmpty() ? text: labelContent), null, false);
refMem, (labelContent.isEmpty()
? plainOrCode(isLinkPlain, Text.of(refMemName))
: labelContent), null, false);
}
}

@@ -489,7 +489,7 @@ protected void buildSerialFieldTagsInfo(Content serializableFieldsTree) {
// TODO: Print the signature directly, if it is an array, the
// current DocTree APIs makes it very hard to distinguish
// an as these are returned back as "Array" a DeclaredType.
if (refSignature.endsWith("[]")) {
if (refSignature != null && refSignature.endsWith("[]")) {
te = null;
fieldType = refSignature;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, 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
@@ -461,11 +461,63 @@ public String getReferencedSignature(DocTree dtree) {
return new ReferenceDocTreeVisitor<String>() {
@Override
public String visitReference(ReferenceTree node, Void p) {
return node.getSignature();
return normalizeSignature(node.getSignature());
}
}.visit(dtree, null);
}

@SuppressWarnings("fallthrough")
private static String normalizeSignature(String sig) {
if (sig == null
|| (!sig.contains(" ") && !sig.contains("\n")
&& !sig.contains("\r") && !sig.endsWith("/"))) {
return sig;
}
StringBuilder sb = new StringBuilder();
char lastChar = 0;
for (int i = 0; i < sig.length(); i++) {
char ch = sig.charAt(i);
switch (ch) {
case '\n':
case '\r':
case '\f':
case '\t':
case ' ':
// Add at most one space char, or none if it isn't needed
switch (lastChar) {
case 0:
case'(':
case'<':
case ' ':
case '.':
break;
default:
sb.append(' ');
lastChar = ' ';
break;
}
break;
case ',':
case '>':
case ')':
case '.':
// Remove preceding space character
if (lastChar == ' ') {
sb.setLength(sb.length() - 1);
}
// fallthrough
default:
sb.append(ch);
lastChar = ch;
}
}
// Delete trailing slash
if (lastChar == '/') {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}

private static class ReferenceDocTreeVisitor<R> extends SimpleDocTreeVisitor<R, Void> {
@Override
public R visitSee(SeeTree node, Void p) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, 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 8017191 8182765 8200432 8239804
* @bug 8017191 8182765 8200432 8239804 8250766
* @summary Javadoc is confused by at-link to imported classes outside of the set of generated packages
* @library ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
@@ -43,6 +43,7 @@ public static void main(String... args) throws Exception {
@Test
public void test() {
javadoc("-d", "out",
"--no-platform-links",
"-sourcepath", testSrc,
"pkg");
checkExit(Exit.OK);
@@ -55,7 +56,9 @@ public void test() {
<dd><a href="Test.InnerOne.html#foo()"><code>Test.InnerOne.foo()</code></a>,\s
<a href="Test.InnerOne.html#bar(java.lang.Object)"><code>Test.InnerOne.bar(Object)</code></a>,\s
<a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#see">Javadoc</a>,\s
<a href="Test.InnerOne.html#baz(float)"><code>something</code></a></dd>
<a href="Test.InnerOne.html#baz(float)"><code>something</code></a>,\s
<a href="Test.InnerOne.html#format(java.lang.String,java.lang.Object...)"><code>Test\
.InnerOne.format(java.lang.String, java.lang.Object...)</code></a></dd>
</dl>""");
checkOutput("pkg/Test.html", false,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, 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
@@ -28,11 +28,14 @@
public class Test {

/**
* Testing different combos of see tags.
* Testing different combos of see tags, including some weird formatting.
* @see InnerOne#foo()
* @see InnerOne#bar(Object)
* @see InnerOne#bar(
* Object
* )
* @see <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#see">Javadoc</a>
* @see InnerOne#baz(float priority) something
* @see InnerOne#format( java .lang.String , java. lang.Object ... )
*/
public void foo() {}

@@ -54,6 +57,13 @@ public void bar(Object o) {}
* @param GravitationalConstant
*/
public void baz(float GravitationalConstant) {}

/**
* Test for multiple args and varargs.
*/
public static String format(String s, Object... args) {
return String.format(s, args);
}
}
}

0 comments on commit f07bb2f

Please sign in to comment.