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 2020/02/03 07:32:10 UTC

[groovy] branch master updated (c6b8122 -> 6c9cbba)

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

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


    from c6b8122  Revert "GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module (module should be optional) (closes #1154)"
     new a4d414d  GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module (module should be optional) (closes #1154)
     new 6c9cbba  Revert "GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module"

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:
 gradle/upload.gradle                               |   1 +
 settings.gradle                                    |   3 +-
 .../ast/builder/AstBuilderTransformation.java      |   1 -
 ...org.codehaus.groovy.transform.ASTTransformation |   3 +
 src/spec/doc/core-domain-specific-languages.adoc   |   2 +-
 src/spec/test/CustomizersTest.groovy               |  19 +++
 .../test/groovy/bugs/Groovy4272Bug.groovy          |   0
 .../test/groovy/bugs/Groovy5025Bug.groovy          |   4 +-
 .../test/groovy/bugs/MyConstants4272.java          |   0
 .../bugs/MyConstantsASTTransformation4272.groovy   |   0
 .../groovy/ast/CodeVisitorSupportTest.groovy       |   0
 .../ast/builder/AstBuilderFromCodeTest.groovy      |   0
 .../builder/AstBuilderFromSpecificationTest.groovy |   0
 .../ast/builder/AstBuilderFromStringTest.groovy    |   0
 .../groovy/ast/builder/WithAstBuilder.groovy       |   0
 .../AstBuilderFromCodePackageImportTest.groovy     |   0
 .../ASTTransformationCustomizerTest.groovy         | 101 +++++++++++-
 subprojects/groovy-astbuilder/build.gradle         |  23 ---
 .../ast/builder/AstBuilderTransformation.java      | 183 ---------------------
 ...org.codehaus.groovy.transform.ASTTransformation |  17 --
 .../src/spec/test/CustomizersTest.groovy           |  45 -----
 .../ASTTransformationCustomizerTest.groovy         | 128 --------------
 22 files changed, 121 insertions(+), 409 deletions(-)
 rename {subprojects/groovy-astbuilder/src => src}/test/groovy/bugs/Groovy4272Bug.groovy (100%)
 rename {subprojects/groovy-astbuilder/src => src}/test/groovy/bugs/Groovy5025Bug.groovy (89%)
 rename {subprojects/groovy-astbuilder/src => src}/test/groovy/bugs/MyConstants4272.java (100%)
 rename {subprojects/groovy-astbuilder/src => src}/test/groovy/bugs/MyConstantsASTTransformation4272.groovy (100%)
 rename {subprojects/groovy-astbuilder/src/test/groovy => src/test}/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy (100%)
 rename {subprojects/groovy-astbuilder/src/test/groovy => src/test}/org/codehaus/groovy/ast/builder/AstBuilderFromCodeTest.groovy (100%)
 rename {subprojects/groovy-astbuilder/src/test/groovy => src/test}/org/codehaus/groovy/ast/builder/AstBuilderFromSpecificationTest.groovy (100%)
 rename {subprojects/groovy-astbuilder/src/test/groovy => src/test}/org/codehaus/groovy/ast/builder/AstBuilderFromStringTest.groovy (100%)
 rename {subprojects/groovy-astbuilder/src/test/groovy => src/test}/org/codehaus/groovy/ast/builder/WithAstBuilder.groovy (100%)
 rename {subprojects/groovy-astbuilder/src/test/groovy => src/test}/org/codehaus/groovy/ast/builder/testpackage/AstBuilderFromCodePackageImportTest.groovy (100%)
 delete mode 100644 subprojects/groovy-astbuilder/build.gradle
 delete mode 100644 subprojects/groovy-astbuilder/src/main/java/org/apache/groovy/ast/builder/AstBuilderTransformation.java
 delete mode 100644 subprojects/groovy-astbuilder/src/main/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
 delete mode 100644 subprojects/groovy-astbuilder/src/spec/test/CustomizersTest.groovy
 delete mode 100644 subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy


[groovy] 02/02: Revert "GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module"

Posted by pa...@apache.org.
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

commit 6c9cbbad4cffd4eaa3826f5a0736bc94382913d1
Author: Paul King <pa...@asert.com.au>
AuthorDate: Mon Feb 3 17:31:49 2020 +1000

    Revert "GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module"
    
    This reverts commit 995053bd89477e605637b26884adba5051141de4.
---
 settings.gradle                                    |   3 +-
 .../ast/builder/AstBuilderTransformation.java      |   1 -
 ...org.codehaus.groovy.transform.ASTTransformation |   3 +
 src/spec/doc/core-domain-specific-languages.adoc   |   2 +-
 src/spec/test/CustomizersTest.groovy               |  19 +++
 .../test/groovy/bugs/Groovy4272Bug.groovy          |   0
 .../test/groovy/bugs/Groovy5025Bug.groovy          |   4 +-
 .../test/groovy/bugs/MyConstants4272.java          |   0
 .../bugs/MyConstantsASTTransformation4272.groovy   |   0
 .../groovy/ast/CodeVisitorSupportTest.groovy       |   0
 .../ast/builder/AstBuilderFromCodeTest.groovy      |   0
 .../builder/AstBuilderFromSpecificationTest.groovy |   0
 .../ast/builder/AstBuilderFromStringTest.groovy    |   0
 .../groovy/ast/builder/WithAstBuilder.groovy       |   0
 .../AstBuilderFromCodePackageImportTest.groovy     |   0
 .../ASTTransformationCustomizerTest.groovy         | 101 +++++++++++-
 subprojects/groovy-astbuilder/build.gradle         |  23 ---
 .../ast/builder/AstBuilderTransformation.java      | 183 ---------------------
 ...org.codehaus.groovy.transform.ASTTransformation |  17 --
 .../src/spec/test/CustomizersTest.groovy           |  45 -----
 .../ASTTransformationCustomizerTest.groovy         | 128 --------------
 21 files changed, 120 insertions(+), 409 deletions(-)

diff --git a/settings.gradle b/settings.gradle
index 11455fe..5e23353 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -30,7 +30,6 @@ gradleEnterprise {
 }
 
 def subprojects = ['groovy-ant',
-        'groovy-astbuilder',
         'groovy-bsf',
         'groovy-cli-commons',
         'groovy-cli-picocli',
@@ -44,7 +43,6 @@ def subprojects = ['groovy-ant',
         'groovy-jmx',
         'groovy-json',
         'groovy-jsr223',
-        'groovy-macro',
         'groovy-nio',
         'groovy-servlet',
         'groovy-sql',
@@ -54,6 +52,7 @@ def subprojects = ['groovy-ant',
         'groovy-test-junit5',
         'groovy-testng',
         'groovy-xml',
+        'groovy-macro',
         'groovy-yaml',
         'performance',
         'binary-compatibility'
diff --git a/src/main/java/org/codehaus/groovy/ast/builder/AstBuilderTransformation.java b/src/main/java/org/codehaus/groovy/ast/builder/AstBuilderTransformation.java
index 6d9030c..be192f7 100644
--- a/src/main/java/org/codehaus/groovy/ast/builder/AstBuilderTransformation.java
+++ b/src/main/java/org/codehaus/groovy/ast/builder/AstBuilderTransformation.java
@@ -48,7 +48,6 @@ import java.util.List;
  * approach's responsibility to remove the BlockStatement created
  * by the label.
  */
-@Deprecated
 @GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
 public class AstBuilderTransformation extends MethodCallTransformation {
 
diff --git a/src/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation b/src/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
index 3836360..734b814 100644
--- a/src/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
+++ b/src/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
@@ -15,3 +15,6 @@
 
 # global transformation to handle @Grab annotation
 groovy.grape.GrabAnnotationTransformation
+
+#global transformation for AST Builder
+org.codehaus.groovy.ast.builder.AstBuilderTransformation
diff --git a/src/spec/doc/core-domain-specific-languages.adoc b/src/spec/doc/core-domain-specific-languages.adoc
index 7bf4c05..06ca6a0 100644
--- a/src/spec/doc/core-domain-specific-languages.adoc
+++ b/src/spec/doc/core-domain-specific-languages.adoc
@@ -699,7 +699,7 @@ give it a `ClosureExpression`, like in the following example:
 
 [source,groovy]
 --------------------------------------------------------------------------------------------------------------
-include::{projectdir}/subprojects/groovy-astbuilder/src/spec/test/CustomizersTest.groovy[tags=ast_cz_closure,indent=0]
+include::{projectdir}/src/spec/test/CustomizersTest.groovy[tags=ast_cz_closure,indent=0]
 --------------------------------------------------------------------------------------------------------------
 
 For a complete list of options, please refer to gapi:org.codehaus.groovy.control.customizers.ASTTransformationCustomizer[]
diff --git a/src/spec/test/CustomizersTest.groovy b/src/spec/test/CustomizersTest.groovy
index 6bf5fdf..27b0a3a 100644
--- a/src/spec/test/CustomizersTest.groovy
+++ b/src/spec/test/CustomizersTest.groovy
@@ -102,6 +102,25 @@ class CustomizersTest extends GroovyTestCase {
         '''
     }
 
+    void testAstTransformationCustomizerWithClosureExpression() {
+        // tag::ast_cz_closure[]
+        def configuration = new CompilerConfiguration()
+        def expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) { -> true }.expression[0]
+        def customizer = new ASTTransformationCustomizer(ConditionalInterrupt, value: expression, thrown: SecurityException)
+        configuration.addCompilationCustomizers(customizer)
+        def shell = new GroovyShell(configuration)
+        shouldFail(SecurityException) {
+            shell.evaluate("""
+                // equivalent to adding @ConditionalInterrupt(value={true}, thrown: SecurityException)
+                class MyClass {
+                    void doIt() { }
+                }
+                new MyClass().doIt()
+            """)
+        }
+        // end::ast_cz_closure[]
+    }
+
     void testSecureASTCustomizer() {
         // tag::secure_cz[]
         def scz = new SecureASTCustomizer()
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/bugs/Groovy4272Bug.groovy b/src/test/groovy/bugs/Groovy4272Bug.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/bugs/Groovy4272Bug.groovy
rename to src/test/groovy/bugs/Groovy4272Bug.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/bugs/Groovy5025Bug.groovy b/src/test/groovy/bugs/Groovy5025Bug.groovy
similarity index 89%
rename from subprojects/groovy-astbuilder/src/test/groovy/bugs/Groovy5025Bug.groovy
rename to src/test/groovy/bugs/Groovy5025Bug.groovy
index d724fad..5cc6cd3 100644
--- a/subprojects/groovy-astbuilder/src/test/groovy/bugs/Groovy5025Bug.groovy
+++ b/src/test/groovy/bugs/Groovy5025Bug.groovy
@@ -23,7 +23,7 @@ import groovy.test.GroovyTestCase
 class Groovy5025Bug extends GroovyTestCase {
     void testDisableAstBuilder() {
         def config = new org.codehaus.groovy.control.CompilerConfiguration()
-        config.disabledGlobalASTTransformations = ['org.apache.groovy.ast.builder.AstBuilderTransformation']
+        config.disabledGlobalASTTransformations = ['org.codehaus.groovy.ast.builder.AstBuilderTransformation']
         def script = '''
             new org.codehaus.groovy.ast.builder.AstBuilder().buildFromCode { "Hello" }
         '''
@@ -32,7 +32,7 @@ class Groovy5025Bug extends GroovyTestCase {
         assert shell.evaluate(script).class == ArrayList
 
         shell = new GroovyShell(config)
-        shouldFail(IllegalStateException) {
+        shouldFail {
             shell.evaluate(script)
         }
     }
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/bugs/MyConstants4272.java b/src/test/groovy/bugs/MyConstants4272.java
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/bugs/MyConstants4272.java
rename to src/test/groovy/bugs/MyConstants4272.java
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/bugs/MyConstantsASTTransformation4272.groovy b/src/test/groovy/bugs/MyConstantsASTTransformation4272.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/bugs/MyConstantsASTTransformation4272.groovy
rename to src/test/groovy/bugs/MyConstantsASTTransformation4272.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy b/src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy
rename to src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/AstBuilderFromCodeTest.groovy b/src/test/org/codehaus/groovy/ast/builder/AstBuilderFromCodeTest.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/AstBuilderFromCodeTest.groovy
rename to src/test/org/codehaus/groovy/ast/builder/AstBuilderFromCodeTest.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/AstBuilderFromSpecificationTest.groovy b/src/test/org/codehaus/groovy/ast/builder/AstBuilderFromSpecificationTest.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/AstBuilderFromSpecificationTest.groovy
rename to src/test/org/codehaus/groovy/ast/builder/AstBuilderFromSpecificationTest.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/AstBuilderFromStringTest.groovy b/src/test/org/codehaus/groovy/ast/builder/AstBuilderFromStringTest.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/AstBuilderFromStringTest.groovy
rename to src/test/org/codehaus/groovy/ast/builder/AstBuilderFromStringTest.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/WithAstBuilder.groovy b/src/test/org/codehaus/groovy/ast/builder/WithAstBuilder.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/WithAstBuilder.groovy
rename to src/test/org/codehaus/groovy/ast/builder/WithAstBuilder.groovy
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/testpackage/AstBuilderFromCodePackageImportTest.groovy b/src/test/org/codehaus/groovy/ast/builder/testpackage/AstBuilderFromCodePackageImportTest.groovy
similarity index 100%
rename from subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/ast/builder/testpackage/AstBuilderFromCodePackageImportTest.groovy
rename to src/test/org/codehaus/groovy/ast/builder/testpackage/AstBuilderFromCodePackageImportTest.groovy
diff --git a/src/test/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy b/src/test/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy
index b861ed8..90c252d 100644
--- a/src/test/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy
+++ b/src/test/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy
@@ -20,20 +20,29 @@ package org.codehaus.groovy.control.customizers
 
 import groovy.test.GroovyTestCase
 import groovy.transform.TimedInterrupt
-import groovy.util.logging.Log
-import org.codehaus.groovy.ast.ASTNode
 import org.codehaus.groovy.ast.ClassHelper
 import org.codehaus.groovy.ast.expr.ClassExpression
 import org.codehaus.groovy.ast.expr.PropertyExpression
-import org.codehaus.groovy.control.CompilePhase
 import org.codehaus.groovy.control.CompilerConfiguration
-import org.codehaus.groovy.control.SourceUnit
-import org.codehaus.groovy.transform.ASTTransformation
-import org.codehaus.groovy.transform.GroovyASTTransformation
+import groovy.util.logging.Log
 
 import java.util.concurrent.TimeUnit
-import java.util.concurrent.atomic.AtomicBoolean
 import java.util.logging.Logger
+import org.codehaus.groovy.transform.ASTTransformation
+import org.codehaus.groovy.transform.GroovyASTTransformation
+import org.codehaus.groovy.control.CompilePhase
+import java.util.concurrent.atomic.AtomicBoolean
+import org.codehaus.groovy.ast.ASTNode
+import org.codehaus.groovy.control.SourceUnit
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+import org.codehaus.groovy.transform.GroovyASTTransformationClass
+import java.lang.annotation.ElementType
+import java.lang.annotation.RetentionPolicy
+import org.codehaus.groovy.ast.ClassNode
+import org.objectweb.asm.Opcodes
+import org.codehaus.groovy.ast.builder.AstBuilder
+import groovy.transform.ConditionalInterrupt
 
 /**
  * Tests the {@link ASTTransformationCustomizer}.
@@ -88,6 +97,62 @@ class ASTTransformationCustomizerTest extends GroovyTestCase {
         }
     }
 
+    void testLocalTransformationWithClosureAnnotationParameter() {
+        // add @Contract({distance = 1 })
+        customizer = new ASTTransformationCustomizer(Contract)
+        final expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) {->
+            distance = 1
+        }.expression[0]
+        customizer.annotationParameters = [value: expression]
+        configuration.addCompilationCustomizers(customizer)
+        def shell = new GroovyShell(configuration)
+        def result = shell.evaluate("""
+            class MyClass {
+                int distance
+                MyClass() {}
+            }
+            new MyClass()
+        """)
+        assert result.distance == 1
+    }
+
+    void testLocalTransformationWithClosureAnnotationParameter_notAnnotatedAsASTInterface() {
+        // add @Contract2({distance = 1 })
+        customizer = new ASTTransformationCustomizer(Contract2, "org.codehaus.groovy.control.customizers.ContractAnnotation")
+        final expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) {->
+            distance = 1
+        }.expression[0]
+        customizer.annotationParameters = [value: expression]
+        configuration.addCompilationCustomizers(customizer)
+        def shell = new GroovyShell(configuration)
+        def result = shell.evaluate("""
+            class MyClass {
+                int distance
+                MyClass() {}
+            }
+            new MyClass()
+        """)
+        assert result.distance == 1
+    }
+
+    void testLocalTransformationWithClassAnnotationParameter() {
+        // add @ConditionalInterrupt(value={ true }, thrown=SecurityException)
+        final expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) {->
+            true
+        }.expression[0]
+        customizer = new ASTTransformationCustomizer(ConditionalInterrupt, value:expression, thrown:SecurityException)
+        configuration.addCompilationCustomizers(customizer)
+        def shell = new GroovyShell(configuration)
+        shouldFail(SecurityException) {
+            shell.evaluate("""
+                class MyClass {
+                    void doIt() { }
+                }
+                new MyClass().doIt()
+            """)
+        }
+    }
+
     void testGlobalTransformation() {
         final TestTransformation transformation = new TestTransformation()
         customizer = new ASTTransformationCustomizer(transformation)
@@ -153,3 +218,25 @@ interrupted'''
     }
 
 }
+
+@Retention(RetentionPolicy.SOURCE)
+@Target([ElementType.TYPE])
+@GroovyASTTransformationClass("org.codehaus.groovy.control.customizers.ContractAnnotation")
+protected @interface Contract {
+    Class value();
+}
+
+@GroovyASTTransformation(phase=CompilePhase.CONVERSION)
+protected class ContractAnnotation implements ASTTransformation, Opcodes {
+    void visit(ASTNode[] nodes, SourceUnit source) {
+        def node = nodes[0]
+        def member = node.getMember("value")
+        ((ClassNode)nodes[1]).getDeclaredConstructors()[0].code = member.code
+    }
+}
+
+@Retention(RetentionPolicy.SOURCE)
+@Target([ElementType.TYPE])
+protected @interface Contract2 {
+    Class value();
+}
diff --git a/subprojects/groovy-astbuilder/build.gradle b/subprojects/groovy-astbuilder/build.gradle
deleted file mode 100644
index 1e6b820..0000000
--- a/subprojects/groovy-astbuilder/build.gradle
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- *  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.
- */
-dependencies {
-    implementation rootProject
-    testImplementation project(':groovy-test')
-    testImplementation rootProject.sourceSets.test.runtimeClasspath
-}
diff --git a/subprojects/groovy-astbuilder/src/main/java/org/apache/groovy/ast/builder/AstBuilderTransformation.java b/subprojects/groovy-astbuilder/src/main/java/org/apache/groovy/ast/builder/AstBuilderTransformation.java
deleted file mode 100644
index 63a6555..0000000
--- a/subprojects/groovy-astbuilder/src/main/java/org/apache/groovy/ast/builder/AstBuilderTransformation.java
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- *  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.apache.groovy.ast.builder;
-
-import org.codehaus.groovy.ast.ASTNode;
-import org.codehaus.groovy.ast.GroovyCodeVisitor;
-import org.codehaus.groovy.ast.ImportNode;
-import org.codehaus.groovy.ast.MethodCallTransformation;
-import org.codehaus.groovy.ast.MethodInvocationTrap;
-import org.codehaus.groovy.ast.expr.ArgumentListExpression;
-import org.codehaus.groovy.ast.expr.ClosureExpression;
-import org.codehaus.groovy.ast.expr.ConstantExpression;
-import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.ast.expr.MethodCallExpression;
-import org.codehaus.groovy.ast.expr.TupleExpression;
-import org.codehaus.groovy.control.CompilePhase;
-import org.codehaus.groovy.control.SourceUnit;
-import org.codehaus.groovy.control.io.ReaderSource;
-import org.codehaus.groovy.transform.GroovyASTTransformation;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Transformation to capture ASTBuilder from code statements.
- * <p>
- * The AstBuilder "from code" approach is used with a single Closure
- * parameter. This transformation converts the ClosureExpression back
- * into source code and rewrites the AST so that the "from string"
- * builder is invoked on the source. In order for this to work, the
- * closure source must be given a goto label. It is the "from string"
- * approach's responsibility to remove the BlockStatement created
- * by the label.
- */
-@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
-public class AstBuilderTransformation extends MethodCallTransformation {
-
-    @Override
-    protected GroovyCodeVisitor getTransformer(ASTNode[] nodes, SourceUnit sourceUnit) {
-        // todo : are there other import types that can be specified?
-        return new AstBuilderInvocationTrap(
-            sourceUnit.getAST().getImports(),
-            sourceUnit.getAST().getStarImports(),
-            sourceUnit.getSource(),
-            sourceUnit
-        );
-    }
-
-    /**
-     * This class traps invocations of AstBuilder.build(CompilePhase, boolean, Closure) and converts
-     * the contents of the closure into expressions by reading the source of the Closure and sending
-     * that as a String to AstBuilder.build(String, CompilePhase, boolean) at runtime.
-     */
-    private static class AstBuilderInvocationTrap extends MethodInvocationTrap {
-
-        private final List<String> factoryTargets = new ArrayList<>();
-
-        /**
-         * Creates the trap and captures all the ways in which a class may be referenced via imports.
-         *
-         * @param imports        all the imports from the source
-         * @param importPackages all the imported packages from the source
-         * @param source         the reader source that contains source for the SourceUnit
-         * @param sourceUnit     the source unit being compiled. Used for error messages.
-         */
-        AstBuilderInvocationTrap(List<ImportNode> imports, List<ImportNode> importPackages, ReaderSource source, SourceUnit sourceUnit) {
-            super(source, sourceUnit);
-
-            // factory type may be references as fully qualified, an import, or an alias
-            factoryTargets.add("org.codehaus.groovy.ast.builder.AstBuilder");//default package
-
-            if (imports != null) {
-                for (ImportNode importStatement : imports) {
-                    if ("org.codehaus.groovy.ast.builder.AstBuilder".equals(importStatement.getType().getName())) {
-                        factoryTargets.add(importStatement.getAlias());
-                    }
-                }
-            }
-
-            if (importPackages != null) {
-                for (ImportNode importPackage : importPackages) {
-                    if ("org.codehaus.groovy.ast.builder.".equals(importPackage.getPackageName())) {
-                        factoryTargets.add("AstBuilder");
-                        break;
-                    }
-                }
-            }
-        }
-        
-        @Override
-        protected boolean handleTargetMethodCallExpression(MethodCallExpression call) {
-            ClosureExpression closureExpression = getClosureArgument(call);
-            List<Expression> otherArgs = getNonClosureArguments(call);
-            String source = convertClosureToSource(closureExpression);
-
-            // parameter order is build(CompilePhase, boolean, String)
-            otherArgs.add(new ConstantExpression(source));
-            call.setArguments(new ArgumentListExpression(otherArgs));
-            call.setMethod(new ConstantExpression("buildFromBlock"));
-            call.setSpreadSafe(false);
-            call.setSafe(false);
-            call.setImplicitThis(false);
-            
-            return false;
-        }
-
-        private static List<Expression> getNonClosureArguments(MethodCallExpression call) {
-            List<Expression> result = new ArrayList<>();
-            if (call.getArguments() instanceof TupleExpression) {
-                for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) {
-                    if (!(node instanceof ClosureExpression)) {
-                        result.add((Expression) node);
-                    }
-                }
-            }
-            return result;
-        }
-
-        private static ClosureExpression getClosureArgument(MethodCallExpression call) {
-
-            if (call.getArguments() instanceof TupleExpression) {
-                for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) {
-                    if (node instanceof ClosureExpression) {
-                        return (ClosureExpression) node;
-                    }
-                }
-            }
-            return null;
-        }
-
-        /**
-         * Looks for method calls on the AstBuilder class called build that take
-         * a Closure as parameter. This is all needed b/c build is overloaded.
-         *
-         * @param call the method call expression, may not be null
-         */
-        @Override
-        protected boolean isBuildInvocation(MethodCallExpression call) {
-            if (call == null) throw new IllegalArgumentException("Null: call");
-
-            // is method name correct?
-            if (call.getMethod() instanceof ConstantExpression && "buildFromCode".equals(((ConstantExpression) call.getMethod()).getValue())) {
-
-                // is method object correct type?
-                if (call.getObjectExpression() != null && call.getObjectExpression().getType() != null) {
-                    String name = call.getObjectExpression().getType().getName();
-                    if (name != null && !"".equals(name) && factoryTargets.contains(name)) {
-
-                        // is one of the arguments a closure?
-                        if (call.getArguments() != null && call.getArguments() instanceof TupleExpression) {
-                            if (((TupleExpression) call.getArguments()).getExpressions() != null) {
-                                for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) {
-                                    if (node instanceof ClosureExpression) {
-                                        return true;
-                                    }
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-            return false;
-        }
-    }
-}
-
-
diff --git a/subprojects/groovy-astbuilder/src/main/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation b/subprojects/groovy-astbuilder/src/main/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
deleted file mode 100644
index 94f88b4..0000000
--- a/subprojects/groovy-astbuilder/src/main/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
+++ /dev/null
@@ -1,17 +0,0 @@
-# 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.
-
-#global transformation for AST Builder
-org.apache.groovy.ast.builder.AstBuilderTransformation
diff --git a/subprojects/groovy-astbuilder/src/spec/test/CustomizersTest.groovy b/subprojects/groovy-astbuilder/src/spec/test/CustomizersTest.groovy
deleted file mode 100644
index 4615787..0000000
--- a/subprojects/groovy-astbuilder/src/spec/test/CustomizersTest.groovy
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *  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.
- */
-import groovy.test.GroovyTestCase
-import groovy.transform.ConditionalInterrupt
-import org.codehaus.groovy.ast.builder.AstBuilder
-import org.codehaus.groovy.control.CompilePhase
-import org.codehaus.groovy.control.CompilerConfiguration
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
-
-class CustomizersTest extends GroovyTestCase {
-    void testAstTransformationCustomizerWithClosureExpression() {
-        // tag::ast_cz_closure[]
-        def configuration = new CompilerConfiguration()
-        def expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) { -> true }.expression[0]
-        def customizer = new ASTTransformationCustomizer(ConditionalInterrupt, value: expression, thrown: SecurityException)
-        configuration.addCompilationCustomizers(customizer)
-        def shell = new GroovyShell(configuration)
-        shouldFail(SecurityException) {
-            shell.evaluate("""
-                // equivalent to adding @ConditionalInterrupt(value={true}, thrown: SecurityException)
-                class MyClass {
-                    void doIt() { }
-                }
-                new MyClass().doIt()
-            """)
-        }
-        // end::ast_cz_closure[]
-    }
-}
diff --git a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy b/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy
deleted file mode 100644
index f378ce1..0000000
--- a/subprojects/groovy-astbuilder/src/test/groovy/org/codehaus/groovy/control/customizers/ASTTransformationCustomizerTest.groovy
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- *  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.control.customizers
-
-import groovy.test.GroovyTestCase
-import groovy.transform.ConditionalInterrupt
-import org.codehaus.groovy.ast.ASTNode
-import org.codehaus.groovy.ast.ClassNode
-import org.codehaus.groovy.ast.builder.AstBuilder
-import org.codehaus.groovy.control.CompilePhase
-import org.codehaus.groovy.control.CompilerConfiguration
-import org.codehaus.groovy.control.SourceUnit
-import org.codehaus.groovy.transform.ASTTransformation
-import org.codehaus.groovy.transform.GroovyASTTransformation
-import org.codehaus.groovy.transform.GroovyASTTransformationClass
-import org.objectweb.asm.Opcodes
-
-import java.lang.annotation.ElementType
-import java.lang.annotation.Retention
-import java.lang.annotation.RetentionPolicy
-import java.lang.annotation.Target
-
-/**
- * Tests the {@link ASTTransformationCustomizer} for cases which rely on AST Builder.
- */
-class ASTTransformationCustomizerTest extends GroovyTestCase {
-    CompilerConfiguration configuration
-    ASTTransformationCustomizer customizer
-
-    void setUp() {
-        configuration = new CompilerConfiguration()
-    }
-
-    void testLocalTransformationWithClosureAnnotationParameter() {
-        // add @Contract({distance = 1 })
-        customizer = new ASTTransformationCustomizer(Contract)
-        final expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) {->
-            distance = 1
-        }.expression[0]
-        customizer.annotationParameters = [value: expression]
-        configuration.addCompilationCustomizers(customizer)
-        def shell = new GroovyShell(configuration)
-        def result = shell.evaluate("""
-            class MyClass {
-                int distance
-                MyClass() {}
-            }
-            new MyClass()
-        """)
-        assert result.distance == 1
-    }
-
-    void testLocalTransformationWithClosureAnnotationParameter_notAnnotatedAsASTInterface() {
-        // add @Contract2({distance = 1 })
-        customizer = new ASTTransformationCustomizer(Contract2, "org.codehaus.groovy.control.customizers.ContractAnnotation")
-        final expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) {->
-            distance = 1
-        }.expression[0]
-        customizer.annotationParameters = [value: expression]
-        configuration.addCompilationCustomizers(customizer)
-        def shell = new GroovyShell(configuration)
-        def result = shell.evaluate("""
-            class MyClass {
-                int distance
-                MyClass() {}
-            }
-            new MyClass()
-        """)
-        assert result.distance == 1
-    }
-
-    void testLocalTransformationWithClassAnnotationParameter() {
-        // add @ConditionalInterrupt(value={ true }, thrown=SecurityException)
-        final expression = new AstBuilder().buildFromCode(CompilePhase.CONVERSION) {->
-            true
-        }.expression[0]
-        customizer = new ASTTransformationCustomizer(ConditionalInterrupt, value:expression, thrown:SecurityException)
-        configuration.addCompilationCustomizers(customizer)
-        def shell = new GroovyShell(configuration)
-        shouldFail(SecurityException) {
-            shell.evaluate("""
-                class MyClass {
-                    void doIt() { }
-                }
-                new MyClass().doIt()
-            """)
-        }
-    }
-
-}
-
-@Retention(RetentionPolicy.SOURCE)
-@Target([ElementType.TYPE])
-@GroovyASTTransformationClass("org.codehaus.groovy.control.customizers.ContractAnnotation")
-protected @interface Contract {
-    Class value();
-}
-
-@GroovyASTTransformation(phase=CompilePhase.CONVERSION)
-protected class ContractAnnotation implements ASTTransformation, Opcodes {
-    void visit(ASTNode[] nodes, SourceUnit source) {
-        def node = nodes[0]
-        def member = node.getMember("value")
-        ((ClassNode)nodes[1]).getDeclaredConstructors()[0].code = member.code
-    }
-}
-
-@Retention(RetentionPolicy.SOURCE)
-@Target([ElementType.TYPE])
-protected @interface Contract2 {
-    Class value();
-}


[groovy] 01/02: GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module (module should be optional) (closes #1154)

Posted by pa...@apache.org.
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

commit a4d414d5d4899388b6adaf4908080fce8e1a59cb
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Feb 1 11:12:39 2020 +1000

    GROOVY-4694: Move AstBuilderTransformation Global xForm to separate module (module should be optional) (closes #1154)
---
 gradle/upload.gradle | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gradle/upload.gradle b/gradle/upload.gradle
index 937f2b8..3bee9b2 100644
--- a/gradle/upload.gradle
+++ b/gradle/upload.gradle
@@ -199,6 +199,7 @@ def configureAdditionalArtifacts = {
 }
 
 def optionalModules = [
+        'groovy-astbuilder',
         'groovy-bsf',
         'groovy-cli-commons',
         'groovy-dateutil',