Groovy生成了类名

时间:2014-01-31 08:15:22

标签: java groovy jsonobject groovyshell

我正在使用java + groovy脚本。有可能改变由groovy类名生成的(Script1.groovy,Script777.groovy等)吗?如果出现异常,很难找到正确的脚本:/

Caused by: org.json.JSONException: JSONObject["value14"] not found.
at org.json.JSONObject.get(JSONObject.java:498)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetMethodMetaProperty.getProperty(MethodMetaProperty.java:59)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:61)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
at Script4.run(Script4.groovy:23)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:346)
... 13 more

1 个答案:

答案 0 :(得分:4)

是的,可以为groovy脚本设置自定义名称。您应该使用groovy.lang.GroovyClassLoader。例如:

GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<Script> parsedClass = groovyClassLoader.parseClass(scriptText, name);
Script script = scriptClass.newInstance();
script.run()