List of utility methods to do Reflection Field Value Set
void | setFieldValue(Object target, Class extends Object> targetClass, String fieldName, Object value) set Field Value try { Field field = getAccessibleField((target != null) ? target.getClass() : targetClass, fieldName); field.set(target, value); } catch (Exception exception) { boolean instanceFieldRequested = (target != null); throw new IllegalAccessException(String.format("Unable to set field '%s' on %s '%s' due to %s: %s", fieldName, instanceFieldRequested ? "object" : "class", instanceFieldRequested ? target : targetClass.getName(), exception.getClass().getSimpleName(), ... |
void | setFieldValue(Object target, Field field, Object newValue) set Field Value boolean accessible = field.isAccessible(); field.setAccessible(true); try { field.set(target, newValue); } catch (IllegalAccessException e) { throw new NoSuchFieldException("No such field: " + target.getClass() + '.' + field.getName()); } finally { field.setAccessible(accessible); ... |
void | setFieldValue(Object target, Field field, Object value) set Field Value if (field.isAccessible()) { field.set(target, value); } else { field.setAccessible(true); field.set(target, value); field.setAccessible(false); |
void | setFieldValue(Object target, Object mock, final Field field) set Field Value field.setAccessible(true); removeFinalModifier(field); field.set(target, mock); |
void | setFieldValue(Object target, String field, Object value) set Field Value try { Class<?> obj = target.getClass(); Field[] fields = obj.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); if (field.equals(fields[i].getName())) { fields[i].set(target, value); break; ... |
void | setFieldValue(Object target, String fieldName, Object fieldValue) Sets the field value by using reflection. try { Field targetField = findFieldFromClassHierarchy(target.getClass(), fieldName); targetField.setAccessible(true); targetField.set(target, fieldValue); } catch (Exception ex) { throw new RuntimeException( String.format("Cannot set the value of the field: %s because of an error", fieldName), ex); |
void | setFieldValue(Object target, String fieldName, Object value) set Field Value try { getField(target, fieldName).set(target, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (NoSuchFieldException e) { throw new RuntimeException(e); |
boolean | setFieldValue(Object target, String fieldName, Object value) set Field Value return setFieldValue(target, fieldName, value, false);
|
void | setFieldValue(Object target, String fieldName, String fieldValue) set Field Value Field field = getField(target, fieldName); if (field != null) { field.setAccessible(true); try { field.set(target, fieldValue); } catch (IllegalAccessException e) { throw new RuntimeException(e); |
void | setFieldValue(Object target, String fname, Class ftype, Object fvalue) set Field Value if ((target == null) || (fname == null) || ("".equals(fname)) || ((fvalue != null) && (!ftype.isAssignableFrom(fvalue.getClass())))) { return; Class clazz = target.getClass(); try { Method method = clazz.getDeclaredMethod( "set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), new Class[] { ftype }); ... |