I use GroovyScriptEngine
inside my Java application to load code dynamically from different sources. Let's say I have two folders sources_A\
and sources_B\
.
GroovyScriptEngine engine = new GroovyScriptEngine(new String[]{
"sources_A", "sources_B"
});
Within each folder I have Groovy packages and classes that I wish to instantiate arbitrarily at runtime.
Problem: Some of these classes have the same (full) name in both folders.
So there is an ambiguity when I run:
engine.loadScriptByName("some.package.SomeClass").newInstance()
Of course, I could create two different engines:
GroovyScriptEngine engine_A = new GroovyScriptEngine(new String[]{"sources_A"});
GroovyScriptEngine engine_B = new GroovyScriptEngine(new String[]{"sources_B"});
But then I run into issues when two objects instantiated from these two different engines have to interact with each other:
object_from_A.someMethod(object_from_B);
Error: argument type mismatch at line ** in method foo in file bar.groovy
(Needless to say that there is no type mismatch: the object has the right type but is not recognized due to the different engine)
In short, do you have a solution to either:
- The one-engine solution with some way to disambiguate which source folder is used?
- The two-engine solution with some way to have objects from the two different engines to work together?