Eclipse 生成 setter

发布于 2024-10-02 11:44:36 字数 799 浏览 1 评论 0原文

在 Eclipse 中,是否有任何方法可以给定一个具有定义的 setter 列表的类,以便您可以在填充它们之前将它们列出来?

例如:

public class Test {
  private String valueA;
  private String valueB;
  private String valueC;
  private String valueD;

  public void setValueA(String val) {
    this.valueA = val;
  }
  public void setValueB(String val) {
    this.valueB = val;
  }
  public void setValueC(String val) {
    this.valueC = val;
  }
  public void setValueD(String val) {
    this.valueD = val;
  }
}

如果有一个输出模板/快捷方式会非常方便:

test.setValueA(value);
test.setValueB(value);
test.setValueC(value);
test.setValueD(value);

显然,4 个字段没有该值,但是当您有 100 个字段时,那就太好了(想想 JAXB Bean 用于一段令人讨厌的 XML)。

注意:我不是在询问来源 ->生成 Getters/Setters 菜单。

谢谢。

In Eclipse is there any way that given a class with a list of defined setters, that you can list these out before populating them?

For example:

public class Test {
  private String valueA;
  private String valueB;
  private String valueC;
  private String valueD;

  public void setValueA(String val) {
    this.valueA = val;
  }
  public void setValueB(String val) {
    this.valueB = val;
  }
  public void setValueC(String val) {
    this.valueC = val;
  }
  public void setValueD(String val) {
    this.valueD = val;
  }
}

It would be very handy to have a template/shortcut to output:

test.setValueA(value);
test.setValueB(value);
test.setValueC(value);
test.setValueD(value);

Obviously the value isn't there for 4 fields but when you have 100 it would be nice (think JAXB Bean for a nasty piece of XML).

Note: I'm not asking about the Source -> Generate Getters / Setters menu.

Thanks.

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

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

发布评论

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

评论(3

伤痕我心 2024-10-09 11:44:36

我认为 cat、grep 和 sed 的非常简单的组合可能对你有用。

这是我花了半分钟写的一个例子:

cat Device.java  | grep "public void set" | sed 's/.*public void /myObj./' | sed 's/int\|long|boolean\|float\|double\|String//' | sed 's/( /(/' | sed 's/ {/;

我在名为 Device 的类上运行了它。

这是产生的输出:

myObj.setId(id);
myObj.setNativeId(nativeId);
myObj.setManufacturer(制造商);
myObj.setModel(模型);
myObj.setCapability(列出功能);

正如您所看到的,它适用于所有基元,但最后一个设置器需要一些修改。这是因为 sed 命令不支持全套正则表达式运算符。欢迎您改用 perl 或 awk 用户。在这种情况下你可以简单地说
s/\(\S+ //,即删除左括号后面的所有非空白字符以及后面的空格。

我认为编写 Eclipse 插件是一个完美但太昂贵的解决方案。

我使用这里是 Unix shell 命令。如果你在 Windows 上开发运气不好(像我一样),我建议你使用 cygwin(就像我一样)。

I think that pretty simple combination of cat, grep and sed may do work for you.

Here is an example I wrote during half a minute:

cat Device.java  | grep "public void set" | sed 's/.*public void /myObj./' | sed 's/int\|long|boolean\|float\|double\|String//' | sed 's/( /(/' | sed 's/ {/;

I ran it on my class named Device.

Here is the produced output:

myObj.setId(id);
myObj.setNativeId(nativeId);
myObj.setManufacturer(manufacturer);
myObj.setModel(model);
myObj.setCapabilities(List capabilities);

As you can see it worked for all primitives but the last setter requires some modifications. It is because sed command does not support full set of regular expression operators. You are welcome to user perl or awk instead. In this case you can say simply
s/\(\S+ //, i.e. remove all non-whitespace characters that are following the left bracket and space after that.

I think that writing Eclipse plugin is a perfect but too expensive solution.

I used here Unix shell commands. If you have a bad luck :( to develop on Windows (like me) I'd recommend you to use cygwin (as I do).

蝶舞 2024-10-09 11:44:36

这在 Eclipse 中不能直接实现,但当然可以在插件中完成。

例如,插件 Fluent-builder 您可能感兴趣:

在单元测试中实例化数据对象时,流畅的构建器特别方便

List<Movie> movies = Arrays.asList(
            MovieBuilder.movie().withTitle("Blade Runner")       // <- here's the builder used
                                .withAddedActor("Harrison Ford")
                                .withAddedActor("Rutger Hauer")
                        .build(),
            MovieBuilder.movie().withTitle("Star Wars")          // <- ... and also here
                                .withAddedActor("Carrie Fisher")
                                .withAddedActor("Harrison Ford")
                        .build());

该插件将允许您使用以下向导为每个设置器生成此类测试代码:

This is not directly possible in Eclipse, but certainly can be done in a plugin.

For instance, the plugin fluent-builder might interest you:

Fluent builders are especially handy when instantiating data objects within unit tests

List<Movie> movies = Arrays.asList(
            MovieBuilder.movie().withTitle("Blade Runner")       // <- here's the builder used
                                .withAddedActor("Harrison Ford")
                                .withAddedActor("Rutger Hauer")
                        .build(),
            MovieBuilder.movie().withTitle("Star Wars")          // <- ... and also here
                                .withAddedActor("Carrie Fisher")
                                .withAddedActor("Harrison Ford")
                        .build());

The plugin will allow you to generate that kind of test code for each setter with the following wizzard:

alt text

无人问我粥可暖 2024-10-09 11:44:36

我不知道有什么专门做这件事的。然而,类大纲视图相当接近,特别是当您调整过滤器以排除您不感兴趣的内容(例如字段、静态成员、嵌套类)时。

I'm not aware of anything that specifically does this. However, the class outline view comes fairly close, especially if you adjust the filters to exclude things (e.g. fields, static members, nested classes) that you are not interested in.

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