I am building a heavily plugin-based application. The plugins communicate via a shared message bus. For several obvious reasons, including assurance of type safety, I end up writing code similar to this:
public class OtherPluginController {
...
public void doSomething(int intParam, String stringParam) {
Command cmd = new Command();
cmd.target = "OtherPlugin";
cmd.name = "doSomething";
cmd.params.put("intParam", intParam);
cmd.params.put("stringParam", stringParam);
MessageBus.emit(cmd);
}
...
}
For the sake of extensibility and maintainability it now would be great to create those controllers from some kind of manifest file, for the example above, and using XML, something like
...
...
Although there are plenty of template-based frameworks, most of them seem to target much different use cases. Is there a framework I missed? If not, is there a framework I could at least abuse for this kind of things?
解决方案
After some additional research I find that, if the "manifest" is provided in XML, the best and simplest approach seems to be XSLT. Given a manifest file like this:
And an XSL-file like this:
public class Controller {
public void (, ) {
}
}
the output would be
public class OtherPluginController {
public void doSomething(int intParam, String stringParam) {
}
public void doSomethingElse() {
}
}
The main advantages of using XSLT are, that you don't need to define your own syntax and/or parser for your definition language and, as it works with any browser, it can be used without any additional setup. However, there are still ways to integrate XSLT into eclipse, if you want the code generation as part of your IDE.