groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 02/02: Minor refactoring: simplify code for getting CallType
Date Tue, 31 Dec 2019 16:08:23 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit d45538e9d0bccd66a271ebb614312b94fd187072
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Tue Dec 31 23:17:17 2019 +0800

    Minor refactoring: simplify code for getting CallType
    
    (cherry picked from commit 722e6afbac60494d7d0ad6aa5e3f26752186065c)
---
 .../codehaus/groovy/vmplugin/v7/IndyInterface.java | 38 ++++++++++++----------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyInterface.java b/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyInterface.java
index 75c2dcd..b545f65 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyInterface.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyInterface.java
@@ -29,8 +29,12 @@ import java.lang.invoke.MethodHandles.Lookup;
 import java.lang.invoke.MethodType;
 import java.lang.invoke.MutableCallSite;
 import java.lang.invoke.SwitchPoint;
+import java.util.Map;
+import java.util.function.Function;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * Bytecode level interface for bootstrap methods used by invokedynamic.
@@ -73,6 +77,10 @@ public class IndyInterface {
              * Cast invocation type
              */
             CAST("cast");
+
+            private static final Map<String, CallType> NAME_CALLTYPE_MAP =
+                    Stream.of(CallType.values()).collect(Collectors.toMap(CallType::getCallSiteName,
Function.identity()));
+
             /**
              * The name of the call site type
              */
@@ -88,6 +96,10 @@ public class IndyInterface {
             public String getCallSiteName() {
                 return name;
             }
+
+            public static CallType fromCallSiteName(String callSiteName) {
+                return NAME_CALLTYPE_MAP.get(callSiteName);
+            }
         }
 
         /** Logger */
@@ -159,23 +171,15 @@ public class IndyInterface {
          * @since Groovy 2.1.0
          */
         public static CallSite bootstrap(Lookup caller, String callType, MethodType type,
String name, int flags) {
-            boolean safe = (flags&SAFE_NAVIGATION)!=0;
-            boolean thisCall = (flags&THIS_CALL)!=0;
-            boolean spreadCall = (flags&SPREAD_CALL)!=0;
-            int callID;
-            if (callType.equals(CallType.METHOD.getCallSiteName())) {
-                callID = CallType.METHOD.ordinal();
-            } else if (callType.equals(CallType.INIT.getCallSiteName())) {
-                callID = CallType.INIT.ordinal();
-            } else if (callType.equals(CallType.GET.getCallSiteName())) {
-                callID = CallType.GET.ordinal();
-            } else if (callType.equals(CallType.SET.getCallSiteName())) {
-                callID = CallType.SET.ordinal();
-            } else if (callType.equals(CallType.CAST.getCallSiteName())) {
-                callID = CallType.CAST.ordinal();
-            }else {
-                throw new GroovyBugError("Unknown call type: "+callType);
-            }
+            boolean safe = (flags & SAFE_NAVIGATION) != 0;
+            boolean thisCall = (flags & THIS_CALL) != 0;
+            boolean spreadCall = (flags & SPREAD_CALL) != 0;
+
+            CallType ct = CallType.fromCallSiteName(callType);
+            if (null == ct) throw new GroovyBugError("Unknown call type: " + callType);
+
+            int callID = ct.ordinal();
+
             return realBootstrap(caller, name, callID, type, safe, thisCall, spreadCall);
         }
 


Mime
View raw message