Java - Write code to set String Property vis Reflection

Requirements

Write code to set String Property vis Reflection

Demo

//package com.book2s;
import java.lang.reflect.InvocationTargetException;

public class Main {
    public static void main(String[] argv) {
        Object bean = new MyClass();
        String name = "name";
        String value = "book2s.com";
        setStringProperty(bean, name, value);
    }//from  w  w  w  .  j av  a 2 s  .  co  m

    public static void setStringProperty(Object bean, String name,
            String value) {
        String methodName = "set" + Character.toUpperCase(name.charAt(0))
                + name.substring(1);

        Class<?> clazz = bean.getClass();
        try {
            clazz.getMethod(methodName, String.class).invoke(bean, value);
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | SecurityException
                | NoSuchMethodException e) {
            // ignore
        }
    }
}

class MyClass{
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}