jeudi 19 avril 2018

Execute compiled groovy script from java via reflection

I have many groovy scripts which are compiled with GMaven (located in src/main/groovy/somepackage), each script has run(String, String) function and does not have a class:

// script1.groovy
def run(String name, String arg) {
  // body
}


// script2.groovy
def run(String name, String arg) {
  // body
}

I can find them with Reflections library and resolve their types:

final Set<String> scripts = new Reflections(
  "somepackage",
   new SubTypesScanner(false)
).getAllTypes();
for (String script : scripts) {
  run(Class.forName(name));
}

then I have some issues with execution: I can't create scipt instance because it doesn't have public constructor (has only private one with groovy.lang.Reference parameters) and I can't find run method in this type.

The question: how to execute compiled groovy script (with single method and without a class) from Java using reflection properly?





Aucun commentaire:

Enregistrer un commentaire