Skip to content

Commit 9755782

Browse files
jonathan-gibbonsYano, Masanori
andcommitted
8157682: @inheritdoc doesn't work with @exception
Co-authored-by: Yano, Masanori <yano-masanori@jp.fujitsu.com> Co-authored-by: Jonathan Gibbons <jjg@openjdk.org> Reviewed-by: prappo
1 parent 8c13d26 commit 9755782

File tree

2 files changed

+133
-18
lines changed

2 files changed

+133
-18
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.io.PrintStream;
3131
import java.util.ArrayList;
3232
import java.util.Collections;
33-
import java.util.Comparator;
3433
import java.util.EnumMap;
3534
import java.util.EnumSet;
3635
import java.util.HashSet;
@@ -40,7 +39,7 @@
4039
import java.util.Map;
4140
import java.util.ServiceLoader;
4241
import java.util.Set;
43-
import java.util.TreeSet;
42+
import java.util.TreeMap;
4443

4544
import javax.lang.model.element.Element;
4645
import javax.lang.model.element.ExecutableElement;
@@ -98,7 +97,12 @@ public class TagletManager {
9897
public static final char SIMPLE_TAGLET_OPT_SEPARATOR = ':';
9998

10099
/**
101-
* All taglets, keyed by their {@link Taglet#getName() name}.
100+
* All taglets, keyed either by their {@link Taglet#getName() name},
101+
* or by an alias.
102+
*
103+
* In general, taglets do <i>not</i> provide aliases;
104+
* the one instance that does is {@code ThrowsTaglet}, which handles
105+
* both {@code @throws} tags and {@code @exception} tags.
102106
*/
103107
private final LinkedHashMap<String, Taglet> allTaglets;
104108

@@ -577,17 +581,15 @@ private void initTaglets() {
577581

578582
inlineTags = new LinkedHashMap<>();
579583

580-
for (Taglet t : allTaglets.values()) {
584+
allTaglets.forEach((name, t) -> {
581585
if (t.isInlineTag()) {
582586
inlineTags.put(t.getName(), t);
583587
}
584588

585-
if (t.isBlockTag()) {
586-
for (Location l : t.getAllowedLocations()) {
587-
blockTagletsByLocation.get(l).add(t);
588-
}
589+
if (t.isBlockTag() && t.getName().equals(name)) {
590+
t.getAllowedLocations().forEach(l -> blockTagletsByLocation.get(l).add(t));
589591
}
590-
}
592+
});
591593

592594
// init the serialized form tags for the serialized form page
593595
serializedFormTags = new ArrayList<>();
@@ -612,10 +614,7 @@ private void initStandardTaglets() {
612614

613615
addStandardTaglet(new ParamTaglet());
614616
addStandardTaglet(new ReturnTaglet());
615-
addStandardTaglet(new ThrowsTaglet());
616-
addStandardTaglet(
617-
new SimpleTaglet(EXCEPTION, null,
618-
EnumSet.of(Location.METHOD, Location.CONSTRUCTOR)));
617+
addStandardTaglet(new ThrowsTaglet(), EXCEPTION);
619618
addStandardTaglet(
620619
new SimpleTaglet(SINCE, resources.getText("doclet.Since"),
621620
EnumSet.allOf(Location.class), !nosince));
@@ -683,6 +682,14 @@ private void addStandardTaglet(Taglet taglet) {
683682
standardTagsLowercase.add(Utils.toLowerCase(name));
684683
}
685684

685+
private void addStandardTaglet(Taglet taglet, DocTree.Kind alias) {
686+
addStandardTaglet(taglet);
687+
String name = alias.tagName;
688+
allTaglets.put(name, taglet);
689+
standardTags.add(name);
690+
standardTagsLowercase.add(Utils.toLowerCase(name));
691+
}
692+
686693
public boolean isKnownCustomTag(String tagName) {
687694
return allTaglets.containsKey(tagName);
688695
}
@@ -729,12 +736,11 @@ Taglet getTaglet(String name) {
729736
* a need for a corresponding update to the spec.
730737
*/
731738
private void showTaglets(PrintStream out) {
732-
Set<Taglet> taglets = new TreeSet<>(Comparator.comparing(Taglet::getName));
733-
taglets.addAll(allTaglets.values());
739+
Map<String, Taglet> taglets = new TreeMap<>(allTaglets);
734740

735-
for (Taglet t : taglets) {
741+
taglets.forEach((n, t) -> {
736742
// give preference to simpler block form if a tag can be either
737-
String name = t.isBlockTag() ? "@" + t.getName() : "{@" + t.getName() + "}";
743+
String name = t.isBlockTag() ? "@" + n : "{@" + n + "}";
738744
out.println(String.format("%20s", name) + ": "
739745
+ format(t.isBlockTag(), "block")+ " "
740746
+ format(t.inOverview(), "overview") + " "
@@ -746,7 +752,7 @@ private void showTaglets(PrintStream out) {
746752
+ format(t.inField(), "field") + " "
747753
+ format(t.isInlineTag(), "inline")+ " "
748754
+ format((t instanceof SimpleTaglet) && !((SimpleTaglet) t).enabled, "disabled"));
749-
}
755+
});
750756
}
751757

752758
private String format(boolean b, String s) {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8157682
27+
* @summary at-inheritDoc doesn't work with at-exception
28+
* @library /tools/lib ../../lib
29+
* @modules jdk.javadoc/jdk.javadoc.internal.tool
30+
* @build toolbox.ToolBox javadoc.tester.*
31+
* @run main TestExceptionInheritance
32+
*/
33+
34+
import java.nio.file.Path;
35+
36+
import javadoc.tester.JavadocTester;
37+
38+
import toolbox.ToolBox;
39+
40+
public class TestExceptionInheritance extends JavadocTester {
41+
42+
public static void main(String... args) throws Exception {
43+
TestExceptionInheritance tester = new TestExceptionInheritance();
44+
tester.runTests(m -> new Object[] { Path.of(m.getName()) });
45+
}
46+
47+
ToolBox tb = new ToolBox();
48+
49+
@Test
50+
public void testExceptionException(Path base) throws Exception {
51+
test(base, "exception", "exception");
52+
}
53+
54+
@Test
55+
public void testExceptionThrows(Path base) throws Exception {
56+
test(base, "exception", "throws");
57+
}
58+
59+
@Test
60+
public void testThrowsException(Path base) throws Exception {
61+
test(base, "throws", "exception");
62+
}
63+
64+
@Test
65+
public void testThrowsThrows(Path base) throws Exception {
66+
test(base, "throws", "throws");
67+
}
68+
69+
void test(Path base, String a, String b) throws Exception {
70+
Path src = base.resolve("src");
71+
tb.writeJavaFiles(src,
72+
"""
73+
package p;
74+
public class A {
75+
/**
76+
* @param x a number
77+
* @##A## NullPointerException if x is null
78+
* @##A## IllegalArgumentException if {@code x < 0}
79+
*/
80+
public void m(Integer x) { }
81+
}
82+
""".replace("##A##", a),
83+
"""
84+
package p;
85+
public class A_Sub extends A {
86+
/**
87+
* @param x {@inheritDoc}
88+
* @##B## NullPointerException {@inheritDoc}
89+
* @##B## IllegalArgumentException {@inheritDoc}
90+
*/
91+
@Override
92+
public void m(Integer x) { }
93+
}
94+
""".replace("##B##", b)
95+
);
96+
97+
javadoc("-d", base.resolve("out").toString(),
98+
"-sourcepath", src.toString(),
99+
"--no-platform-links",
100+
"p");
101+
checkExit(Exit.OK);
102+
103+
checkOutput("p/A_Sub.html", true,
104+
"<code>java.lang.NullPointerException</code> - if x is null");
105+
106+
checkOutput("p/A_Sub.html", true,
107+
"<code>java.lang.IllegalArgumentException</code> - if <code>x &lt; 0</code>");
108+
}
109+
}

0 commit comments

Comments
 (0)