Convenience method for getting the value of a private object field,
without the stress of checked exceptions in the reflection API.

@param object
Object containing the field.
@param fieldName
Name of the field whose value to return.
15563
public static Object getFieldValue(Object object, String fieldName) {
        try {
            return getDeclaredFieldInHierarchy(object.getClass(), fieldName).get(object);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
3459
private static void addFieldToJsonObject(Method getter, Object o,
                                             String fieldName, JsonObjectBuilder j) {
        try {
            Object fieldValue = getter.invoke(o);

            if (fieldValue == null) {
            } else if (fieldValue instanceof String) {
                j.add(fieldName, (String) fieldValue);
            } else if (fieldValue instanceof Boolean) {
                j.add(fieldName, (Boolean) fieldValue);
            } else if (fieldValue instanceof BigInteger) {
                j.add(fieldName, (BigInteger) fieldValue);
            } else if (fieldValue instanceof java.math.BigDecimal) {
                j.add(fieldName, (BigDecimal) fieldValue);
            } else if (fieldValue instanceof Double) {
                j.add(fieldName, (Double) fieldValue);
            } else if (fieldValue instanceof Integer) {
                j.add(fieldName, (Integer) fieldValue);
            } else if (fieldValue instanceof Long) {
                j.add(fieldName, (Long) fieldValue);
            } else if (fieldValue instanceof Short) {
                j.add(fieldName, (Short) fieldValue);
            } else if (fieldValue instanceof Byte) {
                j.add(fieldName, (Byte) fieldValue);

            } else if (fieldValue instanceof Enum) {
                try {
                    // enums need careful handling.. look for get/set Value to use.
                    Method getValueForEnumMethod = fieldValue.getClass().getMethod("getValue");
                    Object valueFromGetValueMethod = getValueForEnumMethod.invoke(fieldValue);
                    j.add(fieldName, valueFromGetValueMethod.toString());
                } catch (NoSuchMethodException e) {
                    // else fallback to toString
                    j.add(fieldName, fieldValue.toString());
                }
            } else if (fieldValue instanceof Collection
                       || fieldValue.getClass().getName().startsWith(DATA_MODEL_PACKAGE)) {
                JSONArtrifactPair fieldObjects = findFieldsToSerialize(fieldValue);
                j.add(fieldName, fieldObjects.mainObject);
                if (fieldObjects.incompatibleFieldsObject != null && !fieldObjects.incompatibleFieldsObject.isEmpty()) {
                    // As per the contract on the HasBreakingChanges interface store incompatible fields in a second object
                    j.add(fieldName + "2", fieldObjects.incompatibleFieldsObject);
                }
            } else if (fieldValue instanceof Calendar) {
                Date date = ((Calendar) fieldValue).getTime();
                j.add(fieldName, getDateFormat().format(date));
            } else if (fieldValue instanceof Date) {
                j.add(fieldName, getDateFormat().format((Date) fieldValue));
            } else if (fieldValue instanceof Locale) {
                //note that Locale toString is a bit fiddly to round trip, but it's the best we have for now.
                String localeString = ((Locale) fieldValue).toString();
                j.add(fieldName, localeString);
            } else {
                throw new IllegalStateException("Data Model Error: Unknown data model entity "
                                                + getter.getName() + " of type " + fieldValue.getClass().getName() + " on "
                                                + o.getClass().getName());
            }

        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Data Model Error: unable to invoke getter "
                                            + getter.getName() + " on "
                                            + o.getClass().getName(), e);
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException("Data Model Error: unable to invoke getter "
                                            + getter.getName() + " on "
                                            + o.getClass().getName(), e);
        } catch (InvocationTargetException e) {
            throw new IllegalStateException("Data Model Error: unable to invoke getter "
                                            + getter.getName() + " on "
                                            + o.getClass().getName(), e);
        } catch (JsonException e) {
            // Shouldn't happen unless field name is null or the value cannot be converted
            throw new IllegalStateException("Data Model Error: unable to invoke getter "
                                            + getter.getName() + " on "
                                            + o.getClass().getName(), e);
        }
    }
987879
public static Field getField(String fieldName, Object object) {
    return getField(fieldName, object.getClass());
  }
107377
@SuppressWarnings("unchecked")
  public static <T, O> O getFieldValue(Class<T> clz, T t, String fieldName) throws NoSuchFieldException {
    Field field = clz.getDeclaredField(fieldName);
    field.setAccessible(true);
    try {
      return (O) field.get(t);
    } catch (IllegalAccessException e) {
      throw new IllegalStateException(e);
    }
  }
883971
public static <T> T getFieldValue(final Object obj, final Field field) {
		try {
			return (T) field.get(obj);
		} catch (Exception e) {
			throw convertReflectionExceptionToUnchecked(e);
		}
	}
476513
public Object getFieldValue(final Object instance) {
        try {
            return field.get(instance);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
1198608
public Reflect get(@NonNull String fieldName) throws ReflectionException {
      Field f = getField(fieldName);

      if (f == null) {
         throw new ReflectionException(new NoSuchFieldException(fieldName + " is not a valid field for " + clazz));
      }

      boolean isAccessible = f.isAccessible();
      try {
         if (accessAll) {
            f.setAccessible(true);
         }
         return onObject(f.get(object));
      } catch (IllegalAccessException e) {
         e.printStackTrace();
         throw new ReflectionException(e);
      } finally {
         f.setAccessible(isAccessible);
      }

   }
1081322
private Object getField(Object object, Field f) {
        f.setAccessible(true);
        try {
            return f.get(object);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
686402
public static boolean setField(Object object, String fieldName, Object value) {
		char c = fieldName.charAt(0);

		if (c >= 'a' && c <= 'z') {
			c -= 'a' - 'A';
			fieldName = c + fieldName.substring(1);
		}

		String methodName = "set" + fieldName;
		Class<?>[] returnType = new Class<?>[1];
		Class<?> objectClass = object.getClass();

		Method getMethod = getMethod(objectClass, "get" + fieldName);

		if (getMethod == null) {
			getMethod = getMethod(objectClass, "is" + fieldName);
		}

		if (getMethod == null) {
			return false;
		}

		returnType[0] = getMethod.getReturnType();

		return silentInvoke(object, methodName, returnType, value);
	}
803864
private Object getFieldValue(Object oldInstance, String fieldName) throws NoSuchFieldException, IllegalAccessException
	{
		Class<? extends Object> c = oldInstance.getClass();
		final Field f = c.getDeclaredField(fieldName);
		AccessController.doPrivileged(new PrivilegedAction<Object>() {
			public Object run()
			{
				f.setAccessible(true);
				return null;
			}
		});
		return f.get(oldInstance);
	}
4362
public static Field getField(Class cls, String fieldname) throws NoSuchFieldException
    {
        while (true)
        {
            try
            {
                return cls.getDeclaredField(fieldname);
            }
            catch (NoSuchFieldException ex)
            {
                cls = cls.getSuperclass();
                if (Object.class.equals(cls))
                {
                    throw ex;
                }
            }
            catch (SecurityException ex)
            {
                throw new IllegalArgumentException(ex);
            }
        }
    }
129310
private static Object getFieldValue(final Object classInstance, final Field classField) {
    try {
      return classField.get(classInstance);
    } catch (IllegalArgumentException ex) {
      throw new JBBPMapperException("Can't set get value from a mapping field", null, classInstance.getClass(), classField, ex);
    } catch (IllegalAccessException ex) {
      throw new JBBPMapperException("Can't get access to a mapping field", null, classInstance.getClass(), classField, ex);
    }
  }
552919
public DOField getFieldByName(String fieldName) {
		int idx = this.getIndexOfField(fieldName);
		if (idx != -1)
			return this.getFieldByIndex(idx);
		return null;
	}
879653
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
		Field field = getField(obj.getClass(), fieldName);
		if (field == null) {
			throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + ']');
		}
		setField(obj, field, value);
	}
49278
static String readAnyField(Object obj, String ... names) {
        try {
            for (String name : names) {
                try {
                    Field field = obj.getClass().getDeclaredField(name);
                    field.setAccessible(true);
                    return (String) field.get(obj);
                } catch (NoSuchFieldException e) {
                    //ignore and try next field...
                }
            }
            return null;
        } catch (IllegalAccessException e) {
            return null;
        }
    }
199585
public static function getPrivateValue($fieldName, $obj)
    {
        $r = new \ReflectionObject($obj);

        while (true) {
            if (!$r->hasProperty($fieldName)) {
                $p = $r->getParentClass();
                if (!$p) {
                    break;
                } else {
                    $r = $p;
                }
            } else {
                break;
            }
        }
        $sec = $r->getProperty($fieldName);
        $sec->setAccessible(true);
        $value = $sec->getValue($obj);
        return $value;
    }
1246726
private static Field getDeclaredField(Object object, String fieldName) throws NoSuchFieldException {
        Assert.notNull(object);
        return getDeclaredField(object.getClass(), fieldName);
    }