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:51 UTC
[groovy] 01/02: GROOVY-10178: NullCheck & SC: set direct target on exception constructor
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.*