Return to Snippet

Revision: 33947
at October 15, 2010 02:37 by peceps


Initial Code
/**
   * Gets value from object using reflection.
   * 
   * @param object the object
   * @param fieldName the field
   * @return the value
   */
  protected String getValueFromObject(Object object, String fieldName) {

    // get class
    Class clazz = object != null ? object.getClass() : null;
    if (clazz == null) {
      return null;
    }

    // get object value using reflection
    String getterName = "get" + fieldName;
    try {
      @SuppressWarnings("unchecked")
      Method method = clazz.getMethod(getterName);
      Object valueObject = method.invoke(object, (Object[]) null);
      return valueObject != null ? valueObject.toString() : "";
    } catch (Exception e) {
      // ignore all reflection errors
    }

    return null;
  }

Initial URL

                                

Initial Description

                                

Initial Title
Get value from object using getter via reflection

Initial Tags

                                

Initial Language
Java