Skip to content

Instantly share code, notes, and snippets.

@ufuk
Last active October 5, 2016 21:14
Show Gist options
  • Select an option

  • Save ufuk/a13b951f594ca0c172d5cd8013f2f516 to your computer and use it in GitHub Desktop.

Select an option

Save ufuk/a13b951f594ca0c172d5cd8013f2f516 to your computer and use it in GitHub Desktop.
Workaround for Project Lombok's builders with inheritance issue.
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Child extends Parent {
private String childField_1;
private String childField_2;
@Builder
private Child(String childField_1, String childField_2, String parentField_1, String parentField_2) {
super(parentField_1, parentField_2);
this.childField_1 = childField_1;
this.childField_2 = childField_2;
}
}
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
public class LombokTest {
@Test
public void shouldBuilderWorksForInheritance() {
Child childInstance = Child.builder()
.childField_1("c1")
.parentField_1("p1")
.childField_2("c2")
.parentField_2("p2")
.build();
assertThat(childInstance.getChildField_1(), equalTo("c1"));
assertThat(childInstance.getChildField_2(), equalTo("c2"));
assertThat(childInstance.getParentField_1(), equalTo("p1"));
assertThat(childInstance.getParentField_2(), equalTo("p2"));
}
}
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class Parent {
private String parentField_1;
private String parentField_2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment