All,

JUMP could do with a utility class similar to commons-beanutils that
would allow you to get/set properties on an object without using
introspection. The following is a naive implementation for getting a
property value. A real implementation would need to use caching to
improve performance and take into account class loader issues.

package com.vividsolutions.jump.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BeanUtil {

  private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
 
  public Object getProperty(Object object, String name) {
    if (object != null) {
      Class beanClass = object.getClass();
      try {
        Method method= getReadMethod(beanClass, name);
        if (method != null) {
          return method.invoke(object, EMPTY_OBJECT_ARRAY);
        }
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
    return null;
  }

  private Method getReadMethod(Class beanClass, String name) {
    PropertyDescriptor descriptor = getPropertyDescriptor(beanClass, name);
    if (descriptor != null) {
      return descriptor.getReadMethod();
    } else {
      return null;
    }
  }

  private PropertyDescriptor getPropertyDescriptor(Class beanClass,
String name) {
    try {
      BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
      PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
      for (int i = 0; i < props.length; i++) {
        PropertyDescriptor property = props[i];
        if (name.equals(property.getName())) {
          return property;
        }
      }
    } catch (IntrospectionException e) {
      e.printStackTrace();
    }
    return null;
  }
}


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to