groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] 07/20: Minor refactor and improve robustness
Date Mon, 08 Apr 2019 12:58:59 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 401427bef5afdc65ad33be42671f4721646a340b
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Wed Mar 6 23:56:59 2019 +0800

    Minor refactor and improve robustness
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 28 ++++++++++++++++------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 5534ebc..4deb064 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3618,9 +3618,21 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
     }
 
     private void inferMethodReferenceType(MethodCallExpression call, ClassNode receiver,
ArgumentListExpression argumentList) {
+        if (null == call) return;
+        if (null == receiver) return;
+        if (null == argumentList) return;
+
+        List<Expression> argumentExpressionList = argumentList.getExpressions();
+        if (null == argumentExpressionList) return;
+
+        boolean noMethodReferenceParams = argumentExpressionList.stream().noneMatch(e ->
e instanceof MethodReferenceExpression);
+        if (noMethodReferenceParams) {
+            return;
+        }
+
         MethodNode selectedMethod = call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
+        if (null == selectedMethod) return;
         Parameter[] parameters = selectedMethod.getParameters();
-        List<Expression> argumentExpressionList = argumentList.getExpressions();
 
         List<Integer> methodReferenceParamIndexList = new LinkedList<>();
         List<Expression> newArgumentExpressionList = new LinkedList<>();
@@ -3640,15 +3652,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
                 continue;
             }
 
-            Parameter[] newParameters = createParametersForLambdaExpression(paramType);
-            LambdaExpression lambdaExpression = new LambdaExpression(newParameters, block());
+            LambdaExpression constructedLambdaExpression = constructLambdaExpressionForMethodReference(paramType);
 
-            newArgumentExpressionList.add(lambdaExpression);
+            newArgumentExpressionList.add(constructedLambdaExpression);
             methodReferenceParamIndexList.add(i);
         }
 
-        if (methodReferenceParamIndexList.isEmpty()) return;
-
         visitMethodCallArguments(receiver, new ArgumentListExpression(newArgumentExpressionList),
true, selectedMethod);
 
         for (Integer methodReferenceParamIndex : methodReferenceParamIndexList) {
@@ -3658,7 +3667,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
         }
     }
 
-    private Parameter[] createParametersForLambdaExpression(ClassNode functionalInterfaceType)
{
+    private LambdaExpression constructLambdaExpressionForMethodReference(ClassNode paramType)
{
+        Parameter[] newParameters = createParametersForConstructedLambdaExpression(paramType);
+        return new LambdaExpression(newParameters, block());
+    }
+
+    private Parameter[] createParametersForConstructedLambdaExpression(ClassNode functionalInterfaceType)
{
         MethodNode abstractMethodNode = ClassHelper.findSAM(functionalInterfaceType);
         Parameter[] abstractMethodNodeParameters = abstractMethodNode.getParameters();
         if (null == abstractMethodNodeParameters) {


Mime
View raw message