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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Trending

  • Scalability 101: How to Build, Measure, and Improve It
  • How to Convert Between PDF and TIFF in Java
  • Start Coding With Google Cloud Workstations
  • Why I Started Using Dependency Injection in Python
  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.

By 
Shamik Mitra user avatar
Shamik Mitra
·
Jan. 31, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
11.7K 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

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!