Skip to content

Commit 94f5e44

Browse files
8271258: @param with non-ascii variable names produces incorrect results
Reviewed-by: hannesw
1 parent 7454306 commit 94f5e44

File tree

3 files changed

+86
-30
lines changed

3 files changed

+86
-30
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/ParamTaglet.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -67,10 +67,9 @@ public ParamTaglet() {
6767
}
6868

6969
/**
70-
* Given an array of <code>Parameter</code>s, return
71-
* a name/rank number map. If the array is null, then
72-
* null is returned.
73-
* @param params The array of parameters (from type or executable member) to
70+
* Given a list of parameters, return a name/rank number map.
71+
* If the list is null, then null is returned.
72+
* @param params The list of parameters (from type or executable member) to
7473
* check.
7574
* @return a name-rank number map.
7675
*/
@@ -245,28 +244,24 @@ private Content processParamTags(Element e, ParamKind kind,
245244
CommentHelper ch = writer.configuration().utils.getCommentHelper(e);
246245
for (ParamTree dt : paramTags) {
247246
String name = ch.getParameterName(dt);
248-
String paramName = kind != ParamKind.TYPE_PARAMETER
249-
? name.toString()
250-
: "<" + name + ">";
247+
String paramName = kind == ParamKind.TYPE_PARAMETER ? "<" + name + ">" : name;
251248
if (!rankMap.containsKey(name)) {
252-
String key;
253-
switch (kind) {
254-
case PARAMETER: key = "doclet.Parameters_warn" ; break;
255-
case TYPE_PARAMETER: key = "doclet.TypeParameters_warn" ; break;
256-
case RECORD_COMPONENT: key = "doclet.RecordComponents_warn" ; break;
257-
default: throw new IllegalArgumentException(kind.toString());
258-
}
249+
String key = switch (kind) {
250+
case PARAMETER -> "doclet.Parameters_warn";
251+
case TYPE_PARAMETER -> "doclet.TypeParameters_warn";
252+
case RECORD_COMPONENT -> "doclet.RecordComponents_warn";
253+
default -> throw new IllegalArgumentException(kind.toString());
254+
};
259255
messages.warning(ch.getDocTreePath(dt), key, paramName);
260256
}
261257
String rank = rankMap.get(name);
262258
if (rank != null && alreadyDocumented.contains(rank)) {
263-
String key;
264-
switch (kind) {
265-
case PARAMETER: key = "doclet.Parameters_dup_warn" ; break;
266-
case TYPE_PARAMETER: key = "doclet.TypeParameters_dup_warn" ; break;
267-
case RECORD_COMPONENT: key = "doclet.RecordComponents_dup_warn" ; break;
268-
default: throw new IllegalArgumentException(kind.toString());
269-
}
259+
String key = switch (kind) {
260+
case PARAMETER -> "doclet.Parameters_dup_warn";
261+
case TYPE_PARAMETER -> "doclet.TypeParameters_dup_warn";
262+
case RECORD_COMPONENT -> "doclet.RecordComponents_dup_warn";
263+
default -> throw new IllegalArgumentException(kind.toString());
264+
};
270265
messages.warning(ch.getDocTreePath(dt), key, paramName);
271266
}
272267
result.add(processParamTag(e, kind, writer, dt,

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/CommentHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public boolean isTypeParameter(DocTree dtree) {
139139

140140
public String getParameterName(DocTree dtree) {
141141
if (dtree.getKind() == PARAM) {
142-
return ((ParamTree) dtree).getName().toString();
142+
return ((ParamTree) dtree).getName().getName().toString();
143143
} else {
144144
return null;
145145
}

test/langtools/jdk/javadoc/doclet/testUnicode/TestUnicode.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8203176
26+
* @bug 8203176 8271258
2727
* @summary javadoc handles non-ASCII characters incorrectly
2828
* @library /tools/lib ../../lib
2929
* @modules jdk.javadoc/jdk.javadoc.internal.tool
@@ -33,7 +33,6 @@
3333

3434
import java.nio.file.Files;
3535
import java.nio.file.Path;
36-
import java.nio.file.Paths;
3736

3837
import javadoc.tester.JavadocTester;
3938
import toolbox.ToolBox;
@@ -42,20 +41,20 @@ public class TestUnicode extends JavadocTester {
4241

4342
public static void main(String... args) throws Exception {
4443
TestUnicode tester = new TestUnicode();
45-
tester.runTests();
44+
tester.runTests(m -> new Object[] { Path.of(m.getName())});
4645
}
4746

4847
ToolBox tb = new ToolBox();
4948

5049
@Test
51-
public void test() throws Exception {
50+
public void testUnicode(Path base) throws Exception {
5251
char ellipsis = '\u2026';
53-
Path src = Files.createDirectories(Paths.get("src"));
52+
Path src = Files.createDirectories(base.resolve("src"));
5453
tb.writeJavaFiles(src,
5554
"/** Hel" + ellipsis + "lo {@code World(" + ellipsis + ")}. */\n"
5655
+ "public class Code { }\n");
5756

58-
javadoc("-d", "out",
57+
javadoc("-d", base.resolve("out").toString(),
5958
"-encoding", "utf-8",
6059
src.resolve("Code.java").toString());
6160
checkExit(Exit.OK);
@@ -65,4 +64,66 @@ public void test() throws Exception {
6564
checkOutput("Code.html", false,
6665
"\\u");
6766
}
67+
68+
@Test
69+
public void testParam(Path base) throws Exception {
70+
String chineseElephant = "\u5927\u8c61"; // taken from JDK-8271258
71+
Path src = Files.createDirectories(base.resolve("src"));
72+
tb.writeJavaFiles(src,
73+
"""
74+
/**
75+
* Comment. ##.
76+
* @param <##> the ##
77+
*/
78+
public class Code<##> {
79+
/**
80+
* Comment. ##.
81+
* @param ## the ##
82+
*/
83+
public void set##(int ##) { }
84+
}""".replaceAll("##", chineseElephant));
85+
86+
javadoc("-d", base.resolve("out").toString(),
87+
"-encoding", "utf-8",
88+
"--no-platform-links",
89+
src.resolve("Code.java").toString());
90+
checkExit(Exit.OK);
91+
92+
checkOutput("Code.html", true,
93+
"""
94+
<h1 title="Class Code" class="title">Class Code&lt;##&gt;</h1>
95+
""".replaceAll("##", chineseElephant),
96+
"""
97+
<div class="inheritance" title="Inheritance Tree">java.lang.Object
98+
<div class="inheritance">Code&lt;##&gt;</div>
99+
</div>
100+
""".replaceAll("##", chineseElephant),
101+
"""
102+
<dl class="notes">
103+
<dt>Type Parameters:</dt>
104+
<dd><code>##</code> - the ##</dd>
105+
</dl>
106+
""".replaceAll("##", chineseElephant),
107+
"""
108+
<section class="detail" id="set##(int)">
109+
<h3>set##</h3>
110+
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span c\
111+
lass="return-type">void</span>&nbsp;<span class="element-name">set##</span><wbr>\
112+
<span class="parameters">(int&nbsp;##)</span></div>
113+
<div class="block">Comment. ##.</div>
114+
<dl class="notes">
115+
<dt>Parameters:</dt>
116+
<dd><code>##</code> - the ##</dd>
117+
</dl>
118+
</section>
119+
""".replaceAll("##", chineseElephant)
120+
);
121+
122+
// The following checks for the numeric forms of the Unicode characters being tested:
123+
// these numeric forms should not show up as literal character sequences.
124+
checkOutput("Code.html", false,
125+
Integer.toHexString(chineseElephant.charAt(0)),
126+
Integer.toHexString(chineseElephant.charAt(1))
127+
);
128+
}
68129
}

0 commit comments

Comments
 (0)