Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X b47e9e11f -> 5f2c8be02
GROOVY-8514: NullPointerException in class MissingMethodException (closes #675) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/5f96698b Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5f96698b Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5f96698b Branch: refs/heads/GROOVY_2_6_X Commit: 5f96698b705b89f11ee3884ed5f3e134553815f5 Parents: 63fbf45 Author: paulk <[email protected]> Authored: Mon Mar 19 22:46:07 2018 +1000 Committer: paulk <[email protected]> Committed: Fri Mar 23 09:37:19 2018 +1000 ---------------------------------------------------------------------- .../groovy/lang/MissingMethodException.java | 3 ++- .../groovy/jsr223/GroovyScriptEngineImpl.java | 18 +++++++++++++- .../codehaus/groovy/jsr223/JSR223Test.groovy | 25 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/5f96698b/src/main/groovy/groovy/lang/MissingMethodException.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/groovy/lang/MissingMethodException.java b/src/main/groovy/groovy/lang/MissingMethodException.java index 0902150..ae2eea1 100644 --- a/src/main/groovy/groovy/lang/MissingMethodException.java +++ b/src/main/groovy/groovy/lang/MissingMethodException.java @@ -29,6 +29,7 @@ import org.codehaus.groovy.runtime.MethodRankHelper; */ public class MissingMethodException extends GroovyRuntimeException { + private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; private final String method; private final Class type; private final boolean isStatic; @@ -48,7 +49,7 @@ public class MissingMethodException extends GroovyRuntimeException { this.method = method; this.type = type; this.isStatic = isStatic; - this.arguments = arguments; + this.arguments = arguments == null ? EMPTY_OBJECT_ARRAY : arguments; } public String getMessage() { http://git-wip-us.apache.org/repos/asf/groovy/blob/5f96698b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java index 03fc49e..2c9e633 100644 --- a/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java +++ b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java @@ -367,6 +367,22 @@ public class GroovyScriptEngineImpl extends AbstractScriptEngine implements Comp } } + private Object invokeImplSafe(Object thiz, String name, Object... args) { + if (name == null) { + throw new NullPointerException("method name is null"); + } + + try { + if (thiz != null) { + return InvokerHelper.invokeMethod(thiz, name, args); + } else { + return callGlobal(name, args); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + // call the script global function of the given name private Object callGlobal(String name, Object[] args) { return callGlobal(name, args, context); @@ -404,7 +420,7 @@ public class GroovyScriptEngineImpl extends AbstractScriptEngine implements Comp new InvocationHandler() { public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { - return invokeImpl(thiz, m.getName(), args); + return invokeImplSafe(thiz, m.getName(), args); } }); } http://git-wip-us.apache.org/repos/asf/groovy/blob/5f96698b/subprojects/groovy-jsr223/src/test/groovy/org/codehaus/groovy/jsr223/JSR223Test.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-jsr223/src/test/groovy/org/codehaus/groovy/jsr223/JSR223Test.groovy b/subprojects/groovy-jsr223/src/test/groovy/org/codehaus/groovy/jsr223/JSR223Test.groovy index d680130..ce63254 100644 --- a/subprojects/groovy-jsr223/src/test/groovy/org/codehaus/groovy/jsr223/JSR223Test.groovy +++ b/subprojects/groovy-jsr223/src/test/groovy/org/codehaus/groovy/jsr223/JSR223Test.groovy @@ -220,4 +220,29 @@ class JSR223Test extends GroovyTestCase { assert engine.getFactory() == factory } + void testGetInterfaceScenarios() { + assertScript ''' + interface Test { def foo(); def bar(); def baz() } + def engine = new javax.script.ScriptEngineManager().getEngineByName("groovy") + engine.eval("def foo() { 42 }") + engine.eval("def bar() { throw new Exception('Boom!') }") + def test = engine.getInterface(Test) + assert test.foo() == 42 + + try { + test.bar() + assert false + } catch(RuntimeException re) { + assert re.message.endsWith('Boom!') + } + + try { + test.baz() + assert false + } catch(RuntimeException re) { + assert re.cause.class.name.endsWith('MissingMethodException') + } + ''' + } + }
