You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/04/21 06:13:22 UTC

(groovy) 11/34: GROOVY-3782: {@inheritDoc} + stray-brace rendering bug

This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit e443e748e66f9a112eb38d44b46f024412fbc2db
Author: Paul King <pa...@asert.com.au>
AuthorDate: Mon Apr 20 18:13:39 2026 +1000

    GROOVY-3782: {@inheritDoc} + stray-brace rendering bug
---
 .../tools/groovydoc/SimpleGroovyClassDoc.java      |  12 +-
 .../tools/groovydoc/SimpleGroovyMemberDoc.java     |   4 +-
 .../groovy/tools/groovydoc/TagRenderer.java        | 137 +++++++++++++++++----
 .../groovy/tools/groovydoc/GroovyDocToolTest.java  |  34 +++++
 .../testfiles/ClassWithInheritDocStub.groovy       |  44 +++++++
 5 files changed, 205 insertions(+), 26 deletions(-)

diff --git a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyClassDoc.java b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyClassDoc.java
index a9e5e7aad7..1cf711507c 100644
--- a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyClassDoc.java
+++ b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyClassDoc.java
@@ -889,6 +889,16 @@ public class SimpleGroovyClassDoc extends SimpleGroovyAbstractableElementDoc imp
     }
 
     public String replaceTags(String comment) {
+        return replaceTags(comment, null);
+    }
+
+    /**
+     * GROOVY-3782 / GROOVY-6016: overload that threads the current member
+     * through to {@link TagRenderer} so inline tags that need the enclosing
+     * member context (e.g. {@code {@inheritDoc}} needs the current method
+     * to find its overridden parent) can resolve.
+     */
+    public String replaceTags(String comment, org.codehaus.groovy.groovydoc.GroovyMemberDoc memberDoc) {
         // Strip the leading ' * ' block-comment prefix from each line.
         String result = comment.replaceAll("(?m)^\\s*\\*", "");
 
@@ -902,7 +912,7 @@ public class SimpleGroovyClassDoc extends SimpleGroovyAbstractableElementDoc imp
         result = result.replaceAll(DOCROOT_PATTERN, relativeRootPath);
 
         // GROOVY-11939: single-pass tokenize + render for inline and block tags.
-        return TagRenderer.render(result, links, relativeRootPath, savedRootDoc, this);
+        return TagRenderer.render(result, links, relativeRootPath, savedRootDoc, this, memberDoc);
     }
 
     /**
diff --git a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyMemberDoc.java b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyMemberDoc.java
index 7ce99cb26e..5eff71a40b 100644
--- a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyMemberDoc.java
+++ b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyMemberDoc.java
@@ -38,7 +38,7 @@ public class SimpleGroovyMemberDoc extends SimpleGroovyAbstractableElementDoc im
     public String firstSentenceCommentText() {
         if (super.firstSentenceCommentText() == null) {
             SimpleGroovyClassDoc classDoc = (SimpleGroovyClassDoc) belongsToClass;
-            setFirstSentenceCommentText(classDoc.replaceTags(calculateFirstSentence(getRawCommentText())));
+            setFirstSentenceCommentText(classDoc.replaceTags(calculateFirstSentence(getRawCommentText()), this));
         }
         return super.firstSentenceCommentText();
     }
@@ -47,7 +47,7 @@ public class SimpleGroovyMemberDoc extends SimpleGroovyAbstractableElementDoc im
     public String commentText() {
         if (super.commentText() == null) {
             SimpleGroovyClassDoc classDoc = (SimpleGroovyClassDoc) belongsToClass;
-            setCommentText(classDoc.replaceTags(getRawCommentText()));
+            setCommentText(classDoc.replaceTags(getRawCommentText(), this));
         }
         return super.commentText();
     }
diff --git a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/TagRenderer.java b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/TagRenderer.java
index e687b81fa8..50ce5b7d9f 100644
--- a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/TagRenderer.java
+++ b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/TagRenderer.java
@@ -19,6 +19,9 @@
 package org.codehaus.groovy.tools.groovydoc;
 
 import org.codehaus.groovy.groovydoc.GroovyClassDoc;
+import org.codehaus.groovy.groovydoc.GroovyMemberDoc;
+import org.codehaus.groovy.groovydoc.GroovyMethodDoc;
+import org.codehaus.groovy.groovydoc.GroovyParameter;
 import org.codehaus.groovy.groovydoc.GroovyRootDoc;
 
 import java.util.ArrayList;
@@ -128,7 +131,7 @@ final class TagRenderer {
                          String relPath,
                          GroovyRootDoc rootDoc,
                          SimpleGroovyClassDoc classDoc) {
-        return render(text, links, relPath, rootDoc, classDoc, CLASS_LEVEL);
+        return render(text, links, relPath, rootDoc, classDoc, null, CLASS_LEVEL);
     }
 
     static String render(String text,
@@ -136,6 +139,25 @@ final class TagRenderer {
                          String relPath,
                          GroovyRootDoc rootDoc,
                          SimpleGroovyClassDoc classDoc,
+                         GroovyMemberDoc memberDoc) {
+        return render(text, links, relPath, rootDoc, classDoc, memberDoc, CLASS_LEVEL);
+    }
+
+    static String render(String text,
+                         List<LinkArgument> links,
+                         String relPath,
+                         GroovyRootDoc rootDoc,
+                         SimpleGroovyClassDoc classDoc,
+                         Config cfg) {
+        return render(text, links, relPath, rootDoc, classDoc, null, cfg);
+    }
+
+    static String render(String text,
+                         List<LinkArgument> links,
+                         String relPath,
+                         GroovyRootDoc rootDoc,
+                         SimpleGroovyClassDoc classDoc,
+                         GroovyMemberDoc memberDoc,
                          Config cfg) {
         StringBuilder out = new StringBuilder(text.length() + 64);
         // Whitespace preceding a block tag must be dropped (matches the
@@ -152,11 +174,11 @@ final class TagRenderer {
             if (c == '{' && i + 1 < n && text.charAt(i + 1) == '@') {
                 out.append(pendingWs);
                 pendingWs.setLength(0);
-                int consumed = renderInlineTagAt(text, i, out, links, relPath, rootDoc, classDoc, cfg);
+                int consumed = renderInlineTagAt(text, i, out, links, relPath, rootDoc, classDoc, memberDoc, cfg);
                 if (consumed > 0) { i += consumed; continue; }
             }
             if (c == '@' && isBlockTagBoundary(text, i)) {
-                int consumed = renderBlockTagAt(text, i, out, collated, links, relPath, rootDoc, classDoc, cfg);
+                int consumed = renderBlockTagAt(text, i, out, collated, links, relPath, rootDoc, classDoc, memberDoc, cfg);
                 if (consumed > 0) {
                     pendingWs.setLength(0); // discard ws preceding the tag
                     anyBlockTag = true;
@@ -193,23 +215,19 @@ final class TagRenderer {
     private static int renderInlineTagAt(String text, int start, StringBuilder out,
                                          List<LinkArgument> links, String relPath,
                                          GroovyRootDoc rootDoc, SimpleGroovyClassDoc classDoc,
+                                         GroovyMemberDoc memberDoc,
                                          Config cfg) {
         int nameStart = start + 2; // skip '{@'
         int nameEnd = readTagName(text, nameStart);
         if (nameEnd == nameStart) return 0;
         String name = text.substring(nameStart, nameEnd);
-        // Historical behaviour: only the known inline tags are recognised.
-        // Anything else (e.g. Javadoc `{@linkplain}` or `{@inheritDoc}`) flows
-        // through as plain text so that later passes / subsequent tickets
-        // can decide how to handle it. Skipping unknown tags here preserves
-        // byte-for-byte parity with the old LINK/LITERAL/CODE regex chain.
         if (!isKnownInlineTag(name, cfg)) return 0;
         int bodyStart = skipWhitespace(text, nameEnd);
         // Match the previous regex's laxness: body extends to the first '}'.
         int close = text.indexOf('}', bodyStart);
         if (close < 0) return 0;
         String body = text.substring(bodyStart, close);
-        renderInlineTag(name, body, out, links, relPath, rootDoc, classDoc, cfg);
+        renderInlineTag(name, body, out, links, relPath, rootDoc, classDoc, memberDoc, cfg);
         return close + 1 - start;
     }
 
@@ -220,6 +238,7 @@ final class TagRenderer {
             case "code":
             case "interface":
             case "value":
+            case "inheritDoc":
                 return true;
             case "literal":
                 return cfg.literalEnabled;
@@ -231,6 +250,7 @@ final class TagRenderer {
     private static void renderInlineTag(String name, String body, StringBuilder out,
                                         List<LinkArgument> links, String relPath,
                                         GroovyRootDoc rootDoc, SimpleGroovyClassDoc classDoc,
+                                        GroovyMemberDoc memberDoc,
                                         Config cfg) {
         switch (name) {
             case "interface":
@@ -251,11 +271,8 @@ final class TagRenderer {
                 out.append(cfg.codeOpen).append(SimpleGroovyClassDoc.encodeAngleBrackets(body)).append(cfg.codeClose);
                 return;
             case "value": {
-                // GROOVY-6016: resolve {@value #FIELD} or {@value Class#FIELD}
-                // to the field's source-form constant value. Bare {@value}
-                // (current field) is not supported here because we don't
-                // thread the current member through TagRenderer — fall back
-                // to emitting the tag verbatim in that case.
+                // GROOVY-6016: resolve {@value #FIELD} or {@value Class#FIELD}.
+                // Bare {@value} (current field) is pending — see task #25.
                 String resolved = resolveValueTag(body, rootDoc, classDoc);
                 if (resolved != null) {
                     out.append(SimpleGroovyClassDoc.encodeAngleBrackets(resolved));
@@ -263,6 +280,16 @@ final class TagRenderer {
                 }
                 break; // unresolved — emit verbatim
             }
+            case "inheritDoc": {
+                // GROOVY-3782: only meaningful on a method; pull the parent
+                // method's already-rendered comment text into this position.
+                String inherited = resolveInheritDoc(memberDoc, classDoc, new java.util.HashSet<>());
+                if (inherited != null) {
+                    out.append(inherited);
+                    return;
+                }
+                break; // unresolved — emit verbatim
+            }
         }
         // Unknown or unresolved inline tag — emit body verbatim (matches prior regex behaviour).
         out.append('{').append('@').append(name);
@@ -281,6 +308,73 @@ final class TagRenderer {
      *   <li>empty — not supported here (needs current-member context); returns {@code null}</li>
      * </ul>
      */
+    /**
+     * GROOVY-3782: resolve {@code {@inheritDoc}} to the parent method's
+     * already-rendered comment text. Walks the superclass chain, then
+     * declared interfaces, looking for a method with the same name and
+     * matching parameter type names. Returns {@code null} if the current
+     * member isn't a method, no parent method is found, or the parent
+     * method has no doc.
+     *
+     * <p>Recursion safety: the {@code visited} set tracks methods we've
+     * already expanded on this chain to prevent infinite loops (e.g. two
+     * interfaces with {@code {@inheritDoc}} pointing at each other).</p>
+     */
+    private static String resolveInheritDoc(GroovyMemberDoc memberDoc,
+                                            SimpleGroovyClassDoc classDoc,
+                                            java.util.Set<GroovyMethodDoc> visited) {
+        if (!(memberDoc instanceof GroovyMethodDoc)) return null;
+        GroovyMethodDoc thisMethod = (GroovyMethodDoc) memberDoc;
+        if (!visited.add(thisMethod)) return null; // cycle
+        if (classDoc == null) return null;
+
+        GroovyMethodDoc parent = findInheritedMethod(thisMethod, classDoc, new java.util.HashSet<>());
+        if (parent == null) return null;
+        // Calling commentText() on the parent runs it through the full
+        // replaceTags pipeline — so any {@inheritDoc} nested in the parent
+        // resolves recursively via its own ancestry, and our `visited` set
+        // blocks cycles.
+        String rendered = parent.commentText();
+        return rendered == null ? "" : rendered;
+    }
+
+    private static GroovyMethodDoc findInheritedMethod(GroovyMethodDoc thisMethod,
+                                                       GroovyClassDoc startingClass,
+                                                       java.util.Set<GroovyClassDoc> seen) {
+        // Javadoc's search order: superclass chain first, then interfaces.
+        for (GroovyClassDoc sup = startingClass.superclass(); sup != null; sup = sup.superclass()) {
+            if (!seen.add(sup)) break;
+            GroovyMethodDoc match = findMatchingMethod(sup, thisMethod);
+            if (match != null) return match;
+        }
+        for (GroovyClassDoc iface : startingClass.interfaces()) {
+            if (!seen.add(iface)) continue;
+            GroovyMethodDoc match = findMatchingMethod(iface, thisMethod);
+            if (match != null) return match;
+            GroovyMethodDoc deeper = findInheritedMethod(thisMethod, iface, seen);
+            if (deeper != null) return deeper;
+        }
+        return null;
+    }
+
+    private static GroovyMethodDoc findMatchingMethod(GroovyClassDoc cls, GroovyMethodDoc target) {
+        String targetName = target.name();
+        GroovyParameter[] targetParams = target.parameters();
+        for (GroovyMethodDoc m : cls.methods()) {
+            if (!targetName.equals(m.name())) continue;
+            GroovyParameter[] params = m.parameters();
+            if (params.length != targetParams.length) continue;
+            boolean allMatch = true;
+            for (int i = 0; i < params.length; i++) {
+                String a = params[i].typeName();
+                String b = targetParams[i].typeName();
+                if (a == null ? b != null : !a.equals(b)) { allMatch = false; break; }
+            }
+            if (allMatch) return m;
+        }
+        return null;
+    }
+
     private static String resolveValueTag(String body, GroovyRootDoc rootDoc, SimpleGroovyClassDoc classDoc) {
         if (body == null || body.isEmpty()) return null;
         String className;
@@ -326,27 +420,22 @@ final class TagRenderer {
                                         Map<String, List<String>> collated,
                                         List<LinkArgument> links, String relPath,
                                         GroovyRootDoc rootDoc, SimpleGroovyClassDoc classDoc,
+                                        GroovyMemberDoc memberDoc,
                                         Config cfg) {
         int nameStart = start + 1; // skip '@'
         int nameEnd = readTagName(text, nameStart);
         if (nameEnd == nameStart) return 0;
-        // Require at least one whitespace after the tag name (mirrors the old
-        // regex's `\s+` between name and body). Rejects things like
-        // `@CompileStatic(TypeCheckingMode.SKIP)` mid-sentence — those should
-        // stay as literal text, not be parsed as a block tag.
         int n = text.length();
         if (nameEnd >= n || !Character.isWhitespace(text.charAt(nameEnd))) return 0;
         int bodyStart = skipWhitespace(text, nameEnd);
         int bodyEnd = findBlockTagEnd(text, bodyStart);
         String name = text.substring(nameStart, nameEnd);
-        // Trim trailing whitespace from the body, matching the prior regex's
-        // non-greedy `(.*?)(?=\s+@)` / " @endMarker" behaviour.
         int trimmedEnd = bodyEnd;
         while (trimmedEnd > bodyStart && Character.isWhitespace(text.charAt(trimmedEnd - 1))) {
             trimmedEnd--;
         }
         String rawBody = text.substring(bodyStart, trimmedEnd);
-        handleBlockTag(name, rawBody, out, collated, links, relPath, rootDoc, classDoc, cfg);
+        handleBlockTag(name, rawBody, out, collated, links, relPath, rootDoc, classDoc, memberDoc, cfg);
         // Advance only past the trimmed body — trailing whitespace flows back
         // through the main loop as plain text so that any whitespace trailing
         // the overall input (for the last block tag) is preserved verbatim
@@ -359,12 +448,13 @@ final class TagRenderer {
                                        Map<String, List<String>> collated,
                                        List<LinkArgument> links, String relPath,
                                        GroovyRootDoc rootDoc, SimpleGroovyClassDoc classDoc,
+                                       GroovyMemberDoc memberDoc,
                                        Config cfg) {
         if ("interface".equals(name)) return; // swallow, matches prior behaviour
 
         // Block-tag bodies may embed inline tags like {@link ...}; render those
         // now so that collation sees fully-rendered HTML.
-        String body = renderInline(rawBody, links, relPath, rootDoc, classDoc, cfg);
+        String body = renderInline(rawBody, links, relPath, rootDoc, classDoc, memberDoc, cfg);
 
         if (cfg.collateBlockTags) {
             if ("see".equals(name) || "link".equals(name)) {
@@ -400,6 +490,7 @@ final class TagRenderer {
     private static String renderInline(String text,
                                        List<LinkArgument> links, String relPath,
                                        GroovyRootDoc rootDoc, SimpleGroovyClassDoc classDoc,
+                                       GroovyMemberDoc memberDoc,
                                        Config cfg) {
         StringBuilder out = new StringBuilder(text.length());
         int i = 0;
@@ -407,7 +498,7 @@ final class TagRenderer {
         while (i < n) {
             char c = text.charAt(i);
             if (c == '{' && i + 1 < n && text.charAt(i + 1) == '@') {
-                int consumed = renderInlineTagAt(text, i, out, links, relPath, rootDoc, classDoc, cfg);
+                int consumed = renderInlineTagAt(text, i, out, links, relPath, rootDoc, classDoc, memberDoc, cfg);
                 if (consumed > 0) { i += consumed; continue; }
             }
             out.append(c);
diff --git a/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocToolTest.java b/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocToolTest.java
index 5d3c47260b..eca92b1b49 100644
--- a/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocToolTest.java
+++ b/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocToolTest.java
@@ -131,6 +131,40 @@ public class GroovyDocToolTest extends GroovyTestCase {
             doc.contains("https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html"));
     }
 
+    // GROOVY-3782 (stray-brace rendering bug): the old regex chain broke
+    // `{@inheritDoc}` into bogus `{<DL><DT><B>inheritDoc:</B>...` HTML. The
+    // GROOVY-11939 tokenizer refactor fixed that as a side-effect. Verify.
+    public void testInheritDocDoesNotLeaveStrayBrace() throws Exception {
+        String base = "org/codehaus/groovy/tools/groovydoc/testfiles";
+        htmlTool.add(List.of(base + "/ClassWithInheritDocStub.groovy"));
+
+        MockOutputTool output = new MockOutputTool();
+        htmlTool.renderToOutput(output, MOCK_DIR);
+
+        String doc = output.getText(MOCK_DIR + "/" + base + "/InheritDocChild.html");
+        assertNotNull("Expected InheritDocChild page", doc);
+        assertFalse("stray-{ or broken block tag around inheritDoc in:\n" + doc,
+                doc.contains("{<DL><DT><B>inheritDoc") || doc.contains("inheritDoc:</B>"));
+    }
+
+    // GROOVY-3782 (the actual {@inheritDoc} tag): the child method's doc
+    // should be expanded with the parent's rendered description plus tags.
+    public void testInheritDocSubstitutesParentMethodDoc() throws Exception {
+        String base = "org/codehaus/groovy/tools/groovydoc/testfiles";
+        htmlTool.add(List.of(base + "/ClassWithInheritDocStub.groovy"));
+
+        MockOutputTool output = new MockOutputTool();
+        htmlTool.renderToOutput(output, MOCK_DIR);
+
+        String doc = output.getText(MOCK_DIR + "/" + base + "/InheritDocChild.html");
+        assertNotNull(doc);
+        assertTrue("Parent description should be inherited into child in:\n" + doc,
+                doc.contains("Parent-level description"));
+        // The literal `{@inheritDoc}` must not appear anymore.
+        assertFalse("{@inheritDoc} should be substituted, not left literal in:\n" + doc,
+                doc.contains("{@inheritDoc}"));
+    }
+
     // GROOVY-6016: {@value #FIELD} inline tag resolves same-class constants.
     public void testValueTagResolvesSameClassConstants() throws Exception {
         String base = "org/codehaus/groovy/tools/groovydoc/testfiles";
diff --git a/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/testfiles/ClassWithInheritDocStub.groovy b/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/testfiles/ClassWithInheritDocStub.groovy
new file mode 100644
index 0000000000..02bd32bd5c
--- /dev/null
+++ b/subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/testfiles/ClassWithInheritDocStub.groovy
@@ -0,0 +1,44 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.tools.groovydoc.testfiles
+
+/**
+ * Base class with a documented method.
+ */
+class InheritDocBase {
+    /**
+     * Parent-level description.
+     * @param x the input
+     * @return the transformed value
+     */
+    String transform(String x) { x.toUpperCase() }
+}
+
+/**
+ * GROOVY-3782 stub: before {@code {@inheritDoc}} is implemented, the tag
+ * should at minimum not leave a stray {@code '{'} or mangled block tag in
+ * the rendered output.
+ */
+class InheritDocChild extends InheritDocBase {
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    String transform(String x) { x.toLowerCase() }
+}