How to invoke(call) Setter and Getter method of field(variable) using Reflection?

UPDATED: 28 April 2015

In my previous article we've seen How to get fields(variables) name and type of class using Reflection?. Now its time to know how you can set value and get value of field using reflection.

Source Code
I've created some sample bean for example.
/* Actor.java */
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Actor {
   private long id;
   private String Name;
   private Set Films;
   private List LanguageSpeaks;
   private Map dummyMap;
}

public String getName() {
   return Name;
}
public void setName(String name) {
   Name = name;
}
/* Other Setter and Getter methods */

Source Code
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

public class InvokeSetterGetter {

   public static void main(String[] args) {
    /* Create object of Actor. */
    Actor objActor = new Actor();

    InvokeSetterGetter objInvokeSetterGetter = new InvokeSetterGetter();
    /* Call invokeSetter method */
    objInvokeSetterGetter.invokeSetter(objActor, "Name", "Benedict Cumberbatch");
    /* Call invokeGetter method */
    objInvokeSetterGetter.invokeGetter(objActor, "Name");
   }
 
   private void invokeSetter(Object obj, String variableName, Object variableValue){
      /* variableValue is Object because value can be an Object, Integer, String, etc... */
      try {
        /**
         * Get object of PropertyDescriptor using variable name and class
         * Note: To use PropertyDescriptor on any field/variable, the field must have both `Setter` and `Getter` method.
         */
         PropertyDescriptor objPropertyDescriptor = new PropertyDescriptor(variableName, obj.getClass());
         /* Set field/variable value using getWriteMethod() */
         objPropertyDescriptor.getWriteMethod().invoke(obj, variableValue);
      } catch (IllegalAccessException | IllegalArgumentException
        | InvocationTargetException | IntrospectionException e) {
        /* Java 8: Multiple exception in one catch. Use Different catch block for lower version. */
        e.printStackTrace();
      }
   }

   private void invokeGetter(Object obj, String variableName){
      try {
        /**
         * Get object of PropertyDescriptor using variable name and class
         * Note: To use PropertyDescriptor on any field/variable, the field must have both `Setter` and `Getter` method.
         */
         PropertyDescriptor objPropertyDescriptor = new PropertyDescriptor(variableName, obj.getClass());
        /**
         * Get field/variable value using getReadMethod()
         * variableValue is Object because value can be an Object, Integer, String, etc...
         */
         Object variableValue = objPropertyDescriptor.getReadMethod().invoke(obj);
        /* Print value of variable */
         System.out.println(variableValue);
      } catch (IllegalAccessException | IllegalArgumentException
        | InvocationTargetException | IntrospectionException e) {
       /* Java 8: Multiple exception in one catch. Use Different catch block for lower version. */
        e.printStackTrace();
      }
   }
}

0 comments :