2014-01-31 67 views
3

我使用java + groovy腳本。是否有可能改變由groovy類名(Script1.groovy,Script777.groovy等)生成?如果發生異常,很難找到正確的腳本:/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 

回答

3

是的,可以爲常規腳本設置自定義名稱。你應該使用groovy.lang.GroovyClassLoader。例如:

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