Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automatic Type Binding in Java

DZone's Guide to

Automatic Type Binding in Java

Using the PropertyEditor interface, and its various support classes and methods, you can set up automatic type binding for your work.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

With frameworks like Struts and Spring, we have seen that we can send some parameters through an HTTP request from a UI. They automatically typecast the parameters and bind them to an object, say ActionForm in Struts.

How was that done? Well, here's how the magic happens. As we know, from the request, we always get a string value.

Java has a nice little interface called PropertyEditor under the java.beans package.

This PropertyEditor has two main methods.

  1. setAsText(String val)
  2. Object getValue();

With the help of these two methods, we can do magic.

The setAsText method accepts any value as a string, and the getValue method actually does the conversion and returns the actual type.

There are many inbuilt editors in Java, like DoubleEditor, BooleanEditor, etc.

These editors extend the PropertyEditorSupport class, which is nothing but a skeletal implementation that implements the PropertyEditor interface. So, you can create your own custom editor by extending PropertyEditorSupport.

Here, I will create a simple example where I bind some string parameters to an Employee object.

For the sake of simplicity, I used Java inbuilt editors.

Step 1: Create a Request Object That Takes Keys and Values as Strings.

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 an 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 + "]";
   }
}


Step 3: Create a Converter Class

Now we will create a Converter class that implements the logic to bind string data to the 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 we want to populate with the data. That is why you need to write the 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 a 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 the 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 in PropertyEditor. One thing to consider here is that the key name must be same as the property name in the Employee object.

At last, we call a setter method using reflection and set the converted value into the Employee POJO using the Property editors.getValue() method.

Voila, we are able to populate the 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]



Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,type casting ,binding ,tutorial

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}