List of utility methods to do Reflection Field Set
void | setFieldAccessible(Field field) set Field Accessible if (!field.isAccessible()) {
field.setAccessible(true);
|
void | setFieldBySomeMethod(List set Field By Some Method Object t = getFieldBySomeMethod(setMethods, object); setMethods.get(setMethods.size() - 1).invoke(t, value); |
void | setFieldContent(final Object obj, final String name, final Object value) This method sets the contents of an object variable. final Field field = getField(obj, name); try { field.set(obj, value); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); |
Field | setFieldEditable(Class> clazz, String fieldName) Sets a field in a class to be modifiable regardless of finality or privacy try { Field targetField = clazz.getDeclaredField(fieldName); Field modifierField = Field.class.getDeclaredField("modifiers"); targetField.setAccessible(true); modifierField.setAccessible(true); modifierField.setInt(targetField, targetField.getModifiers() & ~Modifier.FINAL); return targetField; } catch (Exception ex) { ... |
void | setFieldForAnnotation(Object target, Class extends Annotation> annotation, Object value) set Field For Annotation boolean found = setFieldForAnnotation(target, annotation, value, target.getClass()); if (!found) { Class superClass = target.getClass().getSuperclass(); while (!found) { found = setFieldForAnnotation(target, annotation, value, superClass); if (!found) { superClass = superClass.getSuperclass(); if (superClass == Object.class || superClass == null) { break; |
boolean | setFieldHelper(Object o, String name, Object v) set Field Helper try { Class c = o.getClass(); Field f = null; while (f == null) { if (c != null) { try { f = c.getDeclaredField(name); } catch (Throwable t) { ... |
boolean | setFieldIfExists(final Object instance, final String fieldName, final Object value) set Field If Exists try { final Field f = instance.getClass().getField(fieldName); if (value instanceof Boolean || f.getType().isInstance(value)) { f.set(instance, value); return true; } else { System.out.println(instance.getClass() + " '" + fieldName + "' field not assignable with " + value.getClass() + ", it's a: " + f.getType()); ... |
void | setFieldObject(Class> clazz, String field, Object object, Object newObj) set Field Object try { Field f = clazz.getDeclaredField(field); f.setAccessible(true); f.set(object, newObj); } catch (Exception ex) { |
void | setFieldObject(Class> target, Object targetObject, String fieldName, Object object) set Field Object try { Field field = target.getDeclaredField(fieldName); field.setAccessible(true); Field field1 = Field.class.getDeclaredField("modifiers"); field1.setAccessible(true); field1.set(field, field.getModifiers() & ~Modifier.FINAL); field.set(targetObject, object); } catch (Exception e) { ... |
void | setFieldObjectValue(Class> targetClass, Object target, String fieldName, Object value) Reflection utility: sets the field value of given object using target class. try { Field field = targetClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(target, value); } catch (Throwable e) { throw new RuntimeException( "Unable to get field '" + fieldName + "' from class " + targetClass.getName(), e); |