@Test public void getStrFieldTest() throws IOException { testObject.strField = "value2"; assertEquals("value2", ClassUtil.getFieldValue(testObject, "strField")); }
public static Object getFieldValue(Object obj, String field) throws IOException { try { PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); Method getter = desc.getReadMethod(); if (getter == null) { throw new IOException( String.format("No getter for the name \"%s\".", field)); } if (getter.getParameterCount() != 0) { throw new IOException( String.format("The getter \"%s\" for the name \"%s\" takes a parameter.", getter.getName(), field)); } return getter.invoke(obj); } catch (IntrospectionException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { throw new IOException( String.format("Unsupported operation with object of class %s for name \"%s\" - %s.", obj.getClass().toString(), field, ex.getCause() == null ? ex.getLocalizedMessage() : ex.getCause().getLocalizedMessage()), ex); } }
ClassUtil { public static Object getFieldValue(Object obj, String field) throws IOException { try { PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); Method getter = desc.getReadMethod(); if (getter == null) { throw new IOException( String.format("No getter for the name \"%s\".", field)); } if (getter.getParameterCount() != 0) { throw new IOException( String.format("The getter \"%s\" for the name \"%s\" takes a parameter.", getter.getName(), field)); } return getter.invoke(obj); } catch (IntrospectionException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { throw new IOException( String.format("Unsupported operation with object of class %s for name \"%s\" - %s.", obj.getClass().toString(), field, ex.getCause() == null ? ex.getLocalizedMessage() : ex.getCause().getLocalizedMessage()), ex); } } }
ClassUtil { public static Object getFieldValue(Object obj, String field) throws IOException { try { PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); Method getter = desc.getReadMethod(); if (getter == null) { throw new IOException( String.format("No getter for the name \"%s\".", field)); } if (getter.getParameterCount() != 0) { throw new IOException( String.format("The getter \"%s\" for the name \"%s\" takes a parameter.", getter.getName(), field)); } return getter.invoke(obj); } catch (IntrospectionException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { throw new IOException( String.format("Unsupported operation with object of class %s for name \"%s\" - %s.", obj.getClass().toString(), field, ex.getCause() == null ? ex.getLocalizedMessage() : ex.getCause().getLocalizedMessage()), ex); } } private  ClassUtil(); }
ClassUtil { public static Object getFieldValue(Object obj, String field) throws IOException { try { PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); Method getter = desc.getReadMethod(); if (getter == null) { throw new IOException( String.format("No getter for the name \"%s\".", field)); } if (getter.getParameterCount() != 0) { throw new IOException( String.format("The getter \"%s\" for the name \"%s\" takes a parameter.", getter.getName(), field)); } return getter.invoke(obj); } catch (IntrospectionException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { throw new IOException( String.format("Unsupported operation with object of class %s for name \"%s\" - %s.", obj.getClass().toString(), field, ex.getCause() == null ? ex.getLocalizedMessage() : ex.getCause().getLocalizedMessage()), ex); } } private  ClassUtil(); static void remarkTransientFields(Class<?> targetClass); static void setFieldValue(Object obj, String fieldName, String value); static void setFieldValue(Object obj, String fieldName, Object value); static Object getFieldValue(Object obj, String field); }
ClassUtil { public static Object getFieldValue(Object obj, String field) throws IOException { try { PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); Method getter = desc.getReadMethod(); if (getter == null) { throw new IOException( String.format("No getter for the name \"%s\".", field)); } if (getter.getParameterCount() != 0) { throw new IOException( String.format("The getter \"%s\" for the name \"%s\" takes a parameter.", getter.getName(), field)); } return getter.invoke(obj); } catch (IntrospectionException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { throw new IOException( String.format("Unsupported operation with object of class %s for name \"%s\" - %s.", obj.getClass().toString(), field, ex.getCause() == null ? ex.getLocalizedMessage() : ex.getCause().getLocalizedMessage()), ex); } } private  ClassUtil(); static void remarkTransientFields(Class<?> targetClass); static void setFieldValue(Object obj, String fieldName, String value); static void setFieldValue(Object obj, String fieldName, Object value); static Object getFieldValue(Object obj, String field);  }