Java - 使用 Ant 自动生成样板代码 [英] Java - Using Ant to automatically generate boilerplate code

查看:28
本文介绍了Java - 使用 Ant 自动生成样板代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:我在尝试之前问过这个问题,失败并感到沮丧,因为我对 Apache Ant 的经验为 0.一个简单的是的,这行得通"就足够了,如果不行,请告诉我什么行得通.

Intro: I'm asking this before I try, fail and get frustrated as I have 0 experience with Apache Ant. A simple 'yes this will work' may suffice, or if it won't please tell me what will.

情况:我正在开发一个使用 JavaFX 创建 GUI 的项目.JavaFX 依赖于类 Java Bean 的对象,这些对象的属性需要大量样板代码.例如,我想要的所有功能都是一个名为 nameString,默认值为 "Unnamed",或者使用最小的 Java 语法:

Situation: I'm working on a project that uses JavaFX to create a GUI. JavaFX relies on Java Bean-like objects that require a lot of boilerplate code for it's properties. For example, all functionality I want to have is a String called name with default value "Unnamed", or in a minimal Java syntax:

String name = "Unnamed";

在 JavaFX 中,最少的代码量增加了很多以提供相同的功能(在这种情况下,功能对我来说意味着我可以设置并获取某个变量以在我的程序中使用):

In JavaFX the minimum amount of code increases a lot to give the same functionality (where functionality in this case means to me that I can set and get a certain variable to use in my program):

private StringProperty name = new StringProperty("Unnamed");
public final String getName() { return name.get(); }
public final void setName(String value) { name.set(value); }

问题:我可以使用 Ant 生成此样板代码吗?

Question: Can I use Ant to generate this boilerplate code?

似乎可以使 Ant 脚本用作 (Java) 预处理器.例如通过使用正则表达式替换 (https://ant.apache.org/manual/Tasks/replaceregexp.html) 函数.我在我的代码中考虑与此类似的代码行,然后将自动替换:

It seems possible to make Ant scripts that function as (Java) preprocessors. For instance by using the regex replace (https://ant.apache.org/manual/Tasks/replaceregexp.html) functions. I'm thinking of lines of code similar to this in my code, which then will be auto-replaced:

<TagToSignifyReplaceableLine> StringProperty person "Unnamed"

最后一句话: 正如我之前所说的,我以前从未使用过 Ant,所以我想与您核实一下是否 1) 可以做到 2) 这是否是一个好方法这样做或者是否有更好的方法.

Final remark: As I've said before I have never used Ant before, so I want to check with you if 1) this can be done and 2) if this is a good way to do it or if there are better ways.

谢谢!

推荐答案

是的,可能.您甚至可以实现自己的 Ant 任务,这很容易完成.

Yes, possible. You can even implement your own Ant task, that does this job very easily.

在蚂蚁中是这样的:

<taskdef name="codegen" classpath="bin/" classname="com.example.CodeGen" />

然后

<codegen className="Test.java">
   <Property name="StringProperty.name" value="Unnamed"/>
</codegen>

CodeGen.java 就像这样:

The CodeGen.java then like so:

public class CodeGen extends Task {
    private String className = null;
    private List properties = new ArrayList();

    public void setClassName(String className) {
        this.className = className;
    }

    /**
     * Called by ant for every &lt;property&gt; tag of the task.
     *  
     * @param property The property.
     */
    public void addConfiguredProperty(Property property) {
      properties.add(property);
    }

    public void execute() throws BuildException {
        // here we go!
    }

}

这篇关于Java - 使用 Ant 自动生成样板代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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