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:15 UTC

(groovy) 04/34: GROOVY-9572: some __$stMC fields are inherited from base class in groovydoc

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 c6ed59d00ac03e549083d20722af2b617276cdd3
Author: Paul King <pa...@asert.com.au>
AuthorDate: Mon Apr 20 08:53:46 2026 +1000

    GROOVY-9572: some __$stMC fields are inherited from base class in groovydoc
---
 .../org/codehaus/groovy/classgen/Verifier.java     |  7 ++++
 src/test/groovy/bugs/Groovy9572.groovy             | 40 ++++++++++++++++++++++
 .../java/org/codehaus/groovy/ant/Groovydoc.java    | 14 ++++++++
 .../codehaus/groovy/tools/groovydoc/Main.groovy    |  4 +++
 .../org/apache/groovy/antlr/GroovydocVisitor.java  | 13 +++++++
 .../tools/groovydoc/antlr4/GroovyDocParser.java    |  2 +-
 .../groovydoc/antlr4/GroovydocJavaVisitor.java     | 27 +++++++++++++++
 .../groovy/tools/groovydoc/Main.properties         |  2 ++
 .../groovy/tools/groovydoc/GroovyDocToolTest.java  | 39 ++++++++++++++++++---
 9 files changed, 142 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 4300c57743..e9fdb4c782 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -96,6 +96,7 @@ import static java.util.stream.Collectors.joining;
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.hasAnnotation;
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.isGenerated;
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
+import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsInternal;
 import static org.apache.groovy.ast.tools.ConstructorNodeUtils.getFirstIfSpecialConstructorCall;
 import static org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants;
 import static org.codehaus.groovy.ast.ClassHelper.isObjectType;
@@ -486,6 +487,12 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                     ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC | ACC_TRANSIENT,
                     ClassHelper.boolean_TYPE, null);
             stMCB.setSynthetic(true);
+            // GROOVY-9572: mark as @Internal (GEP-17) so groovydoc hides it from
+            // inherited-field listings. Skip for classes built programmatically
+            // without a module context (matches the guard used by markAsGenerated).
+            if (node.getModule() != null && node.getModule().getContext() != null) {
+                markAsInternal(stMCB);
+            }
         }
     }
 
diff --git a/src/test/groovy/bugs/Groovy9572.groovy b/src/test/groovy/bugs/Groovy9572.groovy
new file mode 100644
index 0000000000..24e59bee23
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9572.groovy
@@ -0,0 +1,40 @@
+/*
+ *  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 bugs
+
+import groovy.transform.Internal
+import org.codehaus.groovy.classgen.Verifier
+import org.junit.jupiter.api.Test
+
+/**
+ * GROOVY-9572B: the Verifier-generated {@code __$stMC} field (constant
+ * {@link Verifier#STATIC_METACLASS_BOOL}) should carry
+ * {@link groovy.transform.Internal} so that tooling (groovydoc per GEP-17)
+ * can hide it from generated documentation of subclasses that inherit it.
+ */
+final class Groovy9572 {
+
+    @Test
+    void stMCFieldIsMarkedInternal() {
+        def cls = new GroovyClassLoader().parseClass('class Sample9572 {}')
+        def field = cls.getDeclaredField(Verifier.STATIC_METACLASS_BOOL)
+        assert field != null
+        assert field.isAnnotationPresent(Internal)
+    }
+}
diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovydoc.java b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovydoc.java
index ec4b53fb90..cd9e76506c 100644
--- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovydoc.java
+++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovydoc.java
@@ -67,6 +67,7 @@ public class Groovydoc extends Task {
     private boolean includeNoSourcePackages;
     private Boolean noTimestamp;
     private Boolean noVersionStamp;
+    private Boolean showInternal;
     private String javaVersion;
     private final List<DirSet> packageSets;
     private final List<String> sourceFilesToDoc;
@@ -95,6 +96,7 @@ public class Groovydoc extends Task {
         includeMainForScripts = true;
         noTimestamp = false;
         noVersionStamp = false;
+        showInternal = false;
     }
 
     /**
@@ -157,6 +159,17 @@ public class Groovydoc extends Task {
         this.javaVersion = javaVersion;
     }
 
+    /**
+     * If set to true, members annotated with {@code groovy.transform.@Internal}
+     * (per GEP-17) are still included in the generated documentation. Defaults
+     * to false, so internal members are hidden.
+     *
+     * @param showInternal new value
+     */
+    public void setShowInternal(boolean showInternal) {
+        this.showInternal = showInternal;
+    }
+
     /**
      * If set to false, Scripts will not be processed.
      * Defaults to true.
@@ -464,6 +477,7 @@ public class Groovydoc extends Task {
         properties.setProperty("fileEncoding", fileEncoding != null ? fileEncoding : "");
         properties.setProperty("timestamp", Boolean.valueOf(!noTimestamp).toString());
         properties.setProperty("versionStamp", Boolean.valueOf(!noVersionStamp).toString());
+        properties.setProperty("showInternal", showInternal.toString());
         String phaseOverride = SystemUtil.getSystemPropertySafe("groovydoc.phase.override");
         if (phaseOverride != null) properties.put("phaseOverride", phaseOverride);
 
diff --git a/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy b/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
index a18ddb5555..376c982c1d 100644
--- a/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
+++ b/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
@@ -50,6 +50,7 @@ class Main {
     private static Boolean publicScope
     private static Boolean protectedScope
     private static Boolean debug = false
+    private static Boolean showInternal
     private static String[] sourcepath
     private static String javaVersion
     private static List<String> sourceFilesToDoc
@@ -92,6 +93,7 @@ class Main {
         cli.stylesheetfile(args:1, argName: 'path', messages['cli.option.stylesheetfile.description'])
         cli.sourcepath(args:1, argName: 'pathlist', messages['cli.option.sourcepath.description'])
         cli.javaVersion(args: 1, argName: 'javaVersion', messages['cli.option.javaVersion.description'])
+        cli.showInternal(messages['cli.option.showInternal.description'])
 
         def options = cli.parse(args)
 
@@ -133,6 +135,7 @@ class Main {
         noMainForScripts = Boolean.valueOf(options.nomainforscripts) ?: false
         noTimestamp = Boolean.valueOf(options.notimestamp) ?: false
         noVersionStamp = Boolean.valueOf(options.noversionstamp) ?: false
+        showInternal = Boolean.valueOf(options.showInternal) ?: false
         packageScope = Boolean.valueOf(options.package) ?: false
         privateScope = Boolean.valueOf(options.private) ?: false
         protectedScope = Boolean.valueOf(options.protected) ?: false
@@ -212,6 +215,7 @@ class Main {
         properties.put("author", author.toString())
         properties.put("processScripts", (!noScripts).toString())
         properties.put("includeMainForScripts", (!noMainForScripts).toString())
+        properties.put("showInternal", showInternal.toString())
         properties.put("timestamp", (!noTimestamp).toString())
         properties.put("versionStamp", (!noVersionStamp).toString())
         properties.put("overviewFile", overviewFile?.absolutePath ?: "")
diff --git a/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/antlr/GroovydocVisitor.java b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/antlr/GroovydocVisitor.java
index 8ad221d67c..950b15ed18 100644
--- a/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/antlr/GroovydocVisitor.java
+++ b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/antlr/GroovydocVisitor.java
@@ -61,6 +61,7 @@ import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.deemedInternal;
 import static org.codehaus.groovy.transform.trait.Traits.isTrait;
 import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
 import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
@@ -221,9 +222,18 @@ public class GroovydocVisitor extends ClassCodeVisitorSupport {
         }
     }
 
+    // GROOVY-9572: hide members annotated with groovy.transform.@Internal (per GEP-17)
+    // or deemed internal by name convention (contains '$'), unless the user opts
+    // in with -showInternal / showInternal=true.
+    private boolean isInternal(AnnotatedNode node) {
+        if ("true".equals(properties.getProperty("showInternal", "false"))) return false;
+        return deemedInternal(node);
+    }
+
     @Override
     public void visitConstructor(ConstructorNode node) {
         if (node.isSynthetic()) return;
+        if (isInternal(node)) return;
         SimpleGroovyConstructorDoc cons = new SimpleGroovyConstructorDoc(currentClassDoc.simpleTypeName(), currentClassDoc);
         setConstructorOrMethodCommon(node, cons);
         currentClassDoc.add(cons);
@@ -235,6 +245,7 @@ public class GroovydocVisitor extends ClassCodeVisitorSupport {
         if (currentClassDoc.isEnum() && "$INIT".equals(node.getName()))
             return;
         if (node.isSynthetic()) return;
+        if (isInternal(node)) return;
         if ("false".equals(properties.getProperty("includeMainForScripts", "true"))
                 && currentClassDoc.isScript() && "main".equals(node.getName()) && node.isStatic() && node.getParameters().length == 1)
             return;
@@ -321,6 +332,7 @@ public class GroovydocVisitor extends ClassCodeVisitorSupport {
 
     @Override
     public void visitProperty(PropertyNode node) {
+        if (isInternal(node.getField())) return;
         String name = node.getName();
         SimpleGroovyFieldDoc fieldDoc = new SimpleGroovyFieldDoc(name, currentClassDoc);
         fieldDoc.setType(new SimpleGroovyType(makeType(node.getType())));
@@ -387,6 +399,7 @@ public class GroovydocVisitor extends ClassCodeVisitorSupport {
     @Override
     public void visitField(FieldNode node) {
         if (node.isSynthetic()) return;
+        if (isInternal(node)) return;
         String name = node.getName();
         SimpleGroovyFieldDoc fieldDoc = new SimpleGroovyFieldDoc(name, currentClassDoc);
         fieldDoc.setType(new SimpleGroovyType(makeType(node.getType())));
diff --git a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovyDocParser.java b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovyDocParser.java
index c507ff79fc..79e2be7535 100644
--- a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovyDocParser.java
+++ b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovyDocParser.java
@@ -70,7 +70,7 @@ public class GroovyDocParser implements GroovyDocParserI {
     }
 
     private Map<String, GroovyClassDoc> parseJava(String packagePath, String file, String src) throws RuntimeException {
-        GroovydocJavaVisitor visitor = new GroovydocJavaVisitor(packagePath, links);
+        GroovydocJavaVisitor visitor = new GroovydocJavaVisitor(packagePath, links, properties);
         try {
             visitor.visit(StaticJavaParser.parse(src), null);
         } catch (Throwable t) {
diff --git a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovydocJavaVisitor.java b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovydocJavaVisitor.java
index 3b8ec53c16..827fb61b1b 100644
--- a/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovydocJavaVisitor.java
+++ b/subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovydocJavaVisitor.java
@@ -63,6 +63,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Properties;
 
 public class GroovydocJavaVisitor
     extends VoidVisitorAdapter<Object> {
@@ -72,11 +73,17 @@ public class GroovydocJavaVisitor
     private final String packagePath;
     private final Map<String, String> aliases = new LinkedHashMap<>();
     private final List<String> imports = new ArrayList<>();
+    private final Properties properties;
     private static final String FS = "/";
 
     public GroovydocJavaVisitor(String packagePath, List<LinkArgument> links) {
+        this(packagePath, links, new Properties());
+    }
+
+    public GroovydocJavaVisitor(String packagePath, List<LinkArgument> links, Properties properties) {
         this.packagePath = packagePath;
         this.links = links;
+        this.properties = properties;
     }
 
     @Override
@@ -315,6 +322,23 @@ public class GroovydocJavaVisitor
         return "";
     }
 
+    // GROOVY-9572: hide members annotated with groovy.transform.@Internal (per GEP-17)
+    // or deemed internal by name convention (contains '$'), unless the user opts
+    // in with -showInternal / showInternal=true. Mirrors the policy of
+    // {@link org.apache.groovy.ast.tools.AnnotatedNodeUtils#deemedInternal} on
+    // the Groovy-AST side, expressed against JavaParser AST nodes here.
+    private boolean isInternal(NodeWithAnnotations<?> n) {
+        if ("true".equals(properties.getProperty("showInternal", "false"))) return false;
+        for (AnnotationExpr an : n.getAnnotations()) {
+            String name = an.getNameAsString();
+            if ("Internal".equals(name) || name.endsWith(".Internal")) return true;
+        }
+        if (n instanceof FieldDeclaration fd && fd.getVariable(0).getNameAsString().contains("$")) return true;
+        if (n instanceof MethodDeclaration md && md.getNameAsString().contains("$")) return true;
+        if (n instanceof ConstructorDeclaration cd && cd.getNameAsString().contains("$")) return true;
+        return false;
+    }
+
     private void setModifiers(NodeList<Modifier> modifiers, SimpleGroovyAbstractableElementDoc elementDoc) {
         if (modifiers.contains(Modifier.publicModifier())) {
             elementDoc.setPublic(true);
@@ -342,6 +366,7 @@ public class GroovydocJavaVisitor
 
     @Override
     public void visit(MethodDeclaration m, Object arg) {
+        if (isInternal(m)) return;
         SimpleGroovyMethodDoc meth = new SimpleGroovyMethodDoc(m.getNameAsString(), currentClassDoc);
         meth.setTypeParameters(genericTypesAsString(m.getTypeParameters()));
         meth.setReturnType(makeType(m.getType()));
@@ -356,6 +381,7 @@ public class GroovydocJavaVisitor
 
     @Override
     public void visit(ConstructorDeclaration c, Object arg) {
+        if (isInternal(c)) return;
         SimpleGroovyConstructorDoc meth = new SimpleGroovyConstructorDoc(c.getNameAsString(), currentClassDoc);
         setConstructorOrMethodCommon(c, meth);
         currentClassDoc.add(meth);
@@ -398,6 +424,7 @@ public class GroovydocJavaVisitor
 
     @Override
     public void visit(FieldDeclaration f, Object arg) {
+        if (isInternal(f)) return;
         String name = f.getVariable(0).getNameAsString();
         SimpleGroovyFieldDoc field = new SimpleGroovyFieldDoc(name, currentClassDoc);
         field.setType(makeType(f.getVariable(0).getType()));
diff --git a/subprojects/groovy-groovydoc/src/main/resources/org/codehaus/groovy/tools/groovydoc/Main.properties b/subprojects/groovy-groovydoc/src/main/resources/org/codehaus/groovy/tools/groovydoc/Main.properties
index 8636d1d79d..aa44b40fa0 100644
--- a/subprojects/groovy-groovydoc/src/main/resources/org/codehaus/groovy/tools/groovydoc/Main.properties
+++ b/subprojects/groovy-groovydoc/src/main/resources/org/codehaus/groovy/tools/groovydoc/Main.properties
@@ -78,4 +78,6 @@ cli.option.exclude.description=Specify a list of packages to exclude (separated
 
 cli.option.javaVersion.description=Specify the Java Version for parsed java files (e.g. JAVA_11)
 
+cli.option.showInternal.description=Include members annotated with groovy.transform.@Internal (hidden by default)
+
 cli.info.version=@|green GroovyDoc|@ {0}
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 af96e13e58..ebc6337f69 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
@@ -1265,11 +1265,9 @@ public class GroovyDocToolTest extends GroovyTestCase {
         assertFalse("@SuppressWarnings (not @Documented) should not appear in:\n" + javadoc,
                 javadoc.contains("SuppressWarnings"));
 
-        // GROOVY-4634: @groovy.transform.Internal is not @Documented so is filtered out
-        assertFalse("@Internal (not @Documented) should not appear on annotatedField in:\n" + groovydoc,
-                Pattern.compile("<h4>@groovy.transform.Internal<br>\\s*public&nbsp;.*annotatedField").matcher(groovydoc).find());
-        assertTrue("The Groovy field details should still render the field itself in:\n" + groovydoc,
-                Pattern.compile("<strong>annotatedField</strong></h4>").matcher(groovydoc).find());
+        // GROOVY-9572: @Internal-annotated members (per GEP-17) are hidden from groovydoc output
+        assertFalse("Field annotated with @Internal should be hidden in:\n" + groovydoc,
+                groovydoc.contains("<strong>annotatedField</strong>"));
 
         assertTrue("The Java field details should have the annotation", Pattern.compile(
                 "<h4>@(<a href='https://docs.oracle.com/javase/8/docs/api/java/lang/Deprecated.html' title='Deprecated'>Deprecated</a>|java.lang.Deprecated)<br>" +
@@ -1330,6 +1328,37 @@ public class GroovyDocToolTest extends GroovyTestCase {
                 javadoc.contains("@CommandLine.Parameters"));
     }
 
+    // GROOVY-9572A: members annotated with groovy.transform.@Internal are hidden,
+    // unless -showInternal / showInternal=true is set.
+    public void testInternalAnnotationHidesMembers() throws Exception {
+        final String base = "org/codehaus/groovy/tools/groovydoc/testfiles/anno";
+        Properties props = new Properties();
+        props.put("phaseOverride", "7");
+
+        // default: hidden
+        GroovyDocTool defaultTool = makeHtmltool(new ArrayList<>(), null, props);
+        defaultTool.add(Arrays.asList(base + "/Groovy.groovy"));
+        MockOutputTool defaultOut = new MockOutputTool();
+        defaultTool.renderToOutput(defaultOut, MOCK_DIR);
+        String defaultDoc = defaultOut.getText(MOCK_DIR + "/" + base + "/Groovy.html");
+        assertNotNull(defaultDoc);
+        assertFalse("annotatedField is @Internal and should be hidden by default in:\n" + defaultDoc,
+                defaultDoc.contains("<strong>annotatedField</strong>"));
+
+        // opt-in: shown
+        Properties opted = new Properties();
+        opted.put("phaseOverride", "7");
+        opted.put("showInternal", "true");
+        GroovyDocTool optedTool = makeHtmltool(new ArrayList<>(), null, opted);
+        optedTool.add(Arrays.asList(base + "/Groovy.groovy"));
+        MockOutputTool optedOut = new MockOutputTool();
+        optedTool.renderToOutput(optedOut, MOCK_DIR);
+        String optedDoc = optedOut.getText(MOCK_DIR + "/" + base + "/Groovy.html");
+        assertNotNull(optedDoc);
+        assertTrue("annotatedField should reappear with showInternal=true in:\n" + optedDoc,
+                optedDoc.contains("<strong>annotatedField</strong>"));
+    }
+
     public void testAbstractMethods() throws Exception {
         final String base = "org/codehaus/groovy/tools/groovydoc/testfiles";
         htmlTool.add(Arrays.asList(