1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
|
/*
* Copyright (c) 2022, 2023, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6934301
* @library /tools/lib ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
* @build toolbox.ToolBox javadoc.tester.*
* @run main TestDirectedInheritance
*/
import java.nio.file.Path;
import javadoc.tester.JavadocTester;
import toolbox.ToolBox;
public class TestDirectedInheritance extends JavadocTester {
public static void main(String... args) throws Exception {
new TestDirectedInheritance().runTests(m -> new Object[]{Path.of(m.getName())});
}
private final ToolBox tb = new ToolBox();
/*
* Javadoc won't crash if an unknown tag uses {@inheritDoc}.
*/
@Test
public void testUnknownTag(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public interface I1 {
/** @foo bar */
void m();
}
""", """
package x;
public interface E1 extends I1 {
/** @foo {@inheritDoc} */
void m();
}
""", """
package x;
public interface E2 extends I1 {
/** @foo {@inheritDoc I1} */
void m();
}
""");
// DocLint should neither prevent nor cause a crash. Explicitly check that
// there's no crash with DocLint on and off, but don't check that the exit
// code is OK, it likely isn't (after all, there's an unknown tag).
setAutomaticCheckNoStacktrace(true);
{ // DocLint is explicit
int i = 0;
for (var check : new String[]{":all", ":none", ""}) {
var outputDir = "out-DocLint-" + i++; // use separate output directories
javadoc("-Xdoclint" + check,
"-d", base.resolve(outputDir).toString(),
"--source-path", src.toString(),
"x");
}
}
// DocLint is default
javadoc("-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
}
/*
* An interface method inherits documentation from that interface's rightmost
* superinterface in the `extends` clause.
*/
@Test
public void testInterfaceInheritsFromSuperinterface(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public interface I1 {
/**
* I1: main description
*
* @param <A> I1: first type parameter
* @param <B> I1: second type parameter
*
* @param bObj I1: parameter
* @return I1: return
*
* @throws B I1: first description of an exception
* @throws B I1: second description of an exception
*/
<A, B extends RuntimeException> int m(A bObj);
}
""", """
package x;
public interface I2 {
/**
* I2: main description
*
* @param <C> I2: first type parameter
* @param <D> I2: second type parameter
*
* @param cObj I2: parameter
* @return I2: return
*
* @throws D I2: first description of an exception
* @throws D I2: second description of an exception
*/
<C, D extends RuntimeException> int m(C cObj);
}
""", """
package x;
public interface E1 extends I1, I2 {
/**
* {@inheritDoc I2}
*
* @param <E> {@inheritDoc I2}
* @param <F> {@inheritDoc I2}
*
* @param eObj {@inheritDoc I2}
* @return {@inheritDoc I2}
*
* @throws F {@inheritDoc I2}
*/
<E, F extends RuntimeException> int m(E eObj);
}
""");
javadoc("-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.OK);
new OutputChecker("x/E1.html").check("""
<div class="block">I2: main description</div>
""", """
<dt>Type Parameters:</dt>
<dd><code>E</code> - I2: first type parameter</dd>
<dd><code>F</code> - I2: second type parameter</dd>
<dt>Parameters:</dt>
<dd><code>eObj</code> - I2: parameter</dd>
<dt>Returns:</dt>
<dd>I2: return</dd>
<dt>Throws:</dt>
<dd><code>F</code> - I2: first description of an exception</dd>
<dd><code>F</code> - I2: second description of an exception</dd>
</dl>""");
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
/*
* An interface method both provides and inherits the main description and
* the exception documentation from all its superinterfaces.
*
* Note: the same does not work for @param and @return as these are one-to-one.
*/
@Test
public void testInterfaceInheritsFromAllSuperinterfaces(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public interface I1 {
/**
* I1: main description
*
* @throws B I1: first description of an exception
* @throws B I1: second description of an exception
*/
<A, B extends RuntimeException> int m(A bObj);
}
""", """
package x;
public interface I2 {
/**
* I2: main description
*
* @throws D I2: first description of an exception
* @throws D I2: second description of an exception
*/
<C, D extends RuntimeException> int m(C cObj);
}
""", """
package x;
public interface E1 extends I1, I2 {
/**
* E1: main description
* {@inheritDoc I2}
* {@inheritDoc I1}
*
* @throws F E1: description of an exception
* @throws F {@inheritDoc I2}
* @throws F {@inheritDoc I1}
*/
<E, F extends RuntimeException> int m(E eObj);
}
""");
javadoc("-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.OK);
new OutputChecker("x/E1.html").check("""
<div class="block">E1: main description
I2: main description
I1: main description</div>""", """
<dt>Throws:</dt>
<dd><code>F</code> - E1: description of an exception</dd>
<dd><code>F</code> - I2: first description of an exception</dd>
<dd><code>F</code> - I2: second description of an exception</dd>
<dd><code>F</code> - I1: first description of an exception</dd>
<dd><code>F</code> - I1: second description of an exception</dd>
</dl>""");
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
/*
* C1.m directedly inherits documentation from B1, which inherits A.m
* along with its documentation.
*
* C2.m directedly inherits documentation from B2, whose m overrides A.m
* and implicitly inherits its documentation.
*/
@Test
public void testRecursiveInheritance1(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public class A {
/** A.m() */
public void m() { }
}
""", """
package x;
public class B1 extends A { }
""", """
package x;
public class C1 extends B1 {
/** {@inheritDoc B1} */
@Override public void m() { }
}
""", """
package x;
public class B2 extends A {
@Override public void m() { }
}
""", """
package x;
public class C2 extends B2 {
/** {@inheritDoc B2} */
@Override public void m() { }
}
""");
javadoc("-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.OK);
var m = """
<section class="detail" id="m()">
<h3>m</h3>
<div class="horizontal-scroll">
<div class="member-signature"><span class="modifiers">\
public</span> <span class="return-type">void</span>\
<span class="element-name">m</span>()</div>
<div class="block">A.m()</div>""";
new OutputChecker("x/C1.html").check(m);
new OutputChecker("x/C2.html").check(m);
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
/*
* C1.m directedly inherits documentation from B1, which in turn inherits
* it undirectedly from A.
*
* C2.m directedly inherits documentation from B2, which in turn inherits
* in directedly from A.
*/
@Test
public void testRecursiveInheritance2(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public class A {
/** A.m() */
public void m() { }
}
""", """
package x;
public class B1 extends A {
/** {@inheritDoc} */
@Override public void m() { }
}
""", """
package x;
public class C1 extends B1 {
/** {@inheritDoc B1} */
@Override
public void m() { }
}
""", """
package x;
public class B2 extends A {
/** {@inheritDoc A} */
@Override public void m() { }
}
""", """
package x;
public class C2 extends B2 {
/** {@inheritDoc B2} */
@Override public void m() { }
}
""");
javadoc("-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.OK);
var m = """
<section class="detail" id="m()">
<h3>m</h3>
<div class="horizontal-scroll">
<div class="member-signature"><span class="modifiers">\
public</span> <span class="return-type">void</span>\
<span class="element-name">m</span>()</div>
<div class="block">A.m()</div>""";
new OutputChecker("x/C1.html").check(m);
new OutputChecker("x/C2.html").check(m);
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
/*
* Currently, there's no special error for a documentation comment that inherits
* from itself. Instead, such a comment is seen as a general case of a comment
* that inherits from a documentation of a method which that comment's method
* does not override (JLS says that a method does not override itself).
*
* TODO: DocLint might not always be able to find another type, but it
* should always be capable of detecting the same type; we could
* consider implementing this check _also_ in DocLint
*/
@Test
public void testSelfInheritance(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public class A {
/** {@inheritDoc A} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class B {
/** @param i {@inheritDoc B} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class C {
/** @param <T> {@inheritDoc C} */
public <T> Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class D {
/** @return {@inheritDoc D} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class E {
/** @throws NullPointerException {@inheritDoc E} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class F {
/** @throws T NullPointerException {@inheritDoc F} */
public <T extends RuntimeException> Integer minus(Integer i) { return -i; }
}
""");
javadoc("-Xdoclint:none", // turn off DocLint
"-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.ERROR);
new OutputChecker(Output.OUT).setExpectOrdered(false).check("""
A.java:3: error: cannot find the overridden method
/** {@inheritDoc A} */
^""", """
B.java:3: error: cannot find the overridden method
/** @param i {@inheritDoc B} */
^""", """
C.java:3: error: cannot find the overridden method
/** @param <T> {@inheritDoc C} */
^""", """
D.java:3: error: cannot find the overridden method
/** @return {@inheritDoc D} */
^""", """
E.java:3: error: cannot find the overridden method
/** @throws NullPointerException {@inheritDoc E} */
^""", """
F.java:3: error: cannot find the overridden method
/** @throws T NullPointerException {@inheritDoc F} */
^""");
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
/*
* While E1.m and I.m have override-equivalent signatures, E1 does not extend I.
* While E2 extends I, E2.m1 does not override I.m. In either case, there's no
* (overridden) method to inherit documentation from.
*/
@Test
public void testInvalidSupertype1(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public interface I {
/**
* I: main description
*
* @param <A> I: first type parameter
* @param <B> I: second type parameter
*
* @param bObj I: parameter
* @return I: return
*
* @throws B I: first description of an exception
* @throws B I: second description of an exception
*/
<A, B extends RuntimeException> int m(A bObj);
}
""", """
package x;
public interface E1 {
/**
* {@inheritDoc I}
*
* @param <C> {@inheritDoc I}
* @param <D> {@inheritDoc I}
*
* @param cObj {@inheritDoc I}
* @return {@inheritDoc I}
*
* @throws D {@inheritDoc I}
*/
<C, D extends RuntimeException> int m(C cObj);
}
""", """
package x;
public interface E2 extends I {
/**
* {@inheritDoc I}
*
* @param <E> {@inheritDoc I}
* @param <F> {@inheritDoc I}
*
* @param eObj {@inheritDoc I}
* @return {@inheritDoc I}
*
* @throws F {@inheritDoc I}
*/
<E, F extends RuntimeException> int m1(E eObj);
}
""");
javadoc("-Xdoclint:none", // turn off DocLint
"-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.ERROR);
new OutputChecker(Output.OUT).setExpectOrdered(false).check("""
E1.java:4: error: cannot find the overridden method
* {@inheritDoc I}
^""", """
E1.java:6: error: cannot find the overridden method
* @param <C> {@inheritDoc I}
^""", """
E1.java:7: error: cannot find the overridden method
* @param <D> {@inheritDoc I}
^""", """
E1.java:9: error: cannot find the overridden method
* @param cObj {@inheritDoc I}
^""", """
E1.java:10: error: cannot find the overridden method
* @return {@inheritDoc I}
^""", """
E1.java:12: error: cannot find the overridden method
* @throws D {@inheritDoc I}
^""");
new OutputChecker(Output.OUT).setExpectOrdered(false).check("""
E2.java:4: error: cannot find the overridden method
* {@inheritDoc I}
^""", """
E2.java:6: error: cannot find the overridden method
* @param <E> {@inheritDoc I}
^""", """
E2.java:7: error: cannot find the overridden method
* @param <F> {@inheritDoc I}
^""", """
E2.java:9: error: cannot find the overridden method
* @param eObj {@inheritDoc I}
^""", """
E2.java:10: error: cannot find the overridden method
* @return {@inheritDoc I}
^""", """
E2.java:12: error: cannot find the overridden method
* @throws F {@inheritDoc I}
^""");
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
/*
* Cannot inherit documentation from a subtype.
*/
@Test
public void testInvalidSupertype2(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public interface E extends I {
/**
* E: main description
*
* @param <A> E: first type parameter
* @param <B> E: second type parameter
*
* @param aObj E: parameter
* @return E: return
*
* @throws B E: first description of an exception
* @throws B E: second description of an exception
*/
<A, B extends RuntimeException> int m(A aObj);
}
""", """
package x;
public interface I {
/**
* {@inheritDoc E}
*
* @param <C> {@inheritDoc E}
* @param <D> {@inheritDoc E}
*
* @param cObj {@inheritDoc E}
* @return {@inheritDoc E}
*
* @throws D {@inheritDoc E}
*/
<C, D extends RuntimeException> int m(C cObj);
}
""");
javadoc("-Xdoclint:none", // turn off DocLint
"-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.ERROR);
new OutputChecker(Output.OUT).setExpectOrdered(false).check("""
I.java:4: error: cannot find the overridden method
* {@inheritDoc E}
^""", """
I.java:6: error: cannot find the overridden method
* @param <C> {@inheritDoc E}
^""", """
I.java:7: error: cannot find the overridden method
* @param <D> {@inheritDoc E}
^""", """
I.java:9: error: cannot find the overridden method
* @param cObj {@inheritDoc E}
^""", """
I.java:10: error: cannot find the overridden method
* @return {@inheritDoc E}
^""", """
I.java:12: error: cannot find the overridden method
* @throws D {@inheritDoc E}
^""");
}
@Test
public void testUnknownSupertype(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
package x;
public class A {
/** {@inheritDoc MySuperType} */
public Integer m(Integer i) { return -i; }
}
""", """
package x;
public class B {
/** @param i {@inheritDoc MySuperType} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class C {
/** @param <T> {@inheritDoc MySuperType} */
public <T> Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class D {
/** @return {@inheritDoc MySuperType} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class E {
/** @throws NullPointerException {@inheritDoc MySuperType} */
public Integer minus(Integer i) { return -i; }
}
""", """
package x;
public class F {
/** @throws T NullPointerException {@inheritDoc MySuperType} */
public <T extends RuntimeException> Integer minus(Integer i) { return -i; }
}
""");
javadoc("-Xdoclint:none", // turn off DocLint
"-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"x");
checkExit(Exit.ERROR);
new OutputChecker(Output.OUT).setExpectOrdered(false).check("""
A.java:3: error: cannot find the overridden method
/** {@inheritDoc MySuperType} */
^""", """
B.java:3: error: cannot find the overridden method
/** @param i {@inheritDoc MySuperType} */
^""", """
C.java:3: error: cannot find the overridden method
/** @param <T> {@inheritDoc MySuperType} */
^""", """
D.java:3: error: cannot find the overridden method
/** @return {@inheritDoc MySuperType} */
^""", """
E.java:3: error: cannot find the overridden method
/** @throws NullPointerException {@inheritDoc MySuperType} */
^""", """
F.java:3: error: cannot find the overridden method
/** @throws T NullPointerException {@inheritDoc MySuperType} */
^""");
new OutputChecker(Output.OUT).setExpectFound(false)
.check("warning: not a direct supertype"); // no unexpected warnings
}
}
|