Java的基于模板和清单的代码生成 [英] Template- and manifest-based Code Generation for Java

查看:73
本文介绍了Java的基于模板和清单的代码生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基于插件的大量应用程序。插件通过共享消息总线进行通信。出于几个显而易见的原因,包括确保类型安全,我最终编写了类似于以下代码:

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);
    }
    ...
}

为了可扩展性和可维护性现在,从上面的示例中的某种清单文件创建那些控制器,并使用XML,就像

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?

推荐答案

经过一些额外的研究,我发现,如果以XML形式提供清单,则最好和最简单的方法似乎是 XSLT 。给定这样的清单文件:

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>

还有这样的XSL文件:

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>

输出将是

public class OtherPluginController {

    public void doSomething(int intParam, String stringParam) {

    }

    public void doSomethingElse() {

    }

}

使用XSLT的主要优点是,您无需为自己的定义语言定义自己的语法和/或解析器,并且由于它可与任何浏览器一起使用,因此无需任何其他设置即可使用它。但是,如果要将代码生成作为IDE的一部分,仍然有将XSLT集成到eclipse中的方法。

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.

这篇关于Java的基于模板和清单的代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆