groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [2/3] groovy git commit: Minor refactoring
Date Sat, 02 Dec 2017 08:20:20 GMT
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/2d84a1e4
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/2d84a1e4
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/2d84a1e4

Branch: refs/heads/GROOVY_2_6_X
Commit: 2d84a1e473adf692d40b51a2352eb1a68f44da10
Parents: cfd4901
Author: sunlan <sunlan@apache.org>
Authored: Sat Dec 2 15:44:47 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Sat Dec 2 16:20:01 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/2d84a1e4/src/main/groovy/lang/GroovyShell.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyShell.java b/src/main/groovy/lang/GroovyShell.java
index b169e59..a889e51 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/2d84a1e4/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 6f7406a..56e7a06 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -435,14 +435,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
@@ -480,6 +473,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
      */


Mime
View raw message