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 2017/12/06 12:13:03 UTC

[2/4] groovy git commit: Minor refactoring

Minor refactoring

(cherry picked from commit 5d2fcc6)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 2ee2a4ad4535b9f3f559cd0b45508caa8ad81e30
Parents: e988957
Author: sunlan <su...@apache.org>
Authored: Sat Dec 2 15:44:47 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 2 16:24:34 2017 +0800

----------------------------------------------------------------------
 src/main/groovy/lang/GroovyShell.java           | 14 +------------
 .../codehaus/groovy/runtime/InvokerHelper.java  | 22 +++++++++++++-------
 2 files changed, 15 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/2ee2a4ad/src/main/groovy/lang/GroovyShell.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyShell.java b/src/main/groovy/lang/GroovyShell.java
index 7e2b326..b2a0a30 100644
--- a/src/main/groovy/lang/GroovyShell.java
+++ b/src/main/groovy/lang/GroovyShell.java
@@ -259,24 +259,12 @@ public class GroovyShell extends GroovyObjectSupport {
         if (Script.class.isAssignableFrom(scriptClass)) {
             // treat it just like a script if it is one
             try {
-                Constructor constructor = scriptClass.getConstructor(Binding.class);
-                Script script = (Script) constructor.newInstance(context);
+                Script script = InvokerHelper.newScript(scriptClass, context);
                 return script.run();
             } catch (InstantiationException e) {
                 // ignore instantiation errors,, try to do main
             } catch (IllegalAccessException e) {
                // ignore instantiation errors, try to do main
-            } catch (NoSuchMethodException e) {
-                try {
-                    // Fallback for non-standard "Scripts" that don't have contextual constructor.
-                    Script script = (Script) scriptClass.newInstance();
-                    script.setBinding(context);
-                    return script.run();
-                } catch (InstantiationException e1) {
-                    // ignore instantiation errors, try to do main
-                } catch (IllegalAccessException e1) {
-                    // ignore instantiation errors, try to do main
-                }
             } catch (InvocationTargetException e) {
                 // ignore instantiation errors, try to do main
             }

http://git-wip-us.apache.org/repos/asf/groovy/blob/2ee2a4ad/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index f3471c1..d8f1b37 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -442,14 +442,7 @@ public class InvokerHelper {
         } else {
             try {
                 if (Script.class.isAssignableFrom(scriptClass)) {
-                    try {
-                        Constructor constructor = scriptClass.getConstructor(Binding.class);
-                        script = (Script) constructor.newInstance(context);
-                    } catch (NoSuchMethodException e) {
-                        // Fallback for non-standard "Script" classes.
-                        script = (Script) scriptClass.newInstance();
-                        script.setBinding(context);
-                    }
+                    script = newScript(scriptClass, context);
                 } else {
                     final GroovyObject object = (GroovyObject) scriptClass.newInstance();
                     // it could just be a class, so let's wrap it in a Script
@@ -487,6 +480,19 @@ public class InvokerHelper {
         return script;
     }
 
+    public static Script newScript(Class scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException {
+        Script script;
+        try {
+            Constructor constructor = scriptClass.getConstructor(Binding.class);
+            script = (Script) constructor.newInstance(context);
+        } catch (NoSuchMethodException e) {
+            // Fallback for non-standard "Script" classes.
+            script = (Script) scriptClass.newInstance();
+            script.setBinding(context);
+        }
+        return script;
+    }
+
     /**
      * Sets the properties on the given object
      */