I have a form which is accepting only String
variables but I want to generate a Class
instance such that each string property is converted to its corresponding type identified by property name in the class.
For e.g. If I have three fields in a form with the name of NAME
, ANNUAL SALARY
and isLocalResident
. I want to convert this form to an instance of Class1
where these properties are of String
, BigInteger
and Boolean
type.
How do I do that?
Currently I am using BeanUtil
Property of java, but it doesn't fulfill my requirement because it just doesn't covert to its required types; rather it asks me a setter and getter of the desired type.
Please help me out of this. Any suggestion would be appreciated?
P note that this form is dynamic, i.e. number and name of fields shall depend on another input.
The following code represents generating instance emp1
for Employee1
class.
public Empolyee1 setContent(List<String> nameList, List<String> valueList) {
Empolyee1 emp1 = new Empolyee1();
for(String attr : nameList) {
if(attr.equalsIgnoreCase("name")) {
if(valueList.get(nameList.indexOf("name")) != null && !valueList.get(nameList.indexOf("name")).trim().equals("")) {
emp1.setName(valueList.get(nameList.indexOf("name")));
}
} else {
if(attr.equalsIgnoreCase("ANNUAL SALARY")) {
if(valueList.get(nameList.indexOf("ANNUAL SALARY")) != null && !valueList.get(nameList.indexOf("ANNUAL SALARY")).trim().equals("")) {
emp1.setAnnualSalary(valueList.get(nameList.indexOf("ANNUAL SALARY")));
}
} else {
if(attr.equalsIgnoreCase("isLocalResident")) {
if(valueList.get(nameList.indexOf("isLocalResident")) != null && !valueList.get(nameList.indexOf("isLocalResident")).trim().equals("")) {
emp1.setIsLocalResident(valueList.get(nameList.indexOf("isLocalResident")));
}
}
}
}
return empl1;
}