调用 firePropertyChange() 的 setter 的 Eclipse 模板

发布于 2024-12-27 07:44:48 字数 513 浏览 0 评论 0 原文

对于 MVC 模型类,我的 setter 如下所示:

enum BoundProperty {FIELD_NAME, ...}

private Type fieldName;

public setFieldName(Type newValue) {
    Type oldValue = fieldName;
    fieldName = newValue;
    firePropertyChange(BoundProperty.FIELD_NAME, oldValue, newValue);
}

给定一个字段,可以从自动生成的 setter 生成此输出吗?如果没有,有没有办法从模板中获取此输出?

输出应使用 CamelCase 字段名称来生成方法名称,因此 fieldName 生成 setFieldName() 并将字段名称大写以生成属性枚举。

因此 fieldName 会生成 FIELD_NAME (或者 FIELDNAME 也可以)。

For MVC model classes, my setters look like:

enum BoundProperty {FIELD_NAME, ...}

private Type fieldName;

public setFieldName(Type newValue) {
    Type oldValue = fieldName;
    fieldName = newValue;
    firePropertyChange(BoundProperty.FIELD_NAME, oldValue, newValue);
}

Given a field, can this output be produced from the autogenerated setter? If not is there a way to get this output from a template?

The output should CamelCase the field name to produce the method name, so fieldName generates setFieldName() and Uppercase the field name to produce the property enum.

So fieldName generates FIELD_NAME (or FIELDNAME would work too).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安稳善良 2025-01-03 07:44:48

我认为没有一种直接的方法可以通过 Eclipse 模板来实现这一点,主要涉及camelCase/upperCase 和枚举值的生成。您可以检查这两个问题有没有办法在 Eclipse (Helios) 代码模板中将变量值的第一个字母大写, 以编程方式添加代码模板?以深入了解更多详细信息。

恕我直言,实现您想要的效果的最佳方法是使用 Fast Code Eclipse 插件 并编写一个该插件的速度模板,可从字段生成所有代码。

enum BoundProperty {
#foreach ($field in ${fields})
    ${field.toUpperCase()} #if( $foreach.hasNext ), #end
#end
}

#foreach ($field in ${fields})
    public ${field.type} get${field.name.substring(0,1).toUpperCase()}${field.name.substring(1)}(${field.type} newValue) {
        Type oldValue = fieldName;
        fieldName = newValue;
        firePropertyChange(BoundProperty.${field.name.toUpperCase()}, oldValue, newValue);       
    }
#end

或者更改该插件的"getter_setter"模板

I think there is not a straightforward way to make this through Eclipse templates, mainly regarding the camelCase/upperCase and generation of enum values. You can check these two questions Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates, Programmatically add code templates? to dig into further details.

IMHO, the best way to achieve what you want is to use the Fast Code Eclipse Plugin and write a velocity template for that plugin that generates all the code from the fields.

enum BoundProperty {
#foreach ($field in ${fields})
    ${field.toUpperCase()} #if( $foreach.hasNext ), #end
#end
}

#foreach ($field in ${fields})
    public ${field.type} get${field.name.substring(0,1).toUpperCase()}${field.name.substring(1)}(${field.type} newValue) {
        Type oldValue = fieldName;
        fieldName = newValue;
        firePropertyChange(BoundProperty.${field.name.toUpperCase()}, oldValue, newValue);       
    }
#end

Or change the "getter_setter" template of that plugin.

葵雨 2025-01-03 07:44:48

我可以在“生成 getter/setter”对话框中看到此消息。
可以在“代码模板”首选项页面上配置 getter/setter 的格式。您可以前往那里(Code 部分下的 Setter Body)并修改如下。

Type oldValue = ${field};
${field} = ${param};
firePropertyChange(BoundProperty.FIELD_NAME, oldValue, ${param});

然而,它不会生成 BoundProperty 。需要更多的研究来确定是否可能。这些链接可能会帮助

有用的 Eclipse Java 代码模板
Eclipse 代码模板入门

I can see this message on "Generate getters/setters" dialog.
The format of the getters/setters may be configured on the Code Templates preference page. You can go there (Setter Body under Code section) and modify like below.

Type oldValue = ${field};
${field} = ${param};
firePropertyChange(BoundProperty.FIELD_NAME, oldValue, ${param});

However it is not going to generate BoundProperty though. It need more research to find out if it is possible or not. These links may help

Useful Eclipse Java Code Templates and
Getting started with Eclipse code templates

铜锣湾横着走 2025-01-03 07:44:48

Fast Code Eclipse Plugin 看起来像是一个选项,但我在安装它时遇到了错误,但我没有这样做没有时间追问用户qualidafial对问题有用的 Eclipse Java 代码模板 可以完成我需要做的一切,而不需要 Eclipse 插件。

The Fast Code Eclipse Plugin looks like an option, but I got an error installing it which I don't have time to chase and the answer offered by user qualidafial on the question Useful Eclipse Java Code Templates does everything I need to do without requiring an Eclipse plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文