We already have a final class like the following. It has private fields and getters for the fields. It does not have setters but it does have an internal class to set the values for some of the fields. I use Jacksonmapper to construct Vehicle object from a JSON string. After the Vehicle object is constructed, i need to set values for some fields. Since i dont have setters and since i cannot use the ObjBuilder, I used reflection. Instead of using reflection, is there a more proper way to set the values for the private fields, without modifying the Vehicle class?
public final class Vehicle {
private String regNo;
private String make;
private String model;
public String regNo() {
return regNo;
}
public String make() {
return make;
}
public String model() {
return model;
}
public static class ObjBuilder {
Vehicle veh = null;
public ObjBuilder(String regNo) {
veh = new Vehicle(regNo);
}
public ObjBuilder make(String val) {
veh.make = val;
return this;
}
public Vehicle build() {
return veh;
}
}
}
Aucun commentaire:
Enregistrer un commentaire