简体   繁体   English

Eclipse生成setter

[英]Eclipse to generate 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? 在Eclipse中是否有任何方法给定一个具有已定义的setter列表的类,您可以在填充它们之前将它们列出来?

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). 显然,4个字段的值不存在,但是当你有100个字段时它会很好(想想一个讨厌的XML片段的JAXB Bean)。

Note : I'm not asking about the Source -> Generate Getters / Setters menu. 注意 :我不是在询问Source -> Generate Getters / Setters菜单。

Thanks. 谢谢。

This is not directly possible in Eclipse, but certainly can be done in a plugin. 这在Eclipse中不可能直接实现,但肯定可以在插件中完成。

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: 该插件允许您使用以下wizzard为每个setter生成该类测试代码:

替代文字

I think that pretty simple combination of cat, grep and sed may do work for you. 我认为cat,grep和sed的非常简单的组合可能对你有用。

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. 我在名为Device的类上运行它。

Here is the produced output: 这是产生的输出:

myObj.setId(id); myObj.setId(ID);
myObj.setNativeId(nativeId); myObj.setNativeId(nativeId);
myObj.setManufacturer(manufacturer); myObj.setManufacturer(制造商);
myObj.setModel(model); myObj.setModel(模型);
myObj.setCapabilities(List capabilities); myObj.setCapabilities(列表功能);

As you can see it worked for all primitives but the last setter requires some modifications. 正如您所看到的,它适用于所有原语,但最后一个setter需要进行一些修改。 It is because sed command does not support full set of regular expression operators. 这是因为sed命令不支持完整的正则表达式运算符集。 You are welcome to user perl or awk instead. 欢迎您使用perl或awk。 In this case you can say simply s/\\(\\S+ // , ie remove all non-whitespace characters that are following the left bracket and space after that. 在这种情况下,您可以简单地说s/\\(\\S+ // ,即删除后面左括号和空格后面的所有非空格字符。

I think that writing Eclipse plugin is a perfect but too expensive solution. 我认为编写Eclipse插件是一个完美但过于昂贵的解决方案。

I used here Unix shell commands. 我在这里使用Unix shell命令。 If you have a bad luck :( to develop on Windows (like me) I'd recommend you to use cygwin (as I do). 如果你运气不好:(在Windows上开发(像我一样)我建议你使用cygwin(就像我一样)。

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 (eg fields, static members, nested classes) that you are not interested in. 但是,类大纲视图非常接近,特别是如果您调整过滤器以排除您不感兴趣的内容(例如字段,静态成员,嵌套类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM