You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "paulk-asert (via GitHub)" <gi...@apache.org> on 2026/03/27 23:38:22 UTC

[PR] GROOVY-11884: Close gaps in Groovy annotation validation [groovy]

paulk-asert opened a new pull request, #2407:
URL: https://github.com/apache/groovy/pull/2407

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GROOVY-11884: Close gaps in Groovy annotation validation [groovy]

Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert merged PR #2407:
URL: https://github.com/apache/groovy/pull/2407


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GROOVY-11884: Close gaps in Groovy annotation validation [groovy]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2407:
URL: https://github.com/apache/groovy/pull/2407#issuecomment-4146023223

   ## [Codecov](https://app.codecov.io/gh/apache/groovy/pull/2407?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `86.95652%` with `6 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 66.4248%. Comparing base ([`21604a6`](https://app.codecov.io/gh/apache/groovy/commit/21604a6a187427d7d0623f9d5f8d229dbf2867d9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`88afe5b`](https://app.codecov.io/gh/apache/groovy/commit/88afe5b28c3b902f4feddc2a514d177c749c217b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/groovy/pull/2407?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...org/codehaus/groovy/classgen/ExtendedVerifier.java](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fclassgen%2FExtendedVerifier.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NsYXNzZ2VuL0V4dGVuZGVkVmVyaWZpZXIuamF2YQ==) | 82.6087% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...n/java/org/codehaus/groovy/ast/AnnotationNode.java](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2FAnnotationNode.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9Bbm5vdGF0aW9uTm9kZS5qYXZh) | 90.0000% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/groovy/pull/2407/graphs/tree.svg?width=650&height=150&src=pr&token=1r45138NfQ&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@                Coverage Diff                 @@
   ##               master      #2407        +/-   ##
   ==================================================
   + Coverage     66.4099%   66.4248%   +0.0149%     
   - Complexity      29892      29906        +14     
   ==================================================
     Files            1392       1393         +1     
     Lines          116823     116869        +46     
     Branches        20675      20682         +7     
   ==================================================
   + Hits            77582      77630        +48     
   + Misses          32874      32871         -3     
   - Partials         6367       6368         +1     
   ```
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/groovy/pull/2407?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...va/groovy/lang/annotation/ExtendedElementType.java](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fgroovy%2Flang%2Fannotation%2FExtendedElementType.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9ncm9vdnkvbGFuZy9hbm5vdGF0aW9uL0V4dGVuZGVkRWxlbWVudFR5cGUuamF2YQ==) | `100.0000% <100.0000%> (ø)` | |
   | [...n/java/org/codehaus/groovy/ast/AnnotationNode.java](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2FAnnotationNode.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9Bbm5vdGF0aW9uTm9kZS5qYXZh) | `72.5806% <90.0000%> (+3.3499%)` | :arrow_up: |
   | [...org/codehaus/groovy/classgen/ExtendedVerifier.java](https://app.codecov.io/gh/apache/groovy/pull/2407?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fclassgen%2FExtendedVerifier.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NsYXNzZ2VuL0V4dGVuZGVkVmVyaWZpZXIuamF2YQ==) | `93.8462% <82.6087%> (-1.0906%)` | :arrow_down: |
   
   ... and [4 files with indirect coverage changes](https://app.codecov.io/gh/apache/groovy/pull/2407/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GROOVY-11884: Close gaps in Groovy annotation validation [groovy]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2407:
URL: https://github.com/apache/groovy/pull/2407#discussion_r3003915697


##########
src/main/java/org/codehaus/groovy/ast/AnnotationNode.java:
##########
@@ -159,9 +164,39 @@ public boolean isTargetAllowed(final int target) {
             return 0x4FF; // GROOVY-11838: JLS 9.6.4.1
         });
 
+        // check @ExtendedTarget for Groovy-specific element types
+        allowedTargets |= classNode.redirect().getNodeMetaData(ExtendedTarget.class, (k) -> {
+            for (AnnotationNode an : classNode.getAnnotations()) {
+                if ("groovy.lang.annotation.ExtendedTarget".equals(an.getClassNode().getName())) {
+                    Expression member = an.getMember("value");
+                    int bits = 0;
+                    if (member instanceof ListExpression list) {
+                        for (Expression e : list.getExpressions()) {
+                            bits |= extendedElementTypeBits(e);
+                        }
+                    } else {
+                        bits |= extendedElementTypeBits(member);
+                    }
+                    return bits;
+                }
+            }
+            return 0;
+        });
+
         return (target & allowedTargets) == target;
     }
 
+    private static int extendedElementTypeBits(final Expression e) {
+        if (e instanceof PropertyExpression item) {
+            String name = item.getPropertyAsString();
+            return switch (ExtendedElementType.valueOf(name)) {
+                case IMPORT -> IMPORT_TARGET;
+                case LOOP   -> STATEMENT_TARGET;
+            };

Review Comment:
   `extendedElementTypeBits` calls `ExtendedElementType.valueOf(name)` without guarding against `name == null` or unknown values. If an invalid enum constant is used in `@ExtendedTarget(...)`, the compiler may throw `IllegalArgumentException`/`NPE` here (potentially masking the existing validation error) instead of reporting a clean compilation error. Consider handling null/invalid names (e.g., check for null and catch `IllegalArgumentException`) and returning 0 or producing a targeted error message.
   ```suggestion
               if (name == null) {
                   return 0;
               }
               try {
                   return switch (ExtendedElementType.valueOf(name)) {
                       case IMPORT -> IMPORT_TARGET;
                       case LOOP   -> STATEMENT_TARGET;
                   };
               } catch (IllegalArgumentException ignore) {
                   // unknown ExtendedElementType value; no extended element type bits
                   return 0;
               }
   ```



##########
src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java:
##########
@@ -221,6 +232,32 @@ public void visitMethod(final MethodNode node) {
         }
     }
 
+    @Override
+    protected void visitStatementAnnotations(final Statement statement) {
+        for (AnnotationNode annotation : statement.getStatementAnnotations()) {
+            ErrorCollector errorCollector = new ErrorCollector(source.getConfiguration());
+            AnnotationVisitor visitor = new AnnotationVisitor(source, errorCollector);
+            AnnotationNode visited = visitor.visit(annotation);
+            source.getErrorCollector().addCollectorContents(errorCollector);
+
+            if (!visited.isTargetAllowed(STATEMENT_TARGET)) {
+                addError("Annotation @" + visited.getClassNode().getName() + " is not allowed on element "
+                        + AnnotationNode.targetToName(STATEMENT_TARGET), visited);
+            }
+        }
+    }

Review Comment:
   This PR adds new validation for statement annotations via `visitStatementAnnotations`, but the test additions only cover import-level annotations. Consider adding a focused test that (1) rejects a non-`@ExtendedTarget(LOOP)` annotation on a loop and (2) accepts an annotation that declares `@ExtendedTarget(LOOP)`, to ensure this new validation path is exercised and doesn’t regress.



##########
src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java:
##########
@@ -43,23 +45,27 @@
 import org.objectweb.asm.Opcodes;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;

Review Comment:
   Unused import: `java.util.Collections` is added but not referenced in this file. Please remove it to keep the import list clean and avoid warnings.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org