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 2021/09/29 08:11:08 UTC

[groovy] branch master updated: GROOVY-10240: Support record grammar (adjust annotation validation for records)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 67dfd60  GROOVY-10240: Support record grammar (adjust annotation validation for records)
67dfd60 is described below

commit 67dfd6025bb8802b0aa30efcc87278627eb376d5
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Sep 29 18:11:02 2021 +1000

    GROOVY-10240: Support record grammar (adjust annotation validation for records)
---
 .../java/org/codehaus/groovy/classgen/ExtendedVerifier.java  | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java b/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
index 9688dae..ea1ec4e 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
@@ -270,6 +270,7 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
             }
 
             String name = visited.getClassNode().getName();
+            boolean skip = currentClass.isRecord() && skippableRecordAnnotation(node, visited);
             if (!visited.hasSourceRetention()) {
                 List<AnnotationNode> seen = nonSourceAnnotations.get(name);
                 if (seen == null) {
@@ -278,13 +279,15 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
                     addError("Cannot specify duplicate annotation on the same member : " + name, visited);
                 }
                 seen.add(visited);
-                nonSourceAnnotations.put(name, seen);
+                if (!skip) {
+                    nonSourceAnnotations.put(name, seen);
+                }
             }
 
             // Check if the annotation target is correct, unless it's the target annotating an annotation definition
             // defining on which target elements the annotation applies
             boolean isTargetAnnotation = name.equals("java.lang.annotation.Target");
-            if (!isTargetAnnotation && !visited.isTargetAllowed(target) && !isTypeUseScenario(visited, target)) {
+            if (!isTargetAnnotation && !skip && !visited.isTargetAllowed(target) && !isTypeUseScenario(visited, target)) {
                 addError("Annotation @" + name + " is not allowed on element " + AnnotationNode.targetToName(target), visited);
             }
             visitDeprecation(node, visited);
@@ -293,6 +296,11 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
         processDuplicateAnnotationContainers(node, nonSourceAnnotations);
     }
 
+    private boolean skippableRecordAnnotation(AnnotatedNode node, AnnotationNode visited) {
+        return (node instanceof ClassNode && !visited.isTargetAllowed(TYPE_TARGET) && !visited.isTargetAllowed(TYPE_USE_TARGET) && visited.isTargetAllowed(CONSTRUCTOR_TARGET))
+                || (node instanceof FieldNode && !visited.isTargetAllowed(FIELD_TARGET) && !visited.isTargetAllowed(TYPE_USE_TARGET));
+    }
+
     private boolean isRepeatable(final AnnotationNode annoNode) {
         ClassNode annoClassNode = annoNode.getClassNode();
         String name = annoClassNode.getName();