You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/07/05 18:25:14 UTC

[groovy] branch GROOVY_4_0_X updated (13cf20edd0 -> 493f15efde)

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

emilles pushed a change to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


    from 13cf20edd0 minor typos
     new 61ecf78b60 add `@since` tag
     new 493f15efde GROOVY-9871: class model: populate `AnnotationConstantExpression` values

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/java/groovy/transform/Generated.java      |  6 +-
 src/main/java/groovy/transform/Internal.java       |  6 +-
 src/main/java/groovy/transform/Pure.java           |  8 +-
 .../org/codehaus/groovy/vmplugin/v8/Java8.java     | 99 ++++++++++++----------
 .../ast/{Groovy7826Bug.java => Groovy7826.java}    | 28 +++---
 src/test/org/codehaus/groovy/ast/Groovy9871.groovy | 46 ++++++++++
 6 files changed, 129 insertions(+), 64 deletions(-)
 rename src/test/org/codehaus/groovy/ast/{Groovy7826Bug.java => Groovy7826.java} (61%)
 create mode 100644 src/test/org/codehaus/groovy/ast/Groovy9871.groovy


[groovy] 02/02: GROOVY-9871: class model: populate `AnnotationConstantExpression` values

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 493f15efdecad2c55298e747e47da80b23552be2
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jul 5 11:17:08 2022 -0500

    GROOVY-9871: class model: populate `AnnotationConstantExpression` values
---
 .../org/codehaus/groovy/vmplugin/v8/Java8.java     | 99 ++++++++++++----------
 .../ast/{Groovy7826Bug.java => Groovy7826.java}    | 28 +++---
 src/test/org/codehaus/groovy/ast/Groovy9871.groovy | 46 ++++++++++
 3 files changed, 116 insertions(+), 57 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
index 51a6c3538c..81b060ebfe 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
@@ -35,6 +35,7 @@ import org.codehaus.groovy.ast.GenericsType;
 import org.codehaus.groovy.ast.MethodNode;
 import org.codehaus.groovy.ast.PackageNode;
 import org.codehaus.groovy.ast.Parameter;
+import org.codehaus.groovy.ast.expr.AnnotationConstantExpression;
 import org.codehaus.groovy.ast.expr.ClassExpression;
 import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
@@ -291,40 +292,7 @@ public class Java8 implements VMPlugin {
         return gts;
     }
 
-    private void setAnnotationMetaData(final Annotation[] annotations, final AnnotatedNode an) {
-        for (Annotation annotation : annotations) {
-            AnnotationNode node = new AnnotationNode(ClassHelper.make(annotation.annotationType()));
-            configureAnnotation(node, annotation);
-            an.addAnnotation(node);
-        }
-    }
-
-    @Override
-    public void configureAnnotationNodeFromDefinition(final AnnotationNode definition, final AnnotationNode root) {
-        ClassNode type = definition.getClassNode();
-        final String typeName = type.getName();
-        if ("java.lang.annotation.Retention".equals(typeName)) {
-            Expression exp = definition.getMember("value");
-            if (!(exp instanceof PropertyExpression)) return;
-            PropertyExpression pe = (PropertyExpression) exp;
-            String name = pe.getPropertyAsString();
-            RetentionPolicy policy = RetentionPolicy.valueOf(name);
-            setRetentionPolicy(policy, root);
-        } else if ("java.lang.annotation.Target".equals(typeName)) {
-            Expression exp = definition.getMember("value");
-            if (!(exp instanceof ListExpression)) return;
-            ListExpression le = (ListExpression) exp;
-            int bitmap = 0;
-            for (Expression e : le.getExpressions()) {
-                if (!(e instanceof PropertyExpression)) return;
-                PropertyExpression element = (PropertyExpression) e;
-                String name = element.getPropertyAsString();
-                ElementType value = ElementType.valueOf(name);
-                bitmap |= getElementCode(value);
-            }
-            root.setAllowedTargets(bitmap);
-        }
-    }
+    //
 
     @Override
     public void configureAnnotation(final AnnotationNode node) {
@@ -367,37 +335,80 @@ public class Java8 implements VMPlugin {
             for (Method declaredMethod : declaredMethods) {
                 try {
                     Object value = declaredMethod.invoke(annotation);
-                    Expression valueExpression = annotationValueToExpression(value);
-                    if (valueExpression == null)
-                        continue;
-                    node.setMember(declaredMethod.getName(), valueExpression);
+                    Expression valueExpression = toAnnotationValueExpression(value);
+                    if (valueExpression != null) node.setMember(declaredMethod.getName(), valueExpression);
+
                 } catch (IllegalAccessException | InvocationTargetException ignore) {
                 }
             }
         }
     }
 
-    private Expression annotationValueToExpression(final Object value) {
+    private void setAnnotationMetaData(final Annotation[] annotations, final AnnotatedNode target) {
+        for (Annotation annotation : annotations) {
+            target.addAnnotation(toAnnotationNode(annotation));
+        }
+    }
+
+    private AnnotationNode toAnnotationNode(final Annotation annotation) {
+        ClassNode type = ClassHelper.make(annotation.annotationType());
+        AnnotationNode node = new AnnotationNode(type);
+        configureAnnotation(node, annotation);
+        return node;
+    }
+
+    private Expression toAnnotationValueExpression(final Object value) {
         if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
             return new ConstantExpression(value);
 
         if (value instanceof Class)
             return new ClassExpression(ClassHelper.makeWithoutCaching((Class<?>)value));
 
+        if (value instanceof Annotation)
+            return new AnnotationConstantExpression(toAnnotationNode((Annotation)value));
+
         if (value instanceof Enum)
             return new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(value.getClass())), value.toString());
 
         if (value.getClass().isArray()) {
-            ListExpression elementExprs = new ListExpression();
-            int len = Array.getLength(value);
-            for (int i = 0; i != len; i += 1)
-                elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
-            return elementExprs;
+            ListExpression list = new ListExpression();
+            for (int i = 0, n = Array.getLength(value); i < n; i += 1)
+                list.addExpression(toAnnotationValueExpression(Array.get(value, i)));
+            return list;
         }
 
         return null;
     }
 
+    //
+
+    @Override
+    public void configureAnnotationNodeFromDefinition(final AnnotationNode definition, final AnnotationNode root) {
+        ClassNode type = definition.getClassNode();
+        final String typeName = type.getName();
+        if ("java.lang.annotation.Retention".equals(typeName)) {
+            Expression exp = definition.getMember("value");
+            if (!(exp instanceof PropertyExpression)) return;
+            PropertyExpression pe = (PropertyExpression) exp;
+            String name = pe.getPropertyAsString();
+            RetentionPolicy policy = RetentionPolicy.valueOf(name);
+            setRetentionPolicy(policy, root);
+        } else if ("java.lang.annotation.Target".equals(typeName)) {
+            Expression exp = definition.getMember("value");
+            if (!(exp instanceof ListExpression)) return;
+            ListExpression le = (ListExpression) exp;
+            int bitmap = 0;
+            for (Expression e : le.getExpressions()) {
+                if (!(e instanceof PropertyExpression)) return;
+                PropertyExpression element = (PropertyExpression) e;
+                String name = element.getPropertyAsString();
+                ElementType value = ElementType.valueOf(name);
+                bitmap |= getElementCode(value);
+            }
+            root.setAllowedTargets(bitmap);
+        }
+    }
+
     @Override
     public void configureClassNode(final CompileUnit compileUnit, final ClassNode classNode) {
         try {
diff --git a/src/test/org/codehaus/groovy/ast/Groovy7826Bug.java b/src/test/org/codehaus/groovy/ast/Groovy7826.java
similarity index 61%
rename from src/test/org/codehaus/groovy/ast/Groovy7826Bug.java
rename to src/test/org/codehaus/groovy/ast/Groovy7826.java
index ed6b4ab596..fb8745c52a 100644
--- a/src/test/org/codehaus/groovy/ast/Groovy7826Bug.java
+++ b/src/test/org/codehaus/groovy/ast/Groovy7826.java
@@ -19,23 +19,25 @@
 package org.codehaus.groovy.ast;
 
 import groovy.lang.GroovyShell;
-import groovy.test.GroovyTestCase;
 import org.codehaus.groovy.control.CompilerConfiguration;
+import org.junit.Test;
 
-public class Groovy7826Bug extends GroovyTestCase {
-  public void testComplexTypeArguments() throws Exception {
-    String script = "def f(org.codehaus.groovy.ast.Groovy7826Bug.C1 c1) { }";
+public final class Groovy7826 {
 
-    CompilerConfiguration config = new CompilerConfiguration();
-    config.getOptimizationOptions().put("asmResolving", false);
+    @Test
+    public void testComplexTypeArguments() throws Exception {
+        String script = "def f(" + getClass().getName() + ".C1 c1) { }";
 
-    GroovyShell shell = new GroovyShell(config);
-    shell.evaluate(script, "bug7826.groovy");
-  }
+        CompilerConfiguration config = new CompilerConfiguration();
+        config.getOptimizationOptions().put("asmResolving", false);
 
-  public static class C1<T2 extends C2<T2, T1>, T1 extends C1<T2, T1>> {
-  }
+        GroovyShell shell = new GroovyShell(config);
+        shell.evaluate(script, "bug7826.groovy");
+    }
 
-  public static class C2<T2 extends C2<T2, T1>, T1 extends C1<T2, T1>> {
-  }
+    public static class C1<T2 extends C2<T2, T1>, T1 extends C1<T2, T1>> {
+    }
+
+    public static class C2<T2 extends C2<T2, T1>, T1 extends C1<T2, T1>> {
+    }
 }
diff --git a/src/test/org/codehaus/groovy/ast/Groovy9871.groovy b/src/test/org/codehaus/groovy/ast/Groovy9871.groovy
new file mode 100644
index 0000000000..8ff8d14fc5
--- /dev/null
+++ b/src/test/org/codehaus/groovy/ast/Groovy9871.groovy
@@ -0,0 +1,46 @@
+/*
+ *  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.ast
+
+import org.codehaus.groovy.ast.expr.AnnotationConstantExpression
+import org.codehaus.groovy.ast.expr.Expression
+import org.codehaus.groovy.ast.expr.ListExpression
+import org.junit.Test
+
+final class Groovy9871 {
+
+    @Test
+    void testAnnotationConstantExpression() {
+        ClassNode cn = new ClassNode(org.codehaus.groovy.runtime.ResourceGroovyMethods)
+        // method with @NamedParam annotations that should be wrapped in @NamedParams container
+        MethodNode mn = cn.getMethod('getText', new Parameter(ClassHelper.make(URL), 'url'), new Parameter(ClassHelper.MAP_TYPE, 'map'))
+
+        List<AnnotationNode> annotations = mn.parameters[1].annotations
+
+        assert annotations.size() == 1
+        assert annotations[0].classNode.name == 'groovy.transform.NamedParams'
+        assert annotations[0].members.value instanceof ListExpression
+
+        List<Expression> expressions = annotations[0].members.value.expressions
+
+        assert expressions.size() > 1 // 5 currently
+        assert expressions[0] instanceof AnnotationConstantExpression
+        assert expressions[0].type.name == 'groovy.transform.NamedParam'
+    }
+}


[groovy] 01/02: add `@since` tag

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 61ecf78b608696026a4f9e99f55ceee9a6cb4752
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jul 5 13:22:33 2022 -0500

    add `@since` tag
---
 src/main/java/groovy/transform/Generated.java | 6 ++++--
 src/main/java/groovy/transform/Internal.java  | 6 ++++--
 src/main/java/groovy/transform/Pure.java      | 8 +++++---
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/main/java/groovy/transform/Generated.java b/src/main/java/groovy/transform/Generated.java
index 98eaf5b0df..218e2b8bf9 100644
--- a/src/main/java/groovy/transform/Generated.java
+++ b/src/main/java/groovy/transform/Generated.java
@@ -24,9 +24,11 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 /**
- * The Generated annotation is used to mark members that have been generated.
+ * Marks class members that have been generated by the compiler.
+ *
+ * @since 2.5.0
  */
-@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE, ElementType.FIELD})
+@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Generated {
 }
diff --git a/src/main/java/groovy/transform/Internal.java b/src/main/java/groovy/transform/Internal.java
index a645f81c8c..8d0e3b6310 100644
--- a/src/main/java/groovy/transform/Internal.java
+++ b/src/main/java/groovy/transform/Internal.java
@@ -24,9 +24,11 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 /**
- * The Internal annotation is used to mark members that are internal.
+ * Marks class members that are intended for internal use.
+ *
+ * @since 2.5.3
  */
-@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE, ElementType.FIELD})
+@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Internal {
 }
diff --git a/src/main/java/groovy/transform/Pure.java b/src/main/java/groovy/transform/Pure.java
index 7827343c42..19c724f034 100644
--- a/src/main/java/groovy/transform/Pure.java
+++ b/src/main/java/groovy/transform/Pure.java
@@ -18,18 +18,20 @@
  */
 package groovy.transform;
 
+import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 /**
- * Mark the return value of "pure" method only comes from expressions involving constants or other pure methods
+ * Marks the return value of "pure" method only comes from expressions involving
+ * constants or other pure methods.
  *
  * @since 4.0.0
  */
-@java.lang.annotation.Documented
+@Documented
+@Target(ElementType.METHOD)
 @Retention(RetentionPolicy.RUNTIME)
-@Target({ElementType.METHOD})
 public @interface Pure {
 }