You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/09/23 23:38:58 UTC

[groovy] 02/02: add explicit phase to prevent running in all phases

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

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

commit 41911a10d53eea938c57614222c82cfeba662f2b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Sep 23 12:53:46 2020 -0500

    add explicit phase to prevent running in all phases
    
    (cherry picked from commit 8e1aaf1675e8e00ce99d384557c9e524bde5be17)
---
 src/main/java/groovy/transform/ASTTest.java                    |  8 +++++---
 src/spec/test/TestingASTTransformsTest.groovy                  | 10 ++++++----
 src/test/groovy/bugs/Groovy9010.groovy                         |  6 +++---
 src/test/groovy/bugs/Groovy9691.groovy                         |  2 +-
 src/test/groovy/bugs/ImportNodeLineNumberTest.groovy           |  2 +-
 .../groovy/control/customizers/ImportCustomizerTest.groovy     |  4 ++--
 .../codehaus/groovy/transform/AutoFinalTransformTest.groovy    |  4 ++--
 7 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/src/main/java/groovy/transform/ASTTest.java b/src/main/java/groovy/transform/ASTTest.java
index 1779161..42b6205 100644
--- a/src/main/java/groovy/transform/ASTTest.java
+++ b/src/main/java/groovy/transform/ASTTest.java
@@ -44,10 +44,12 @@ import java.lang.annotation.Target;
  * {@code @Field int x}
  * </pre>
  *
- * The closure code is executed after the specified phase has completed. If no phase is selected, then the
- * code is executed after the {@link org.codehaus.groovy.control.CompilePhase#SEMANTIC_ANALYSIS semantic analysis} phase.
+ * The closure code is executed after the specified phase has completed. If no phase is provided, then the
+ * code is executed after the {@link org.codehaus.groovy.control.CompilePhase#SEMANTIC_ANALYSIS semantic analysis} phase
+ * and each subsequent phase.
+ * <p>
  * The <code>node</code> variable refers to the AST node where the AST test annotation is put. In the previous example,
- * it means that <i>node</i> refers to the declaration node (int x).
+ * it means that <i>node</i> refers to the declaration "int x".
  *
  * @since 2.0.0
  */
diff --git a/src/spec/test/TestingASTTransformsTest.groovy b/src/spec/test/TestingASTTransformsTest.groovy
index 5242ce3..58b0fb2 100644
--- a/src/spec/test/TestingASTTransformsTest.groovy
+++ b/src/spec/test/TestingASTTransformsTest.groovy
@@ -19,6 +19,7 @@
 import groovy.test.GroovyTestCase
 
 class TestingASTTransformsTest extends GroovyTestCase {
+
     void testNotYetImplemented() {
         assertScript '''// tag::notyetimplemented[]
 import groovy.test.GroovyTestCase
@@ -75,7 +76,7 @@ def p = new Person()
 import groovy.transform.ASTTest
 import groovy.transform.PackageScope
 
-@ASTTest(phase=SEMANTIC_ANALYSIS, value= {
+@ASTTest(phase=SEMANTIC_ANALYSIS, value={
     def nameNode = node.properties.find { it.name == 'name' }
     def ageNode = node.properties.find { it.name == 'age' }
     assert nameNode
@@ -91,6 +92,7 @@ class Person {
 def p = new Person()
 '''
     }
+
     void testASTTestWithForLoop() {
         assertScript '''
 // tag::asttest_forloop[]
@@ -101,7 +103,7 @@ import org.codehaus.groovy.ast.expr.DeclarationExpression
 import org.codehaus.groovy.ast.stmt.ForStatement
 
 class Something {
-    @ASTTest(phase=SEMANTIC_ANALYSIS, value= {
+    @ASTTest(phase=SEMANTIC_ANALYSIS, value={
         def forLoop = lookup('anchor')[0]
         assert forLoop instanceof ForStatement
         def decl = forLoop.collectionExpression.expressions[0]
@@ -163,11 +165,11 @@ import org.codehaus.groovy.ast.ClassNode
 import org.codehaus.groovy.control.CompilePhase
 
 @ASTTest(value={
-    if (compilePhase==CompilePhase.INSTRUCTION_SELECTION) {             // <1>
+    if (compilePhase == CompilePhase.INSTRUCTION_SELECTION) {           // <1>
         println "toString() was added at phase: ${added}"
         assert added == CompilePhase.CANONICALIZATION                   // <2>
     } else {
-        if (node.getDeclaredMethods('toString') && added==null) {       // <3>
+        if (node.getDeclaredMethods('toString') && added == null) {     // <3>
             added = compilePhase                                        // <4>
         }
     }
diff --git a/src/test/groovy/bugs/Groovy9010.groovy b/src/test/groovy/bugs/Groovy9010.groovy
index 8b360fc..fd5137a 100644
--- a/src/test/groovy/bugs/Groovy9010.groovy
+++ b/src/test/groovy/bugs/Groovy9010.groovy
@@ -31,7 +31,7 @@ final class Groovy9010 {
     @NotYetImplemented @Test
     void testASTTestWithArrowInClosure() {
         assertScript '''\
-            @groovy.transform.ASTTest({ -> assert true })
+            @groovy.transform.ASTTest(phase=SEMANTIC_ANALYSIS, value={ -> assert true })
             def x
         '''
     }
@@ -39,11 +39,11 @@ final class Groovy9010 {
     @Test
     void testASTTestWithNoSpacesInClosure() {
         assertScript '''
-            @groovy.transform.ASTTest({assert 1==1})
+            @groovy.transform.ASTTest(phase=SEMANTIC_ANALYSIS, value={assert 1==1})
             def x
         '''
         def err = shouldFail '''
-            @groovy.transform.ASTTest({assert 0==1})
+            @groovy.transform.ASTTest(phase=SEMANTIC_ANALYSIS, value={assert 0==1})
             def x
         '''
         assert err =~ /(?s)Assertion failed:.*assert 0==1.*false\b/
diff --git a/src/test/groovy/bugs/Groovy9691.groovy b/src/test/groovy/bugs/Groovy9691.groovy
index 6e179f2..5fd25e6 100644
--- a/src/test/groovy/bugs/Groovy9691.groovy
+++ b/src/test/groovy/bugs/Groovy9691.groovy
@@ -34,7 +34,7 @@ final class Groovy9691 {
             }
 
             sourceSets {
-                @groovy.transform.ASTTest({
+                @groovy.transform.ASTTest(phase=SEMANTIC_ANALYSIS, value={
                     def call = node.rightExpression
                     assert call instanceof MethodCall
                     assert !(call instanceof StaticMethodCallExpression)
diff --git a/src/test/groovy/bugs/ImportNodeLineNumberTest.groovy b/src/test/groovy/bugs/ImportNodeLineNumberTest.groovy
index bca576d..020b06a 100644
--- a/src/test/groovy/bugs/ImportNodeLineNumberTest.groovy
+++ b/src/test/groovy/bugs/ImportNodeLineNumberTest.groovy
@@ -31,7 +31,7 @@ final class ImportNodeLineNumberTest {
             import static java.lang.Math.*
             import static java.lang.Math.PI as pi
 
-            @ASTTest({
+            @ASTTest(phase=SEMANTIC_ANALYSIS, value={
                 def moduleNode = sourceUnit.AST
 
                 def importNode = moduleNode.getImport('Grab')
diff --git a/src/test/org/codehaus/groovy/control/customizers/ImportCustomizerTest.groovy b/src/test/org/codehaus/groovy/control/customizers/ImportCustomizerTest.groovy
index adcd0c4..4f61d2b 100644
--- a/src/test/org/codehaus/groovy/control/customizers/ImportCustomizerTest.groovy
+++ b/src/test/org/codehaus/groovy/control/customizers/ImportCustomizerTest.groovy
@@ -130,7 +130,7 @@ final class ImportCustomizerTest {
         importCustomizer.addImports('java.text.SimpleDateFormat')
         def shell = new GroovyShell(configuration)
         shell.evaluate('''\
-            @groovy.transform.ASTTest({
+            @groovy.transform.ASTTest(phase=SEMANTIC_ANALYSIS, value={
                 def imports = node.module.imports*.text
                 assert imports == ['import java.text.SimpleDateFormat as SimpleDateFormat']
             })
@@ -155,7 +155,7 @@ final class ImportCustomizerTest {
         importCustomizer.addStarImports('java.text', 'groovy.transform')
         def shell = new GroovyShell(configuration)
         shell.evaluate('''\
-            @groovy.transform.ASTTest({
+            @groovy.transform.ASTTest(phase=SEMANTIC_ANALYSIS, value={
                 def imports = node.module.starImports*.text
                 assert imports == ['import java.text.*', 'import groovy.transform.*']
             })
diff --git a/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy b/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
index 58b11f5..55c64ab 100644
--- a/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/AutoFinalTransformTest.groovy
@@ -31,7 +31,7 @@ class AutoFinalTransformTest extends CompilableTestSupport {
             import groovy.transform.*
             import static java.lang.reflect.Modifier.isFinal
 
-            @ASTTest({
+            @ASTTest(phase=SEMANTIC_ANALYSIS, value={
                 assert node.methods.size() == 1
                 node.methods[0].with {
                     assert it.name == 'fullName'
@@ -66,7 +66,7 @@ class AutoFinalTransformTest extends CompilableTestSupport {
             import groovy.transform.*
             import static java.lang.reflect.Modifier.isFinal
 
-            @ASTTest({
+            @ASTTest(phase=SEMANTIC_ANALYSIS, value={
                 assert node.methods.size() == 2
                 node.methods[0].with {
                     assert it.name == 'fullName'