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 2018/12/04 15:42:05 UTC

groovy git commit: Minor refactor `StaticTypesLambdaWriter` and remove useless code

Repository: groovy
Updated Branches:
  refs/heads/master ec52f9425 -> ea0418c72


Minor refactor `StaticTypesLambdaWriter` and remove useless code


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ea0418c7
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ea0418c7
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ea0418c7

Branch: refs/heads/master
Commit: ea0418c72f8b13ae99e231dcd6f8c9756f4b1f22
Parents: ec52f94
Author: Daniel Sun <su...@apache.org>
Authored: Tue Dec 4 23:41:29 2018 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Tue Dec 4 23:41:29 2018 +0800

----------------------------------------------------------------------
 .../asm/sc/StaticTypesLambdaWriter.java         | 69 ++++----------------
 1 file changed, 12 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ea0418c7/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 381262f..523c011 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
@@ -76,20 +76,16 @@ public class StaticTypesLambdaWriter extends LambdaWriter {
     private static final String LAMBDA_THIS = "__lambda_this";
     private static final String INIT = "<init>";
     private static final String IS_GENERATED_CONSTRUCTOR = "__IS_GENERATED_CONSTRUCTOR";
-    private StaticTypesClosureWriter staticTypesClosureWriter;
-    private WriterController controller;
-    private WriterControllerFactory factory;
+    private final StaticTypesClosureWriter staticTypesClosureWriter;
+    private final WriterController controller;
+    private final WriterControllerFactory factory;
     private final Map<Expression,ClassNode> lambdaClassMap = new HashMap<>();
 
     public StaticTypesLambdaWriter(WriterController wc) {
         super(wc);
         this.staticTypesClosureWriter = new StaticTypesClosureWriter(wc);
         this.controller = wc;
-        this.factory = new WriterControllerFactory() {
-            public WriterController makeController(final WriterController normalController) {
-                return controller;
-            }
-        };
+        this.factory = normalController -> controller;
     }
 
     @Override
@@ -126,47 +122,6 @@ public class StaticTypesLambdaWriter extends LambdaWriter {
                 createBootstrapMethodArguments(abstractMethodDesc, lambdaWrapperClassNode, syntheticLambdaMethodNode)
         );
         operandStack.replace(redirect, 2);
-
-        if (null != expression.getNodeMetaData(INFERRED_LAMBDA_TYPE)) {
-            // FIXME declaring variable whose initial value is a lambda, e.g. `Function<Integer, String> f = (Integer e) -> 'a' + e`
-            //       Groovy will `POP` automatically, use `DUP` to duplicate the element of operand stack:
-            /*
-                INVOKEDYNAMIC apply(LTest1$_p_lambda1;LTest1;)Ljava/util/function/Function; [
-                  // handle kind 0x6 : INVOKESTATIC
-                  java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
-                  // arguments:
-                  (Ljava/lang/Object;)Ljava/lang/Object;,
-                  // handle kind 0x5 : INVOKEVIRTUAL
-                  Test1$_p_lambda1.doCall(LTest1;Ljava/lang/Integer;)Ljava/lang/String;,
-                  (Ljava/lang/Integer;)Ljava/lang/String;
-                ]
-                DUP           <-------------- FIXME ADDED ON PURPOSE, WE SHOULD REMOVE IT AFTER FIND BETTER SOLUTION
-                ASTORE 0
-               L2
-                ALOAD 0
-                POP           <-------------- Since operand stack is not empty, the `POP`s are issued by `controller.getOperandStack().popDownTo(mark);` in the method `org.codehaus.groovy.classgen.asm.StatementWriter.writeExpressionStatement`, but when we try to `operandStack.pop();` instead of `mv.visitInsn(DUP);`, we will get AIOOBE...
-                POP
-            */
-
-            mv.visitInsn(DUP);
-
-            /*
-                org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
-                General error during class generation: size==0
-
-                java.lang.ArrayIndexOutOfBoundsException: size==0
-                    at org.codehaus.groovy.classgen.asm.OperandStack.getTopOperand(OperandStack.java:693)
-                    at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluateEqual(BinaryExpressionHelper.java:397)
-                    at org.codehaus.groovy.classgen.asm.sc.StaticTypesBinaryExpressionMultiTypeDispatcher.evaluateEqual(StaticTypesBinaryExpressionMultiTypeDispatcher.java:179)
-                    at org.codehaus.groovy.classgen.AsmClassGenerator.visitDeclarationExpression(AsmClassGenerator.java:694)
-                    at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:89)
-                    at org.codehaus.groovy.classgen.asm.StatementWriter.writeExpressionStatement(StatementWriter.java:633)
-                    at org.codehaus.groovy.classgen.AsmClassGenerator.visitExpressionStatement(AsmClassGenerator.java:681)
-                    at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42)
-                             */
-                //            operandStack.pop();
-        }
-
     }
 
     private ClassNode getLambdaType(LambdaExpression expression) {
@@ -333,7 +288,7 @@ public class StaticTypesLambdaWriter extends LambdaWriter {
         Parameter[] localVariableParameters = getLambdaSharedVariables(expression);
         removeInitialValues(localVariableParameters);
 
-        List<Parameter> methodParameterList = new LinkedList<Parameter>(Arrays.asList(parametersWithExactType));
+        List<Parameter> methodParameterList = new LinkedList<>(Arrays.asList(parametersWithExactType));
         prependEnclosingThis(methodParameterList);
 
         MethodNode methodNode =
@@ -373,15 +328,15 @@ public class StaticTypesLambdaWriter extends LambdaWriter {
             parameters = Parameter.EMPTY_ARRAY;
         }
 
-        for (int i = 0; i < parameters.length; i++) {
-            ClassNode inferredType = parameters[i].getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
+        for (Parameter parameter : parameters) {
+            ClassNode inferredType = parameter.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
 
             if (null == inferredType) {
                 continue;
             }
 
-            parameters[i].setType(inferredType);
-            parameters[i].setOriginType(inferredType);
+            parameter.setType(inferredType);
+            parameter.setOriginType(inferredType);
         }
 
         return parameters;
@@ -393,11 +348,11 @@ public class StaticTypesLambdaWriter extends LambdaWriter {
     }
 
     private static final class TransformationVisitor extends ClassCodeVisitorSupport {
-        private CorrectAccessedVariableVisitor correctAccessedVariableVisitor;
-        private Parameter enclosingThisParameter;
+        private final CorrectAccessedVariableVisitor correctAccessedVariableVisitor;
+        private final Parameter enclosingThisParameter;
 
         public TransformationVisitor(InnerClassNode icn, Parameter enclosingThisParameter) {
-            correctAccessedVariableVisitor = new CorrectAccessedVariableVisitor(icn);
+            this.correctAccessedVariableVisitor = new CorrectAccessedVariableVisitor(icn);
             this.enclosingThisParameter = enclosingThisParameter;
         }