DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Java String: A Complete Guide With Examples
  • Java: A Time-Tested Programming Language Still Going Strong
  • Generics in Java and Their Implementation

Trending

  • Enhancing Observability With AI/ML
  • Performance of ULID and UUID in Postgres Database
  • Examining Use Cases for Asynchronous APIs: Webhooks and WebSockets
  • Micro Frontends Architecture
  1. DZone
  2. Coding
  3. Languages
  4. Automatic Type Binding in Java

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.

Shamik Mitra user avatar by
Shamik Mitra
·
Jan. 31, 17 · Tutorial
Like (5)
Save
Tweet
Share
11.3K Views

Join the DZone community and get the full member experience.

Join For Free

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]



Java (programming language) Object (computer science) Binding (linguistics)

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

Opinions expressed by DZone contributors are their own.

Related

  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Java String: A Complete Guide With Examples
  • Java: A Time-Tested Programming Language Still Going Strong
  • Generics in Java and Their Implementation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: