Repository: groovy Updated Branches: refs/heads/master 53b2a07ea -> 5d2fcc607
Minor refactoring Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/5d2fcc60 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5d2fcc60 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5d2fcc60 Branch: refs/heads/master Commit: 5d2fcc607013b92e64cc9da66d0965c22c9868d7 Parents: 53b2a07 Author: sunlan <[email protected]> Authored: Sat Dec 2 15:44:47 2017 +0800 Committer: sunlan <[email protected]> Committed: Sat Dec 2 15:44:47 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/5d2fcc60/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/5d2fcc60/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 */
