/** * Inserts a value for the named field * * @param fieldName the name of the field * @param value the field value to set * @throws IllegalAccessException This should never happen, since the field is always made * accessible. * @throws NoSuchFieldException thrown if field cannot be located */ public void setValue(String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = getFieldByName(fieldName); MakeFieldAccessible makeFieldAccessible = new MakeFieldAccessible(field); field.set(instance, value); makeFieldAccessible.restoreAccessState(); }
/** * Get the value for the named field * * @param fieldName the name of the field * @return the field value * @throws IllegalAccessException if the underlying field is inaccessible. * @throws NoSuchFieldException thrown if field cannot be located */ public Object getValue(String fieldName) throws IllegalAccessException, NoSuchFieldException { Field field = getFieldByName(fieldName); MakeFieldAccessible makeFieldAccessible = new MakeFieldAccessible(field); Object returnValue = field.get(instance); makeFieldAccessible.restoreAccessState(); return returnValue; }
/** * Inserts a value for the matching field by looking at the value class type. * * @param value the field value to set * @throws IllegalAccessException if the underlying field is inaccessible. * @throws NoSuchFieldException thrown if field cannot be located */ public void setValue(Object value) throws IllegalAccessException, NoSuchFieldException { checkForObjectValueClass(value.getClass()); Field field = getFieldByType(value.getClass()); MakeFieldAccessible makeFieldAccessible = new MakeFieldAccessible(field); field.set(instance, value); makeFieldAccessible.restoreAccessState(); }
/** * Get the value for the matching field by looking at class type. * * @param valueClass the class type of field * @param <T> the type of the field * @return the field value * @throws IllegalAccessException This should never happen, since the field is always made * accessible. * @throws NoSuchFieldException thrown if field cannot be located */ @SuppressWarnings("unchecked") public <T> T getValue(Class<T> valueClass) throws IllegalAccessException, NoSuchFieldException { checkForObjectValueClass(valueClass); Field field = getFieldByType(valueClass); MakeFieldAccessible makeFieldAccessible = new MakeFieldAccessible(field); Object returnValue = field.get(instance); makeFieldAccessible.restoreAccessState(); return (T) returnValue; }