Very simple groovy script:
@Field
List list
def execute(Object args) {
return list[0]
}
I try to write simple code in java:
final GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
final File file = new File("<path to groovy file>");
GroovyCodeSource groovyCodeSource = new GroovyCodeSource(file);
final Class groovyClass = groovyClassLoader.parseClass(groovyCodeSource);
final List<String> testList = Collections.singletonList("test");
final Binding context = new Binding();
context.setVariable("list", testList );
final Script script = InvokerHelper.createScript(groovyClass, context);
final Field list = groovyClass.getField("list");
list.setAccessible(true);
list.set(null, testList );
final Object returnValue = script.invokeMethod("execute", null);
But in field groovyClass.getField("list"); I get exception NoSuchFieldException Could you please help me? Why is it happened?
Class::getField()says: "Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object." – What is the default visibility for a field in Groovy?groovyClass.getFields()?Class::getDeclaredField()?