List of utility methods to do Reflection Field Set
void | setField(Field field, U instance, Object value) set Field boolean isAccessible = field.isAccessible(); if (!isAccessible) { field.setAccessible(true); try { field.set(instance, value); } finally { field.setAccessible(isAccessible); ... |
void | setField(final Field field, final Object instance, final Object value) set Field if (field == null) { throw new RuntimeException("No such field"); field.setAccessible(true); try { field.set(instance, value); } catch (final Exception ex) { throw new RuntimeException(ex); ... |
void | setField(final Field field, final Object object, final Object value) Sets the supplied value of the supplied field on the supplied object. try { field.set(object, value); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); |
V | setField(final O object, final String fieldName, final V value) Sets a field on an object, even if the field would not normally be accessible (such as a private field). Field objectField = null; Class<?> workingClass = object.getClass(); while (objectField == null) { try { objectField = workingClass.getDeclaredField(fieldName); } catch (final NoSuchFieldException e) { if (workingClass == Object.class) { throw e; ... |
void | setField(final Object instance, final String fieldName, final T value) set Field Class<?> instanceClass = instance.getClass(); Field field = getDeclaredField(instanceClass, fieldName); while (field == null) { instanceClass = instanceClass.getSuperclass(); field = getDeclaredField(instanceClass, fieldName); field.setAccessible(true); try { ... |
void | setField(final Object obj, final String fieldName, final int value) set Field try { final Field field = obj.getClass().getField(fieldName); field.set(obj, new Integer(value)); } catch (final Throwable t) { |
void | setField(final Object obj, final String fieldName, final Object value) set Field Field field = null; try { field = obj.getClass().getDeclaredField(fieldName); } catch (NoSuchFieldException ex) { throw new IllegalArgumentException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); field.setAccessible(true); ... |
void | setField(final Object obj, final String name, final Object value) Set a (final) field during deserialization. try { final Field field = obj.getClass().getDeclaredField(name); if (field == null) { throw new IOException("Field " + name + " not found in class " + obj.getClass()); field.setAccessible(true); field.set(obj, value); } catch (final SecurityException e) { ... |
void | setField(final Object obj, final String variableName, final Object variableValue) Sets variable by reflection down to 3 levels of abstracts classes. Field declaredField = null; Class<?> clazz = obj.getClass(); for (int i = 0; i <= 3; i++) { try { declaredField = clazz.getDeclaredField(variableName); break; } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); ... |
void | setField(final Object object, final String name, final Object value) set Field try { final Field field = findField(object.getClass(), name); field.setAccessible(true); field.set(object, value); } catch (ReflectiveOperationException e) { throw new IllegalStateException(e); |