Generating getters and setters

Let's assume that Melon has three fields (type, weight, and ripe) and defines only a getter for type and a setter for ripe:

public class Melon {  private String type;  private int weight;  private boolean ripe;  ...  public String getType() {    return type;  }  public void setRipe(boolean ripe) {    this.ripe = ripe;  }  ...}

In order to generate the missing getters and setters, we start by identifying them. The following solution loops the declared fields of the given class and assumes that a foo field doesn't have a getter if the following apply:

  • There is no get/isFoo() method
  • The return type is not the same as the field type
  • The number of arguments is not 0

For each missing getter, this solution adds in a map an entry ...

Get Java Coding Problems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.