7. GenericUpdater update() method.

Listing 7. GenericUpdater update() method.


public void update(Object target, String key)
{
  for (int i = 0; i < propertyNames.length; i++)
  {
    String getterMethodName = "get" +
                            propertyNames[i];
    String setterMethodName = "set" +
                            propertyNames[i];

    try
    {
       // e.g. Icon getIcon(String)
       Method getterMethod =
           resource.getClass().getMethod(
                getterMethodName, GETTER_PARAM);

       // return type of resource's getter is
       // parameter type of target's setter
       Class setterParam =
                   getterMethod.getReturnType();

       // e.g. void setIcon(Icon)
       Method setterMethod =
         target.getClass().getMethod(
            setterMethodName, 
            new Class[] {setterParam});

       // Invoke get
       Object resourceValue =
         getterMethod.invoke(
            resource, new Object[] {key});

       // Invoke set
       setterMethod.invoke(
           target, new Object[] {resourceValue});
    }
    catch ( ... various exceptions ...)
    { ... handled in various ways ...
    }
}