groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "杨文强" <>
Subject about methodMissing
Date Mon, 11 Jan 2016 09:32:47 GMT
     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()  } }";
			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);
		} catch (Exception e) {

      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(
	at org.codehaus.groovy.runtime.InvokerHelper.getProperty(
	at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(
	at DynamicGroovyClass.methodMissing(script1452504513039443064905.groovy:1)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at org.codehaus.groovy.reflection.CachedMethod.invoke(
	at groovy.lang.MetaClassImpl.invokeMissingMethod(
	at groovy.lang.MetaClassImpl.invokePropertyOrMissing(
	at groovy.lang.MetaClassImpl.invokeMethod(
	at groovy.lang.MetaClassImpl.invokeMethod(
	at groovy.lang.MetaClassImpl.invokeMethod(
	at DynamicGroovyClass.invokeMethod(script1452504513039443064905.groovy)
	at com.shanghe.action.TestAction.Test(

      so. why groovy can't get property args but it can print args? how to resolve the exception?
View raw message