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/01/30 18:53:50 UTC

[groovy] branch GROOVY_3_0_X updated (35585bb -> 7d21ea5)

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

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


    from 35585bb  GROOVY-10051: STC: fully resolve "T extends Number" to Number not Object
     new a1bb1d4  GROOVY-10178: NullCheck & SC: set direct target on exception constructor
     new 7d21ea5  GROOVY-10184: fix for NPE

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:
 .../control/customizers/SecureASTCustomizer.java   |  2 +-
 .../transform/NullCheckASTTransformation.java      | 13 +++--
 .../customizers/SecureASTCustomizerTest.groovy     | 32 +++++++-----
 .../groovy/transform/NullCheckTransformTest.groovy | 60 ++++++++++++++++++++--
 4 files changed, 87 insertions(+), 20 deletions(-)

[groovy] 01/02: GROOVY-10178: NullCheck & SC: set direct target on exception constructor

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

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

commit a1bb1d4d9adde3921af128012168c90187580aa7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Sep 28 11:03:44 2021 -0500

    GROOVY-10178: NullCheck & SC: set direct target on exception constructor
---
 .../transform/NullCheckASTTransformation.java      | 13 +++--
 .../groovy/transform/NullCheckTransformTest.groovy | 60 ++++++++++++++++++++--
 2 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/NullCheckASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/NullCheckASTTransformation.java
index 051998c..3f1a2da 100644
--- a/src/main/java/org/codehaus/groovy/transform/NullCheckASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/NullCheckASTTransformation.java
@@ -41,22 +41,25 @@ import java.util.List;
 
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.isGenerated;
 import static org.apache.groovy.ast.tools.MethodNodeUtils.getCodeAsBlock;
-import static org.codehaus.groovy.ast.ClassHelper.make;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.isNullX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.throwS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
+import static org.codehaus.groovy.transform.stc.StaticTypesMarker.DIRECT_METHOD_CALL_TARGET;
 
 /**
  * Handles generation of code for the @NullCheck annotation.
  */
 @GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION)
 public class NullCheckASTTransformation extends AbstractASTTransformation {
-    public static final ClassNode NULL_CHECK_TYPE = make(NullCheck.class);
+    public static final ClassNode NULL_CHECK_TYPE = ClassHelper.make(NullCheck.class);
     private static final String NULL_CHECK_NAME = "@" + NULL_CHECK_TYPE.getNameWithoutPackage();
     private static final ClassNode EXCEPTION = ClassHelper.make(IllegalArgumentException.class);
+    private static final ConstructorNode EXCEPTION_STRING_CTOR = EXCEPTION.getDeclaredConstructor(params(param(ClassHelper.STRING_TYPE, "s")));
     private static final String NULL_CHECK_IS_PROCESSED = "NullCheck.isProcessed";
 
     @Override
@@ -124,8 +127,10 @@ public class NullCheckASTTransformation extends AbstractASTTransformation {
         mn.setCode(newCode);
     }
 
-    public static ThrowStatement makeThrowStmt(String name) {
-        return throwS(ctorX(EXCEPTION, constX(name + " cannot be null")));
+    public static ThrowStatement makeThrowStmt(final String variableName) {
+        ConstructorCallExpression newException = ctorX(EXCEPTION, constX(variableName + " cannot be null"));
+        newException.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, EXCEPTION_STRING_CTOR); // GROOVY-10178
+        return throwS(newException);
     }
 
     /**
diff --git a/src/test/org/codehaus/groovy/transform/NullCheckTransformTest.groovy b/src/test/org/codehaus/groovy/transform/NullCheckTransformTest.groovy
index 75c4133..107b9b4 100644
--- a/src/test/org/codehaus/groovy/transform/NullCheckTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/NullCheckTransformTest.groovy
@@ -18,14 +18,16 @@
  */
 package org.codehaus.groovy.transform
 
-import groovy.test.GroovyTestCase
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
 
 /**
  * Tests for the {@code @NullCheck} AST transform.
  */
-class NullCheckTransformTest extends GroovyTestCase {
+final class NullCheckTransformTest {
 
-    // GROOVY-9406
+    @Test // GROOVY-9406
     void testNullCheckDoesNotConflictWithGeneratedConstructors() {
         assertScript '''
             import groovy.transform.*
@@ -58,6 +60,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckWithIncludeGenerated() {
         assertScript '''
             import groovy.transform.*
@@ -87,6 +90,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckWithEquals() {
         assertScript '''
             import groovy.transform.*
@@ -112,6 +116,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckMethodWithDefaultValues() {
         assertScript '''
             import groovy.transform.*
@@ -136,6 +141,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckMethodWithNullDefaultValue() {
         assertScript '''
             import groovy.transform.*
@@ -159,6 +165,51 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
+    void testNullCheckWithCompileStatic1() {
+        assertScript '''
+            import groovy.transform.*
+            import static groovy.test.GroovyAssert.shouldFail
+
+            @CompileStatic @NullCheck
+            class Pogo {
+                Pogo() {}
+                Pogo(p) {}
+                void m(p) {}
+            }
+
+            new Pogo()
+            new Pogo('')
+            new Pogo().m('')
+            shouldFail(IllegalArgumentException) { new Pogo(null) }
+            shouldFail(IllegalArgumentException) { new Pogo().m(null) }
+        '''
+    }
+
+    @Test // GROOVY-10178
+    void testNullCheckWithCompileStatic2() {
+        assertScript '''
+            import groovy.transform.*
+            import static groovy.test.GroovyAssert.shouldFail
+
+            @CompileStatic
+            class Pogo {
+                Pogo() {}
+                @NullCheck
+                Pogo(p) {}
+                @NullCheck
+                void m(p) {}
+            }
+
+            new Pogo()
+            new Pogo('x')
+            new Pogo('x').m('x')
+            shouldFail(IllegalArgumentException) { new Pogo(null) }
+            shouldFail(IllegalArgumentException) { new Pogo('x').m(null) }
+        '''
+    }
+
+    @Test
     void testNullCheckWithImmutable1() {
         assertScript '''
             import groovy.transform.*
@@ -192,6 +243,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckWithImmutable2() {
         assertScript '''
             import groovy.transform.*
@@ -225,6 +277,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckWithImmutable3() {
         assertScript '''
             import groovy.transform.*
@@ -258,6 +311,7 @@ class NullCheckTransformTest extends GroovyTestCase {
         '''
     }
 
+    @Test
     void testNullCheckWithImmutable4() {
         assertScript '''
             import groovy.transform.*

[groovy] 02/02: GROOVY-10184: fix for NPE

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

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

commit 7d21ea5eec724c088970f3e104c195c3f8822b78
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Sep 28 09:47:54 2021 -0500

    GROOVY-10184: fix for NPE
---
 .../control/customizers/SecureASTCustomizer.java   |  2 +-
 .../customizers/SecureASTCustomizerTest.groovy     | 32 ++++++++++++++--------
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java b/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
index ad8ed7a..74594d2 100644
--- a/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
+++ b/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
@@ -974,7 +974,7 @@ public class SecureASTCustomizer extends CompilationCustomizer {
     }
 
     protected void assertStaticImportIsAllowed(final String member, final String className) {
-        final String fqn = member.equals(className) ? member : className + "." + member;
+        final String fqn = className.equals(member) ? className : className + "." + member;
         if (allowedStaticImports != null && !allowedStaticImports.contains(fqn)) {
             if (allowedStaticStarImports != null) {
                 // we should now check if the import is in the star imports
diff --git a/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy b/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
index 973c00b..1edb459 100644
--- a/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
+++ b/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
@@ -357,7 +357,7 @@ final class SecureASTCustomizerTest {
     }
 
     @Test
-    void testAllowedIndirectStarImports() {
+    void testAllowedIndirectStarImports1() {
         customizer.allowedStarImports = ['java.util.*']
         customizer.indirectImportCheckEnabled = true
         def shell = new GroovyShell(configuration)
@@ -380,6 +380,25 @@ final class SecureASTCustomizerTest {
         }
     }
 
+    @Test // GROOVY-8135
+    void testAllowedIndirectStarImports2() {
+        customizer.allowedStarImports = ['java.lang']
+        customizer.indirectImportCheckEnabled = true
+        def shell = new GroovyShell(configuration)
+        shell.evaluate('Object object = new Object()')
+        shell.evaluate('Object object = new Object(); object.hashCode()')
+        shell.evaluate('Object[] array = new Object[0]; array.size()')
+        shell.evaluate('Object[][] array = new Object[0][0]; array.size()')
+    }
+
+    @Test // GROOVY-10184
+    void testAllowedIndirectStarImports3() {
+        customizer.allowedStarImports = ['java.lang.*']
+        customizer.indirectImportCheckEnabled = true
+        def shell = new GroovyShell(configuration)
+        shell.evaluate('def obj = new Object(); def method = "hashCode"; obj."${method}"()')
+    }
+
     @Test
     void testAllowedStaticImports() {
         customizer.allowedStaticImports = ['java.lang.Math.PI']
@@ -601,15 +620,4 @@ final class SecureASTCustomizerTest {
             '''
         }
     }
-
-    @Test // GROOVY-8135
-    void testStarImportsAllowedListWithIndirectImportCheckEnabled() {
-        customizer.indirectImportCheckEnabled = true
-        customizer.allowedStarImports = ['java.lang']
-        def shell = new GroovyShell(configuration)
-        shell.evaluate('Object object = new Object()')
-        shell.evaluate('Object object = new Object(); object.hashCode()')
-        shell.evaluate('Object[] array = new Object[0]; array.size()')
-        shell.evaluate('Object[][] array = new Object[0][0]; array.size()')
-    }
 }