|
44 | 44 | import org.eclipse.jdt.core.IField; |
45 | 45 | import org.eclipse.jdt.core.IJavaElement; |
46 | 46 | import org.eclipse.jdt.core.IJavaProject; |
| 47 | +import org.eclipse.jdt.core.IMethod; |
47 | 48 | import org.eclipse.jdt.core.ISourceRange; |
48 | 49 | import org.eclipse.jdt.core.ISourceReference; |
49 | 50 | import org.eclipse.jdt.core.IType; |
@@ -370,6 +371,99 @@ record FooBar(String foo, String bar) {} |
370 | 371 | } |
371 | 372 | } |
372 | 373 |
|
| 374 | + @Test |
| 375 | + public void testRecordConstructor1() throws Exception { |
| 376 | + // https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/931 |
| 377 | + String source= |
| 378 | + """ |
| 379 | + package p; |
| 380 | + public class X {} |
| 381 | + /** |
| 382 | + * A foo bar. |
| 383 | + * @param foo The foo. |
| 384 | + * @param bar The bar. |
| 385 | + */ |
| 386 | + record FooBar(String foo, String bar) { |
| 387 | + public FooBar { |
| 388 | + if (foo == null) { |
| 389 | + foo = "abc"; |
| 390 | + } |
| 391 | + } |
| 392 | + } |
| 393 | + """; |
| 394 | + ICompilationUnit cu= getWorkingCopy("/TestSetupProject/src/p/X.java", source, null); |
| 395 | + assertNotNull("TestClass.java", cu); |
| 396 | + |
| 397 | + IType recordType= cu.getType("FooBar"); |
| 398 | + boolean found= false; |
| 399 | + for (IJavaElement member : recordType.getChildren()) { |
| 400 | + if (member.getElementName().equals("FooBar")) { |
| 401 | + assertTrue("not a constructor", member instanceof IMethod method && method.isConstructor()); |
| 402 | + found= true; |
| 403 | + IJavaElement[] elements= { member }; |
| 404 | + ISourceRange range= ((ISourceReference) member).getNameRange(); |
| 405 | + // copy logic from JavadocHover |
| 406 | + if (elements.length == 1 && elements[0] instanceof IMethod method && method.isConstructor() |
| 407 | + && method.getJavadocRange() == null && method.getParent() instanceof IType type && type.isRecord()) { |
| 408 | + elements[0]= method.getParent(); |
| 409 | + } |
| 410 | + JavadocBrowserInformationControlInput hoverInfo= JavadocHover.getHoverInfo(elements, cu, new Region(range.getOffset(), range.getLength()), null); |
| 411 | + String actualHtmlContent= hoverInfo.getHtml(); |
| 412 | + int index= actualHtmlContent.indexOf("A foo bar."); |
| 413 | + assertNotEquals("Expected HTML not found, instead found : " + actualHtmlContent, -1, index); |
| 414 | + } |
| 415 | + } |
| 416 | + assertTrue("constructor not found", found); |
| 417 | + } |
| 418 | + |
| 419 | + @Test |
| 420 | + public void testRecordConstructor2() throws Exception { |
| 421 | + // https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/931 |
| 422 | + String source= |
| 423 | + """ |
| 424 | + package p; |
| 425 | + public class X {} |
| 426 | + /** |
| 427 | + * A foo bar. |
| 428 | + * @param foo The foo. |
| 429 | + * @param bar The bar. |
| 430 | + */ |
| 431 | + record FooBar(String foo, String bar) { |
| 432 | + /** |
| 433 | + * Foobar constructor |
| 434 | + */ |
| 435 | + public FooBar { |
| 436 | + if (foo == null) { |
| 437 | + foo = "abc"; |
| 438 | + } |
| 439 | + } |
| 440 | + } |
| 441 | + """; |
| 442 | + ICompilationUnit cu= getWorkingCopy("/TestSetupProject/src/p/X.java", source, null); |
| 443 | + assertNotNull("TestClass.java", cu); |
| 444 | + |
| 445 | + IType recordType= cu.getType("FooBar"); |
| 446 | + boolean found= false; |
| 447 | + for (IJavaElement member : recordType.getChildren()) { |
| 448 | + if (member.getElementName().equals("FooBar")) { |
| 449 | + assertTrue("not a constructor", member instanceof IMethod method && method.isConstructor()); |
| 450 | + found= true; |
| 451 | + IJavaElement[] elements= { member }; |
| 452 | + ISourceRange range= ((ISourceReference) member).getNameRange(); |
| 453 | + // copy logic from JavadocHover |
| 454 | + if (elements.length == 1 && elements[0] instanceof IMethod method && method.isConstructor() |
| 455 | + && method.getJavadocRange() == null && method.getParent() instanceof IType type && type.isRecord()) { |
| 456 | + elements[0]= method.getParent(); |
| 457 | + } |
| 458 | + JavadocBrowserInformationControlInput hoverInfo= JavadocHover.getHoverInfo(elements, cu, new Region(range.getOffset(), range.getLength()), null); |
| 459 | + String actualHtmlContent= hoverInfo.getHtml(); |
| 460 | + int index= actualHtmlContent.indexOf("Foobar constructor"); |
| 461 | + assertNotEquals("Expected HTML not found, instead found : " + actualHtmlContent, -1, index); |
| 462 | + } |
| 463 | + } |
| 464 | + assertTrue("constructor not found", found); |
| 465 | + } |
| 466 | + |
373 | 467 | @Test |
374 | 468 | public void testLinkTagWithHttp_01() throws Exception { |
375 | 469 | String source= |
|
0 commit comments