For a Java application, is there a better way to support configuration files based on a custom DSL (such as relying on Groovy or Kotlin) than using JSR223 ScriptEngine
's eval
or compile
methods?
Loading script's content as String
and then using it in a code like that is working but maybe there are better and more efficient ways of doing it.
String type = "groovy"; // or "kts"
String script = "..."; // code of DSL script
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension(type);
Compilable compiler = (Compilable) engine;
CompiledScript code = compiler.compile(script);
Object result = code.eval();