8187521: In some corner cases the javadoc tool can reuse id attribute

Reviewed-by: bpatel, ksrini
This commit is contained in:
Jonathan Gibbons 2017-10-10 17:02:52 -07:00
parent b890c3ce83
commit f386e419c3
8 changed files with 326 additions and 66 deletions

View File

@ -313,7 +313,8 @@ public abstract class AbstractExecutableMemberWriter extends AbstractMemberWrite
* @return the 1.4.x style anchor for the executable element.
*/
protected String getErasureAnchor(ExecutableElement executableElement) {
final StringBuilder buf = new StringBuilder(name(executableElement) + "(");
final StringBuilder buf = new StringBuilder(writer.anchorName(executableElement));
buf.append("(");
List<? extends VariableElement> parameters = executableElement.getParameters();
boolean foundTypeVariable = false;
for (int i = 0; i < parameters.size(); i++) {

View File

@ -33,6 +33,7 @@ import java.util.regex.Pattern;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.Name;
@ -74,6 +75,7 @@ import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlVersion;
import jdk.javadoc.internal.doclets.formats.html.markup.RawHtml;
import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeWriter;
@ -1468,20 +1470,18 @@ public class HtmlDocletWriter extends HtmlDocWriter {
if (isProperty) {
return executableElement.getSimpleName().toString();
}
String signature = utils.signature(executableElement);
StringBuilder signatureParsed = new StringBuilder();
int counter = 0;
for (int i = 0; i < signature.length(); i++) {
char c = signature.charAt(i);
if (c == '<') {
counter++;
} else if (c == '>') {
counter--;
} else if (counter == 0) {
signatureParsed.append(c);
}
String member = anchorName(executableElement);
String erasedSignature = utils.makeSignature(executableElement, true, true);
return member + erasedSignature;
}
public String anchorName(Element member) {
if (member.getKind() == ElementKind.CONSTRUCTOR
&& configuration.isOutputHtml5()) {
return "<init>";
} else {
return utils.getSimpleName(member);
}
return utils.getSimpleName(executableElement) + signatureParsed.toString();
}
public Content seeTagToContent(Element element, DocTree see) {

View File

@ -59,7 +59,8 @@ public abstract class HtmlDocWriter extends HtmlWriter {
public static final String CONTENT_TYPE = "text/html";
DocPath pathToRoot;
private final HtmlConfiguration configuration;
private final DocPath pathToRoot;
/**
* Constructor. Initializes the destination file name through the super
@ -68,8 +69,9 @@ public abstract class HtmlDocWriter extends HtmlWriter {
* @param configuration the configuration for this doclet
* @param filename String file name.
*/
public HtmlDocWriter(BaseConfiguration configuration, DocPath filename) {
public HtmlDocWriter(HtmlConfiguration configuration, DocPath filename) {
super(configuration, filename);
this.configuration = configuration;
this.pathToRoot = filename.parent().invert();
Messages messages = configuration.getMessages();
messages.notice("doclet.Generating_0",
@ -80,7 +82,9 @@ public abstract class HtmlDocWriter extends HtmlWriter {
* Accessor for configuration.
* @return the configuration for this doclet
*/
public abstract BaseConfiguration configuration();
public BaseConfiguration configuration() {
return configuration;
}
public Content getHyperLink(DocPath link, String label) {
return getHyperLink(link, new StringContent(label), false, "", "", "");
@ -166,8 +170,6 @@ public abstract class HtmlDocWriter extends HtmlWriter {
* @return a valid HTML name string.
*/
public String getName(String name) {
StringBuilder sb = new StringBuilder();
char ch;
/* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions
* that the name/id should begin with a letter followed by other valid characters.
* The HTML 5 spec (draft) is more permissive on names/ids where the only restriction
@ -178,8 +180,14 @@ public abstract class HtmlDocWriter extends HtmlWriter {
* substitute it accordingly, "_" and "$" can appear at the beginning of a member name.
* The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z".
*/
if (configuration.isOutputHtml5()) {
return name.replaceAll(" +", "");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
ch = name.charAt(i);
char ch = name.charAt(i);
switch (ch) {
case '(':
case ')':

View File

@ -181,36 +181,63 @@ public class HtmlTree extends Content {
return s;
}
/**
* A set of ASCII URI characters to be left unencoded.
/*
* The sets of ASCII URI characters to be left unencoded.
* See "Uniform Resource Identifier (URI): Generic Syntax"
* IETF RFC 3986. https://tools.ietf.org/html/rfc3986
*/
public static final BitSet NONENCODING_CHARS = new BitSet(256);
public static final BitSet MAIN_CHARS;
public static final BitSet QUERY_FRAGMENT_CHARS;
static {
// alphabetic characters
for (int i = 'a'; i <= 'z'; i++) {
NONENCODING_CHARS.set(i);
}
for (int i = 'A'; i <= 'Z'; i++) {
NONENCODING_CHARS.set(i);
}
// numeric characters
for (int i = '0'; i <= '9'; i++) {
NONENCODING_CHARS.set(i);
}
// Reserved characters as per RFC 3986. These are set of delimiting characters.
String noEnc = ":/?#[]@!$&'()*+,;=";
// Unreserved characters as per RFC 3986 which should not be percent encoded.
noEnc += "-._~";
for (int i = 0; i < noEnc.length(); i++) {
NONENCODING_CHARS.set(noEnc.charAt(i));
}
BitSet alphaDigit = bitSet(bitSet('A', 'Z'), bitSet('a', 'z'), bitSet('0', '9'));
BitSet unreserved = bitSet(alphaDigit, bitSet("-._~"));
BitSet genDelims = bitSet(":/?#[]@");
BitSet subDelims = bitSet("!$&'()*+,;=");
MAIN_CHARS = bitSet(unreserved, genDelims, subDelims);
BitSet pchar = bitSet(unreserved, subDelims, bitSet(":@"));
QUERY_FRAGMENT_CHARS = bitSet(pchar, bitSet("/?"));
}
private static BitSet bitSet(String s) {
BitSet result = new BitSet();
for (int i = 0; i < s.length(); i++) {
result.set(s.charAt(i));
}
return result;
}
private static BitSet bitSet(char from, char to) {
BitSet result = new BitSet();
result.set(from, to + 1);
return result;
}
private static BitSet bitSet(BitSet... sets) {
BitSet result = new BitSet();
for (BitSet set : sets) {
result.or(set);
}
return result;
}
/**
* Apply percent-encoding to a URL.
* This is similar to {@link java.net.URLEncoder} but
* is less aggressive about encoding some characters,
* like '(', ')', ',' which are used in the anchor
* names for Java methods in HTML5 mode.
*/
private static String encodeURL(String url) {
BitSet nonEncodingChars = MAIN_CHARS;
StringBuilder sb = new StringBuilder();
for (byte c : url.getBytes(Charset.forName("UTF-8"))) {
if (NONENCODING_CHARS.get(c & 0xFF)) {
if (c == '?' || c == '#') {
sb.append((char) c);
// switch to the more restrictive set inside
// the query and/or fragment
nonEncodingChars = QUERY_FRAGMENT_CHARS;
} else if (nonEncodingChars.get(c & 0xFF)) {
sb.append((char) c);
} else {
sb.append(String.format("%%%02X", c & 0xFF));

View File

@ -37,6 +37,8 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.ref.SoftReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.ArrayList;
@ -46,6 +48,7 @@ import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
@ -150,6 +153,9 @@ public abstract class JavadocTester {
/** The output directory used in the most recent call of javadoc. */
protected File outputDir;
/** The output charset used in the most recent call of javadoc. */
protected Charset charset = Charset.defaultCharset();
/** The exit code of the most recent call of javadoc. */
private int exitCode;
@ -158,6 +164,8 @@ public abstract class JavadocTester {
/** A cache of file content, to avoid reading files unnecessarily. */
private final Map<File,SoftReference<String>> fileContentCache = new HashMap<>();
/** The charset used for files in the fileContentCache. */
private Charset fileContentCacheCharset = null;
/** Stream used for logging messages. */
protected final PrintStream out = System.out;
@ -293,13 +301,46 @@ public abstract class JavadocTester {
out.println("Running javadoc (run "
+ javadocRunNum + ")...");
}
outputDir = new File(".");
String charsetArg = null;
String docencodingArg = null;
String encodingArg = null;
for (int i = 0; i < args.length - 2; i++) {
if (args[i].equals("-d")) {
outputDir = new File(args[++i]);
break;
switch (args[i]) {
case "-d":
outputDir = new File(args[++i]);
break;
case "-charset":
charsetArg = args[++i];
break;
case "-docencoding":
docencodingArg = args[++i];
break;
case "-encoding":
encodingArg = args[++i];
break;
}
}
// The following replicates HtmlConfiguration.finishOptionSettings0
// and sets up the charset used to read files.
String cs;
if (docencodingArg == null) {
if (charsetArg == null) {
cs = (encodingArg == null) ? "UTF-8" : encodingArg;
} else {
cs = charsetArg;
}
} else {
cs = docencodingArg;
}
try {
charset = Charset.forName(cs);
} catch (UnsupportedCharsetException e) {
charset = Charset.defaultCharset();
}
out.println("args: " + Arrays.toString(args));
// log.setOutDir(outputDir);
@ -637,6 +678,10 @@ public abstract class JavadocTester {
* @return the file in string format
*/
private String readFile(File baseDir, String fileName) throws Error {
if (!Objects.equals(fileContentCacheCharset, charset)) {
fileContentCache.clear();
fileContentCacheCharset = charset;
}
try {
File file = new File(baseDir, fileName);
SoftReference<String> ref = fileContentCache.get(file);
@ -644,7 +689,8 @@ public abstract class JavadocTester {
if (content != null)
return content;
content = new String(Files.readAllBytes(file.toPath()));
// charset defaults to a value inferred from latest javadoc run
content = new String(Files.readAllBytes(file.toPath()), charset);
fileContentCache.put(file, new SoftReference<>(content));
return content;
} catch (FileNotFoundException e) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2017, 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,29 +23,37 @@
/*
* @test
* @bug 8025633 8025524 8081854
* @bug 8025633 8025524 8081854 8187521
* @summary Test for valid name attribute in HTML anchors.
* @author Bhavesh Patel
* @library ../lib
* @library /tools/lib ../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
* @build JavadocTester
* @build toolbox.ToolBox JavadocTester
* @run main TestAnchorNames
*/
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import toolbox.*;
public class TestAnchorNames extends JavadocTester {
private static final String[] ARGS = new String[] {
};
public static void main(String[] args) throws Exception {
public final ToolBox tb;
public static void main(String... args) throws Exception {
TestAnchorNames tester = new TestAnchorNames();
tester.runTests();
tester.runTests(m -> new Object[] { Paths.get(m.getName()) });
}
public TestAnchorNames() {
tb = new ToolBox();
}
@Test
void test() {
javadoc("-d", "out",
void testHtml4(Path ignore) {
javadoc("-d", "out-html4",
"-sourcepath", testSrc,
"-source", "8", //so that '_' can be used as an identifier
"-use",
@ -153,11 +161,169 @@ public class TestAnchorNames extends JavadocTester {
"<a href=\"#I:Z:Z_\">_");
// The marker name conversion should only affect HTML anchors. It should not
// affect the lables.
// affect the labels.
checkOutput("pkg1/RegClass.html", false,
" Z:Z_",
" Z:Z:Dfield",
" Z:Z_field_In_Class",
" S_:D:D:D:D:DINT");
}
@Test
void testHtml5(Path ignore) {
javadoc("-d", "out-html5",
"-sourcepath", testSrc,
"-source", "8", //so that '_' can be used as an identifier
"-use",
"-html5",
"pkg1");
checkExit(Exit.OK);
// Test some section markers and links to these markers
checkOutput("pkg1/RegClass.html", true,
"<a id=\"skip.navbar.top\">",
"<a href=\"#skip.navbar.top\" title=\"Skip navigation links\">",
"<a id=\"nested.class.summary\">",
"<a href=\"#nested.class.summary\">",
"<a id=\"method.summary\">",
"<a href=\"#method.summary\">",
"<a id=\"field.detail\">",
"<a href=\"#field.detail\">",
"<a id=\"constructor.detail\">",
"<a href=\"#constructor.detail\">");
// Test some members and link to these members
checkOutput("pkg1/RegClass.html", true,
//The marker for this appears in the serialized-form.html which we will
//test below
"<a href=\"../serialized-form.html#pkg1.RegClass\">");
// Test some fields
checkOutput("pkg1/RegClass.html", true,
"<a id=\"_\">",
"<a href=\"../pkg1/RegClass.html#_\">",
"<a id=\"_$\">",
"<a href=\"../pkg1/RegClass.html#_$\">",
"<a id=\"$_\">",
"<a href=\"../pkg1/RegClass.html#$_\">",
"<a id=\"$field\">",
"<a href=\"../pkg1/RegClass.html#$field\">",
"<a id=\"fieldInCla$$\">",
"<a href=\"../pkg1/RegClass.html#fieldInCla$$\">",
"<a id=\"S_$$$$$INT\">",
"<a href=\"../pkg1/RegClass.html#S_$$$$$INT\">",
"<a id=\"method$$\">",
"<a href=\"../pkg1/RegClass.html#method$$\">");
checkOutput("pkg1/DeprMemClass.html", true,
"<a id=\"_field_In_Class\">",
"<a href=\"../pkg1/DeprMemClass.html#_field_In_Class\">");
// Test constructor
checkOutput("pkg1/RegClass.html", true,
"<a id=\"&lt;init&gt;(java.lang.String,int)\">",
"<a href=\"../pkg1/RegClass.html#%3Cinit%3E(java.lang.String,int)\">");
// Test some methods
checkOutput("pkg1/RegClass.html", true,
"<a id=\"_methodInClass(java.lang.String)\">",
"<a href=\"../pkg1/RegClass.html#_methodInClass(java.lang.String)\">",
"<a id=\"method()\">",
"<a href=\"../pkg1/RegClass.html#method()\">",
"<a id=\"foo(java.util.Map)\">",
"<a href=\"../pkg1/RegClass.html#foo(java.util.Map)\">",
"<a id=\"methodInCla$s(java.lang.String[])\">",
"<a href=\"../pkg1/RegClass.html#methodInCla$s(java.lang.String%5B%5D)\">",
"<a id=\"_methodInClas$(java.lang.String,int)\">",
"<a href=\"../pkg1/RegClass.html#_methodInClas$(java.lang.String,int)\">",
"<a id=\"methodD(pkg1.RegClass.$A)\">",
"<a href=\"../pkg1/RegClass.html#methodD(pkg1.RegClass.$A)\">",
"<a id=\"methodD(pkg1.RegClass.D[])\">",
"<a href=\"../pkg1/RegClass.html#methodD(pkg1.RegClass.D%5B%5D)\">");
checkOutput("pkg1/DeprMemClass.html", true,
"<a id=\"$method_In_Class()\">",
"<a href=\"../pkg1/DeprMemClass.html#$method_In_Class()\">");
// Test enum
checkOutput("pkg1/RegClass.Te$t_Enum.html", true,
"<a id=\"$FLD2\">",
"<a href=\"../pkg1/RegClass.Te$t_Enum.html#$FLD2\">");
// Test nested class
checkOutput("pkg1/RegClass._NestedClas$.html", true,
"<a id=\"&lt;init&gt;()\">",
"<a href=\"../pkg1/RegClass._NestedClas$.html#%3Cinit%3E()\">");
// Test class use page
checkOutput("pkg1/class-use/DeprMemClass.html", true,
"<a href=\"../../pkg1/RegClass.html#d____mc\">");
// Test deprecated list page
checkOutput("deprecated-list.html", true,
"<a href=\"pkg1/DeprMemClass.html#_field_In_Class\">",
"<a href=\"pkg1/DeprMemClass.html#$method_In_Class()\">");
// Test constant values page
checkOutput("constant-values.html", true,
"<a href=\"pkg1/RegClass.html#S_$$$$$INT\">");
// Test serialized form page
checkOutput("serialized-form.html", true,
//This is the marker for the link that appears in the pkg1.RegClass.html page
"<a id=\"pkg1.RegClass\">");
// Test member name index page
checkOutput("index-all.html", true,
"<a id=\"I:$\">",
"<a href=\"#I:$\">$",
"<a href=\"#I:_\">_");
}
/**
* The following test is somewhat simplistic, but it is useful
* in conjunction with the W3C Validation Service at https://validator.w3.org/nu/#file
* @param base A working directory for this method, in which some UTF-8 source files
* will be generated
* @throws IOException if there is a problem generating the source files
*/
@Test
void testNonAscii(Path base) throws IOException {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"package p; public class Def {\n"
+ " public int \u00e0\u00e9;\n" // a`e'
+ " public void \u00c0\u00c9() { }\n" // A`E'
+ " public int \u03b1\u03b2\u03b3;\n" // alpha beta gamma
+ " public void \u0391\u0392\u0393() { }\n" // ALPHA BETA GAMMA
+ "}",
"package p; \n"
+ "/**\n"
+ " * {@link Def#\u00e0\u00e9 &agrave;&eacute;}<br>\n"
+ " * {@link Def#\u00c0\u00c9() &Agrave;&Eacute;}<br>\n"
+ " * {@link Def#\u03b1\u03b2\u03b3 &alpha;&beta;&gamma;}<br>\n"
+ " * {@link Def#\u0391\u0392\u0393() &Alpha;&Beta;&Gamma;}<br>\n"
+ " */\n"
+ "public class Ref { }");
javadoc("-d", "out-nonAscii",
"-sourcepath", src.toString(),
"-html5",
"-encoding", "utf-8",
"p");
checkExit(Exit.OK);
checkOutput("p/Def.html", true,
"<a id=\"\u00e0\u00e9\">",
"<a id=\"\u00c0\u00c9()\">",
"<a id=\"\u03b1\u03b2\u03b3\">",
"<a id=\"\u0391\u0392\u0393()\">");
checkOutput("p/Ref.html", true,
"<a href=\"../p/Def.html#%C3%A0%C3%A9\"><code>&agrave;&eacute;</code></a>",
"<a href=\"../p/Def.html#%C3%80%C3%89()\"><code>&Agrave;&Eacute;</code></a>",
"<a href=\"../p/Def.html#%CE%B1%CE%B2%CE%B3\"><code>&alpha;&beta;&gamma;</code></a>",
"<a href=\"../p/Def.html#%CE%91%CE%92%CE%93()\"><code>&Alpha;&Beta;&Gamma;</code></a>");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2017, 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
@ -37,6 +37,8 @@
* @run main TestDocEncoding
*/
import java.nio.charset.Charset;
public class TestDocEncoding extends JavadocTester {
public static void main(String... args) throws Exception {
@ -53,6 +55,13 @@ public class TestDocEncoding extends JavadocTester {
"pkg");
checkExit(Exit.OK);
checkOutput("stylesheet.css", true,
"body {\n"
+ " background-color:#ffffff;");
// reset the charset, for a negative test, that the -docencoding
// was effective and that the output is not in UTF-8.
charset = Charset.forName("UTF-8");
checkOutput("stylesheet.css", false,
"body {\n"
+ " background-color:#ffffff;");

View File

@ -23,7 +23,8 @@
/*
* @test
* @bug 8141492 8071982 8141636 8147890 8166175 8168965 8176794 8175218 8147881 8181622 8182263 8074407
* @bug 8141492 8071982 8141636 8147890 8166175 8168965 8176794 8175218 8147881
* 8181622 8182263 8074407 8187521
* @summary Test the search feature of javadoc.
* @author bpatel
* @library ../lib
@ -64,7 +65,7 @@ public class TestSearch extends JavadocTester {
checkExit(Exit.OK);
checkInvalidUsageIndexTag();
checkSearchOutput(true);
checkSingleIndex(true);
checkSingleIndex(true, false);
checkSingleIndexSearchTagDuplication();
checkJqueryAndImageFiles(true);
checkSearchJS();
@ -86,7 +87,7 @@ public class TestSearch extends JavadocTester {
checkExit(Exit.ERROR);
checkDocLintErrors();
checkSearchOutput(true);
checkSingleIndex(true);
checkSingleIndex(true, false);
checkSingleIndexSearchTagDuplication();
checkJqueryAndImageFiles(true);
checkSearchJS();
@ -128,7 +129,7 @@ public class TestSearch extends JavadocTester {
"-use", "pkg", "pkg1", "pkg2", "pkg3");
checkExit(Exit.OK);
checkSearchOutput(true);
checkSingleIndex(true);
checkSingleIndex(true, true);
checkSingleIndexSearchTagDuplication();
checkJqueryAndImageFiles(true);
checkSearchJS();
@ -280,7 +281,9 @@ public class TestSearch extends JavadocTester {
"<div class=\"fixedNav\">");
}
void checkSingleIndex(boolean expectedOutput) {
void checkSingleIndex(boolean expectedOutput, boolean html5) {
String html_span_see_span = html5 ? "html%3Cspan%3Esee%3C/span%3E" : "html-span-see-/span-";
// Test for search tags markup in index file.
checkOutput("index-all.html", expectedOutput,
"<dt><span class=\"searchTagLink\"><a href=\"pkg/package-summary.html#phrasewithspaces\">"
@ -313,7 +316,7 @@ public class TestSearch extends JavadocTester {
+ "#nested%7B@indexnested_tag_test%7D\">nested {@index nested_tag_test}</a></span> - "
+ "Search tag in pkg.AnotherClass.ModalExclusionType.NO_EXCLUDE</dt>",
"<dt><span class=\"searchTagLink\"><a href=\"pkg/AnotherClass.ModalExclusionType.html"
+ "#html-span-see-/span-\">html &lt;span&gt; see &lt;/span&gt;</a></span> - Search "
+ "#" + html_span_see_span + "\">html &lt;span&gt; see &lt;/span&gt;</a></span> - Search "
+ "tag in pkg.AnotherClass.ModalExclusionType.APPLICATION_EXCLUDE</dt>",
"<dt><span class=\"searchTagLink\"><a href=\"pkg/AnotherClass.html#quoted\">quoted</a>"
+ "</span> - Search tag in pkg.AnotherClass.CONSTANT1</dt>",