ClassUtils { public static String getFieldValueAsString(Object obj, String fieldName) { return getFieldValueAsString(obj, fieldName, null); } private  ClassUtils(); static boolean canSetProperty(Object obj, String fieldName); static void clearFieldValue(Object obj, String fieldName, Class<?> argType); static Class<?> forClass(final String clazz); static T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static R getAnnotationAttributeValue(Field field, Class<T> annotationClass, String attributeName); static T getAnnotationOnField(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static T getAnnotationOnField(Field field, Class<T> annotationClass); static T getAnnotationOnMethod(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static T getAnnotationOnMethod(Method method, Class<T> annotationClass); static byte[] getBytes(Object obj, String fieldName); static Constructor<T> getConstructor(Class<T> clazz, Object... args); static Field getField(Class<?> clazz, String fieldName); static Object getFieldValue(Object obj, String fieldName); static String getFieldValueAsString(Object obj, String fieldName); static String getFieldValueAsString(Object obj, String fieldName, String defValue); static Method getGetterMethod(Class<?> clazz, String fieldName); static int getMaxLength(Class<?> clazz, String fieldName); static PropertyDescriptor getPropertyDescriptorForNestedProperty(Class<?> clazz, String property); static Class<?> getResolvedType(Class<T> type, String fieldName, int... indexes); static Class<?> getResolvedType(Object object, int... indexes); static boolean hasMethod(Object obj, String methodName); static T instantiateClass(Class<T> clazz, Object... args); static void setBytes(byte[] bytes, Object obj, String fieldName); static void setFieldValue(Object obj, String fieldName, Object value); static void copyFields(T from, S to);  }
@Test public void testGetFieldValueAsString() { TestEntity entity = new TestEntity(); entity.setAge(12L); assertEquals("12", ClassUtils.getFieldValueAsString(entity, "age")); entity.setAge(null); assertNull(ClassUtils.getFieldValueAsString(entity, "age")); }