List of utility methods to do Reflection Field Set
void | setField(Object obj, String name, Object value) set Field try { getFieldAndEnsureItsAccessible(obj, name).set(obj, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); |
boolean | setField(Object obj, String name, Object value) Set the value of this parameter. String setterName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1); Method[] methods = obj.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals(setterName)) { try { methods[i].invoke(obj, new Object[] { value }); return true; } catch (Exception e) { ... |
void | setField(Object object, Class> clazz, String fieldName, Object value) set Field try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, value); } catch (Exception e) { throw new RuntimeException(e); |
boolean | setField(Object object, Class> clazz, String fieldName, Object value) Sets the value of a field in an object if (!clazz.isAssignableFrom(object.getClass())) return false; Field field = Arrays.stream(clazz.getDeclaredFields()).filter(f -> f.getName().equals(fieldName)) .findFirst().orElse(null); return setField(object, field, value); |
void | setField(Object object, Field field, Object value) Sets the value of a field for a given object, regardless if the field is accessible. boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); try { field.set(object, value); } catch (IllegalAccessException shouldNeverHappen) { shouldNeverHappen.printStackTrace(); ... |
void | setField(Object object, Field field, Object value) Sets the given value for the field inside the given object Throws all exceptions that can be thrown by Field.set Changes the accessibility of the field if it is false boolean changedAccessible = false; boolean isAccessible = field.isAccessible(); if (!isAccessible) { changedAccessible = true; field.setAccessible(true); field.set(object, value); if (changedAccessible) { ... |
void | setField(Object object, Object value, String name) Set the value of the field 'name' to be 'value' on object 'object'. ReflectiveOperationException firstError = null; Class<? extends Object> clazz = object.getClass(); while (clazz != null) { try { Field field = clazz.getDeclaredField(name); field.setAccessible(true); field.set(object, value); clazz = null; ... |
void | setField(Object object, String field, Object value) set Field if (object == null || field == null) return; try { Field target = object.getClass().getDeclaredField(field); target.setAccessible(true); target.set(object, value); } catch (Exception ignored) { |
void | setField(Object object, String fieldName, int value) Sets a field value for the given object. setField(object, fieldName, Integer.valueOf(value)); |
void | setField(Object object, String fieldName, Object newValue) set Field try { Field f = object.getClass().getDeclaredField(fieldName); if (f != null) { f.setAccessible(true); f.set(object, newValue); } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { ... |