Automatic Type Binding in Java
Frameworks like Struts, Spring we have seen that we send some parameters through HTTP Request from UI, They automatically typecast the parameters and bind to an Object say ActionForm in Struts.
How they do it?
How the magic happens, as we know from request we always get String value.
In this Article, I reveal the technique.
Java has a nice little interface called PropertyEditor under java.beans package.
This PropertyEditor has two main methods
- setAsText(String val)
- Object getValue();
The help of these two methods we can do the magic.
The setAsText method accepts any value as String and getValue method actual do the conversion and return the actual type.
There are many inbuilt Editors are there in Java
Like DoubleEditor,BooleanEditor etc.
These editors extends PropertyEditorSupport class which is nothing but a skeletal implementation in simple word which implements the PropertyEditor interface,
So you can create your own custom Editor by extending PropertyEditorSupport class.
Here I will create a Simple example where I bind some string parameters to an Employee Object.
For the sake of simplicity, I use java inbuilt editors.
Step 1 : Create Request Object which takes key and value as String.
package com.example.typeconversion;
import java.util.HashMap;
import java.util.Map;
public class Request {
private Map<String,String> requestMap = new HashMap<String,String>();
public Request add(String key,String value)
{
requestMap.put(key, value);
return this;
}
public Request clear()
{
requestMap.clear();
return this;
}
public String getParameter(String key)
{
return requestMap.get(key);
}
@Override
public String toString() {
return "Request [requestMap=" + requestMap + "]";
}
}
Step 2: Create a Employee POJO
package com.example.typeconversion;
public class Employee {
private String name;
private Integer age;
private Double salary;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getGender() {
return gender;
}
public void setGender(String String) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
+ ", gender=" + gender + "]";
}
}
Now we will create a Converter class which implement the logic to bind String data to Property Editor for conversion
package com.example.typeconversion;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Converter {
public <T> T convert(Class<T> clazz, Request req) throws InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, IntrospectionException {
T instance = (T) clazz.newInstance();
BeanInfo info = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (PropertyDescriptor prop : props) {
System.out.println(prop.getName() + ":"
+ prop.getPropertyType().getName());
PropertyEditor editor = PropertyEditorManager.findEditor(prop
.getPropertyType());
System.out.println(editor);
if (editor == null) {
continue;
}
String value = req.getParameter(prop.getName());
editor.setAsText(value);
Method setter = prop.getWriteMethod();
setter.invoke(instance, new Object[] {editor.getValue() });
}
return instance;
}
}
look the convert method very carefully, Here pass the Object which we want to populate with the data, that is why you need to write FQDN of the class in Struts mapping.
Now we will create an instance of this class using reflection
After that, we extract the Property Descriptors from that Target class using BeanInfo object.
PropertyDescriptor holds the metadata of each property of the target Object here the Employee Object.
Then we try to get the PropertyEditor by passing the property type in PropertyEditorManager.
If we pass Property type as Double it returns DoubleEditor, a simple factory method pattern.
After that, we get the value from Request and set the String value into Property editor. One thing we consider here key name must be same with property name in Employee Object.
Atlast, we call Setter method using Reflection and set the converted value into the Employee POJO using Property editors getValue() method.
Voila, We are able to populate Employee Object
Test :
package com.example.typeconversion;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
public class Main {
public void testConversion() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException
{
Request req = new Request();
req.add("name", "Shamik").add("age", "33").add("salary", "10000").add("gender", "M");
Converter converter = new Converter();
Employee emp = converter.convert(Employee.class, req);
System.out.println("After Conversion " + emp);
}
public static void main(String[] args) {
Main main = new Main();
try {
main.testConversion();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output :
age:java.lang.Integer
com.sun.beans.editors.IntegerEditor@99ffac2
class:java.lang.Class
null
gender:java.lang.String
com.sun.beans.editors.StringEditor@372a6e85
name:java.lang.String
com.sun.beans.editors.StringEditor@42bdfa0e
salary:java.lang.Double
com.sun.beans.editors.DoubleEditor@697a9f24
After Conversion Employee [name=Shamik, age=33, salary=10000.0, gender=M]