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

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

问题描述

简介:
我问这之前,我尝试,失败和沮丧,因为我有使用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的依赖于那些需要它的属性很多样板code的Java Bean的状物体。例如,所有的功能,我想有一个的字符串的所谓的名称的默认值的未命名的,或在一个最小的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(其中在这种情况下,功能意味着,我认为我可以设置和获取某个变量在我的程序中使用)code的最低量增加了很多给予相同的功能:

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生成这个样板code?

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

这似乎可以作出(JAVA)preprocessors该功能Ant脚本。例如通过使用正则表达式替换( https://ant.apache.org/manual/Tasks /replaceregexp.html )功能。我想类似这样code线在我的$ C $℃,然后将被自动替换为:

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"

最后再说一句:正如我以前说过我从来没有使用过蚂蚁,所以我想和你一起检查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.

喜欢的东西所以在蚂蚁:

Something like so in ant:

<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!
    }

}

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

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