You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by 杨文强 <16...@qq.com> on 2016/01/11 10:32:47 UTC
about methodMissing
hi,
I am a fresh person for groovy. here is the code I have written:
public class TestAction extends ActionBase {
private static final long serialVersionUID = 8763503930833969428L;
public void Test(){
JSONObject params = requestParams != null? JSONObject.fromObject(requestParams): new JSONObject();
try {
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
String classText = "class DynamicGroovyClass { def methodMissing(String name, args) {println \"You called $name with $args\" args.size() } }";
@SuppressWarnings("rawtypes")
Class groovyClass = loader.parseClass(classText);
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object [] args = {"hello", "world", "feel", "happy"};
Object result = groovyObject.invokeMethod("run", args);
System.out.println("Received:" + result);
sendJsonFormattedData(params.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
when I run the code, it throws an exception:
You called run with [hello, world, feel, happy]
java.lang.NullPointerException: Cannot get property 'args' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:172)
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
at DynamicGroovyClass.methodMissing(script1452504513039443064905.groovy:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaClassImpl.invokeMissingMethod(MetaClassImpl.java:936)
at groovy.lang.MetaClassImpl.invokePropertyOrMissing(MetaClassImpl.java:1259)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1212)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)
at DynamicGroovyClass.invokeMethod(script1452504513039443064905.groovy)
at com.shanghe.action.TestAction.Test(TestAction.java:31)
so. why groovy can't get property args but it can print args? how to resolve the exception?
Re: about methodMissing
Posted by Jochen Theodorou <bl...@gmx.org>.
Am 11.01.2016 um 10:32 schrieb 杨文强:
[...]
> String classText = "class DynamicGroovyClass { def methodMissing(String name, args) {println \"You called $name with $args\" args.size() } }";
you will require a newline or a semicolon after the println to let
Groovy know you started a new command. Otherwise Groovy will assume you
wanted to create a commandexpression, in which case your code translates to:
println ("You called $name with $args").args.size()
and since println is a void method it tries to do null.args resulting in
that NPE
bye Jochen