Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

java - How this non-existing getter method becomes available

I'm just getting my self familiarized with the code base of our application and I found this one Java class:

@Setter
@Getter
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = SqlTables.PRODUCTS)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Schedule {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @NotBlank(message = "name should not be empty")
  private String name;

  private boolean recurring;

An instance of the above class can be passed as method parameter to other classes e.g.:

private void validateRecurringAttribute(Schedule schedule) {
    if (schedule.isRecurring()) {
      log.error("[ERROR] <validateRecurringAttribute()> Recurring schedule is not yet supported.");
      throw new UnsupportedException("Recurring schedule is not yet supported.");
    }
  }

What I'm wondering is this getter method - isRecurring(), it is not available in the Schedule class but somehow can be called in other classes. How is it done?

question from:https://stackoverflow.com/questions/65930112/how-this-non-existing-getter-method-becomes-available

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

These Getters and Setters of the class data members are generated by the @Setter and @Getter annotation of Lombok.

Further you can use @Data which implicitly calls

@Getter 
@Setter 
@RequiredArgsConstructor
@ToString

Basically we are using these annotation of lombok to remove boilerplate code.

Example:

@Data
Class tax{
 private int tax;
}

Now we don't need to explicitly define getter and setters for it like.

private int gettax(){
    return tax;
}
private void settax(int tax){
    this.tax = tax;
}

https://projectlombok.org/features/Data


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...