yonan2236
yonan2236

Reputation: 13649

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?

Upvotes: 1

Views: 131

Answers (2)

Zahid Khan
Zahid Khan

Reputation: 3267

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

Upvotes: 4

shakhawat
shakhawat

Reputation: 2727

The getters and setters are generated at compile time by these 2 lombok annotations in the class -

@Setter
@Getter

To know more about Lombok and getter setter -

https://projectlombok.org/features/GetterSetter

Upvotes: 4

Related Questions