Repository: groovy Updated Branches: refs/heads/master 5d2fcc607 -> 9b29e7f4b
Minor refactoring Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9b29e7f4 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9b29e7f4 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9b29e7f4 Branch: refs/heads/master Commit: 9b29e7f4b7884b62059b6f234ee76ae090df2076 Parents: 5d2fcc6 Author: sunlan <[email protected]> Authored: Sat Dec 2 16:13:43 2017 +0800 Committer: sunlan <[email protected]> Committed: Sat Dec 2 16:13:43 2017 +0800 ---------------------------------------------------------------------- src/main/groovy/lang/GroovyShell.java | 16 +++++++++------- .../org/codehaus/groovy/runtime/InvokerHelper.java | 5 +++-- 2 files changed, 12 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/9b29e7f4/src/main/groovy/lang/GroovyShell.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/GroovyShell.java b/src/main/groovy/lang/GroovyShell.java index a889e51..4dc51c9 100644 --- a/src/main/groovy/lang/GroovyShell.java +++ b/src/main/groovy/lang/GroovyShell.java @@ -39,6 +39,8 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.List; +import static org.codehaus.groovy.runtime.InvokerHelper.MAIN_METHOD_NAME; + /** * Represents a groovy shell capable of running arbitrary groovy scripts * @@ -271,9 +273,9 @@ public class GroovyShell extends GroovyObjectSupport { } try { // let's find a main method - scriptClass.getMethod("main", String[].class); + scriptClass.getMethod(MAIN_METHOD_NAME, String[].class); // if that main method exist, invoke it - return InvokerHelper.invokeMethod(scriptClass, "main", new Object[]{args}); + return InvokerHelper.invokeMethod(scriptClass, MAIN_METHOD_NAME, new Object[]{args}); } catch (NoSuchMethodException e) { // if it implements Runnable, try to instantiate it if (Runnable.class.isAssignableFrom(scriptClass)) { @@ -285,20 +287,20 @@ public class GroovyShell extends GroovyObjectSupport { return runner.run(scriptClass, this.loader); } } - String message = "This script or class could not be run.\n" + + StringBuilder message = new StringBuilder("This script or class could not be run.\n" + "It should either:\n" + "- have a main method,\n" + "- be a JUnit test or extend GroovyTestCase,\n" + "- implement the Runnable interface,\n" + - "- or be compatible with a registered script runner. Known runners:\n"; + "- or be compatible with a registered script runner. Known runners:\n"); if (runnerRegistry.isEmpty()) { - message += " * <none>"; + message.append(" * <none>"); } else { for (String key : runnerRegistry.keySet()) { - message += " * " + key + "\n"; + message.append(" * ").append(key).append("\n"); } } - throw new GroovyRuntimeException(message); + throw new GroovyRuntimeException(message.toString()); } } http://git-wip-us.apache.org/repos/asf/groovy/blob/9b29e7f4/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 56e7a06..0e3b380 100644 --- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java +++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java @@ -84,6 +84,7 @@ public class InvokerHelper { private static final int ITEM_ALLOCATE_SIZE = 5; public static final MetaClassRegistry metaRegistry = GroovySystem.getMetaClassRegistry(); + public static final String MAIN_METHOD_NAME = "main"; public static void removeClass(Class clazz) { metaRegistry.removeMetaClass(clazz); @@ -451,7 +452,7 @@ public class InvokerHelper { } catch (MissingPropertyException e) { // They'll get empty args since none exist in the context. } - object.invokeMethod("main", argsToPass); + object.invokeMethod(MAIN_METHOD_NAME, argsToPass); return null; } }; @@ -473,7 +474,7 @@ public class InvokerHelper { return script; } - public static Script newScript(Class scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException { + public static Script newScript(Class<?> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException { Script script; try { Constructor constructor = scriptClass.getConstructor(Binding.class);
