Template- and manifest-based Code Generation for Java

佐手、 提交于 2019-12-25 03:27:25

问题


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

<Plugin name="OtherPlugin">
    ...
    <Command name="doSomething">
        <Parameter name="intParam" type="int"/>
        <Parameter name="stringParam" type="String"/>
    </Command>
    ...
</Plugin>

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?


回答1:


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:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="PluginControllerTemplate.xsl"?>
<Plugin name="OtherPlugin">
    <Command name="doSomething">
        <Parameter name="intParam" type="int"/>
        <Parameter name="stringParam" type="String"/>
    </Command>
    <Command name="doSomethingElse"/>
</Plugin>

And an XSL-file like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:output method="text"/>
    <xsl:preserve-space elements="*"/>
    <xsl:template match="Plugin" xml:space="preserve">
public class <xsl:value-of select="@name"/>Controller {
        <xsl:for-each select="Command">
    public void <xsl:value-of select="@name"/>(<xsl:for-each select="Parameter"><xsl:value-of select="@type"/> <xsl:value-of select="@name"/><xsl:if test="position() != last()">, </xsl:if></xsl:for-each>) {

    }       
        </xsl:for-each>
}
    </xsl:template>
</xsl:stylesheet>

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.



来源:https://stackoverflow.com/questions/23543247/template-and-manifest-based-code-generation-for-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!