简体   繁体   中英

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

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.

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:

替代文字

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+ // , ie 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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