Repository: groovy Updated Branches: refs/heads/native-lambda 82b08ffa5 -> cd40603da
Trivial refactoring: Extract constants Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/cd40603d Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/cd40603d Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/cd40603d Branch: refs/heads/native-lambda Commit: cd40603da24d8f10e8889518863e39db31ef9de0 Parents: 82b08ff Author: sunlan <[email protected]> Authored: Thu Jan 18 11:16:49 2018 +0800 Committer: sunlan <[email protected]> Committed: Thu Jan 18 11:16:49 2018 +0800 ---------------------------------------------------------------------- .../groovy/classgen/asm/ClosureWriter.java | 11 ++++--- .../asm/sc/StaticTypesLambdaWriter.java | 7 +++-- src/test/groovy/transform/stc/LambdaTest.groovy | 33 ++++++++++++++++++-- 3 files changed, 42 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/cd40603d/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java index 9a4f200..defc7bf 100644 --- a/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java +++ b/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java @@ -65,6 +65,9 @@ import static org.objectweb.asm.Opcodes.NEW; public class ClosureWriter { + public static final String OUTER_INSTANCE = "_outerInstance"; + public static final String THIS_OBJECT = "_thisObject"; + protected interface UseExistingReference {} private final Map<Expression,ClassNode> closureClassMap; @@ -279,8 +282,8 @@ public class ClosureWriter { } Parameter[] params = new Parameter[2 + localVariableParams.length]; - params[0] = new Parameter(ClassHelper.OBJECT_TYPE, "_outerInstance"); - params[1] = new Parameter(ClassHelper.OBJECT_TYPE, "_thisObject"); + params[0] = new Parameter(ClassHelper.OBJECT_TYPE, OUTER_INSTANCE); + params[1] = new Parameter(ClassHelper.OBJECT_TYPE, THIS_OBJECT); System.arraycopy(localVariableParams, 0, params, 2, localVariableParams.length); ASTNode sn = answer.addConstructor(ACC_PUBLIC, params, ClassNode.EMPTY_ARRAY, block); @@ -295,10 +298,10 @@ public class ClosureWriter { BlockStatement block = new BlockStatement(); // this block does not get a source position, because we don't // want this synthetic constructor to show up in corbertura reports - VariableExpression outer = new VariableExpression("_outerInstance"); + VariableExpression outer = new VariableExpression(OUTER_INSTANCE); outer.setSourcePosition(expression); block.getVariableScope().putReferencedLocalVariable(outer); - VariableExpression thisObject = new VariableExpression("_thisObject"); + VariableExpression thisObject = new VariableExpression(THIS_OBJECT); thisObject.setSourcePosition(expression); block.getVariableScope().putReferencedLocalVariable(thisObject); TupleExpression conArgs = new TupleExpression(outer, thisObject); http://git-wip-us.apache.org/repos/asf/groovy/blob/cd40603d/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java index f7a6261..f30fbc2 100644 --- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java +++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java @@ -71,6 +71,7 @@ public class StaticTypesLambdaWriter extends LambdaWriter { private static final String LAMBDA_SHARED_VARIABLES = "__LAMBDA_SHARED_VARIABLES"; private static final String ENCLOSING_THIS = "__enclosing_this"; private static final String LAMBDA_THIS = "__lambda_this"; + public static final String INIT = "<init>"; private StaticTypesClosureWriter staticTypesClosureWriter; private WriterController controller; private WriterControllerFactory factory; @@ -148,7 +149,7 @@ public class StaticTypesLambdaWriter extends LambdaWriter { loadEnclosingClassInstance(); Parameter[] lambdaClassConstructorParameters = createConstructorParameters(); - mv.visitMethodInsn(INVOKESPECIAL, lambdaClassInternalName, "<init>", BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, lambdaClassConstructorParameters), lambdaClassNode.isInterface()); + mv.visitMethodInsn(INVOKESPECIAL, lambdaClassInternalName, INIT, BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, lambdaClassConstructorParameters), lambdaClassNode.isInterface()); controller.getOperandStack().replace(ClassHelper.LAMBDA_TYPE, lambdaClassConstructorParameters.length); } @@ -258,8 +259,8 @@ public class StaticTypesLambdaWriter extends LambdaWriter { private Parameter[] createConstructorParameters() { Parameter[] params = new Parameter[2]; - params[0] = new Parameter(ClassHelper.OBJECT_TYPE, "_outerInstance"); - params[1] = new Parameter(ClassHelper.OBJECT_TYPE, "_thisObject"); + params[0] = new Parameter(ClassHelper.OBJECT_TYPE, OUTER_INSTANCE); + params[1] = new Parameter(ClassHelper.OBJECT_TYPE, THIS_OBJECT); return params; } http://git-wip-us.apache.org/repos/asf/groovy/blob/cd40603d/src/test/groovy/transform/stc/LambdaTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy b/src/test/groovy/transform/stc/LambdaTest.groovy index afa6111..31d0060 100644 --- a/src/test/groovy/transform/stc/LambdaTest.groovy +++ b/src/test/groovy/transform/stc/LambdaTest.groovy @@ -20,6 +20,8 @@ package groovy.transform.stc class LambdaTest extends GroovyTestCase { + private static final boolean SKIP_ERRORS = true; + void testFunction() { assertScript ''' import groovy.transform.CompileStatic @@ -58,7 +60,7 @@ class LambdaTest extends GroovyTestCase { * Depends on fixing https://issues.apache.org/jira/browse/GROOVY-8445 */ void testBinaryOperator() { - if (true) return + if (SKIP_ERRORS) return // the test can pass only in dynamic mode now, it can not pass static type checking... @@ -139,7 +141,7 @@ TestScript0.groovy: 13: [Static type checking] - Cannot find matching method jav * Depends on fixing https://issues.apache.org/jira/browse/GROOVY-8445 */ void testUnaryOperator() { - if (true) return + if (SKIP_ERRORS) return /* FIXME TestScript0.groovy: 14: [Static type checking] - Cannot find matching method java.util.List#replaceAll(groovy.lang.Closure). Please check if the declared type is correct and if the method exists. @@ -376,4 +378,31 @@ TestScript0.groovy: 14: [Static type checking] - Cannot find matching method jav } ''' } + + void testFunctionCall() { + if (SKIP_ERRORS) return + + /* FIXME + [Static type checking] - Cannot find matching method java.lang.Object#plus(int). Please check if the declared type is correct and if the method exists. + @ line 13, column 35. + assert 2 == (e -> e + 1)(1) + */ + + assertScript ''' + import groovy.transform.CompileStatic + import java.util.stream.Collectors + import java.util.stream.Stream + + @CompileStatic + public class Test1 { + public static void main(String[] args) { + p(); + } + + public static void p() { + assert 2 == (e -> e + 1)(1) + } + } + ''' + } }
