Bug 570137 - [compiler] Javadoc inline tags with nested braces parsed incorrectly
Summary: [compiler] Javadoc inline tags with nested braces parsed incorrectly
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.15   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks: 552012
  Show dependency tree
 
Reported: 2021-01-06 11:35 EST by Jonas Konrad CLA
Modified: 2024-02-26 06:33 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonas Konrad CLA 2021-01-06 11:35:04 EST
This bug was found on JDT 3.15.0 (I cannot upgrade due to the unrelated 560434) but seems to be present on newer versions still.

Nested Javadoc `@code` taglets are not parsed correctly:

```
public class Test {
  /**{@code {@code null}}*/
  public void test() {}
}
```

This code yields the HTML `<code>{@code null}</code>` when processed using the OpenJDK `javadoc` tool. When parsing the comment with JDT however, The @code TagElement is empty (`.fragments()` is empty) and the text (` {@code null}}`) comes *after* the TagElement as a separate TextElement.

While nested tag parsing appears to be implemented – there is a `openingBraces` variable in AbstractCommentParser – this handling does not appear to work (anymore?). In particular, the parser falsely sets `inlineTagStarted = false` at the first closing brace and never ends up pushing the text to the TagElement produced for the outer `@code`.

Unfortunately I am too unfamiliar with the JDT project structure to produce a test case for this that would accompany a PR. In local testing, the following patch, backported to JDT 3.15.0, appears to fix the issue:

```
Index: org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java	(revision d5c188d78ee37a5a78869313b94fa62ff542cea6)
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java	(date 1609950685782)
@@ -340,7 +340,8 @@
 							}
 							if (!isFormatterParser && !considerTagAsPlainText)
 								this.textStart = this.index;
-							setInlineTagStarted(false);
+							if (!considerTagAsPlainText)
+								setInlineTagStarted(false);
 						} else {
 							if (!this.lineStarted) {
 								this.textStart = previousPosition;
```
Comment 1 Mateusz Matela CLA 2022-03-05 18:52:04 EST
Just bumped into this while working on bug 552012.

The patch from Jonas works for one-line examples, but it doesn't fix the problem when opening and closing braces are in separate lines, for example:

/**
 * <pre>{@code
 * public class X {
 * 	int   a;
 * }
 * }</pre>
 */
public class MyTest {}

In this case the resulting tags structure is this:
[0] (TextElement) <pre>
[1] (TagElement) {@code public class X  int   a;}
  [1.0] (TextElement) public class X
  [1.1] (TextElement) int   a;
[2] (TextElement) </pre>
The TagElement [1] should have only one TextElement with full text of the @code tag


An even more complex case: when one of the lines starts with @ (in Java 11 Javadoc reported errors for this, but not since at least Java 15).
/**
 * <pre>{@code
 * public class X {
 * 	@Deprecated
 * 	int   a;
 * }
 * }</pre>
 */
Here the structure is wrong at the top level:
[0] (TextElement) <pre>
[1] (TagElement) {@code public class X }
[2] (TextElement) Deprecated
[2] (TextElement) int a;
[2] (TextElement) }
[2] (TextElement) }</pre>
Comment 2 Eclipse Genie CLA 2024-02-26 06:33:08 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.