开发者

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?

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;
开发者_如何学Python  }
}

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.


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).


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:

Eclipse to generate setters


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜