import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface BuilderProperty {
String value() default "";
}
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@BuilderProperty("name")
public void setName(String name) {
this.name = name;
}
@BuilderProperty("age")
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Employee extends Person {
private String designation;
public Employee() {
}
public Employee(String name, int age, String designation) {
super(name, age);
this.designation = designation;
}
@BuilderProperty("designation")
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDesignation() {
return designation;
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class PersonBuilder<T> {
private Class<T> type;
private T instance;
public PersonBuilder(Class<T> type) {
this.type = type;
try {
this.instance = type.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}
public PersonBuilder<T> withName(String name) {
Method[] methods = type.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(BuilderProperty.class) &&
method.getName().startsWith("set") &&
method.getParameterCount() == 1 &&
method.getParameterTypes()[0] == String.class) {
BuilderProperty annotation = method.getAnnotation(BuilderProperty.class);
if ("name".equals(annotation.value())) {
try {
method.invoke(instance, name);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
break;
}
}
}
return this;
}
public PersonBuilder<T> withAge(int age) {
Method[] methods = type.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(BuilderProperty.class) &&
method.getName().startsWith("set") &&
method.getParameterCount() == 1 &&
method.getParameterTypes()[0] == int.class) {
BuilderProperty annotation = method.getAnnotation(BuilderProperty.class);
if ("age".equals(annotation.value())) {
try {
method.invoke(instance, age);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
break;
}
}
}
return this;
}
public PersonBuilder<T> withDesignation(String designation) {
Method[] methods = type.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(BuilderProperty.class) &&
method.getName().startsWith("set") &&
method.getParameterCount() == 1 &&
method.getParameterTypes()[0] == String.class) {
BuilderProperty annotation = method.getAnnotation(BuilderProperty.class);
if ("designation".equals(annotation.value())) {
try {
method.invoke(instance, designation);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
break;
}
}
}
return this;
}
public T build() {
return instance;
}
}
public static void main(String[] args) {
Employee employee = new PersonBuilder<>(Employee.class)
.withName("John")
.withAge(25)
.withDesignation("Manager")
.build();
System.out.println(JSONObject.toJSONString(employee));
}