List of utility methods to do Reflection Field Set
void | setFieldPrimitive(Object target, Field field, Object value) set Field Primitive try { final Class<?> c = field.getType(); if (c == boolean.class) { field.setBoolean(target, (Boolean) value); } else if (c == byte.class) { field.setByte(target, ((Number) value).byteValue()); } else if (c == char.class) { field.setChar(target, (Character) value); ... |
boolean | setFields(Field field, Object value, Object config) set Fields boolean flag = true; Class<?> clazz = field.getType(); try { switch (clazz.getName()) { case "boolean": field.set(config, Boolean.valueOf((String) value)); break; case "int": ... |
void | setFields(final Map Allows for easy injection of mocks into private or protected fields. for (Field field : target.getClass().getDeclaredFields()) { if (fields.containsKey(field.getName())) { try { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); if (Modifier.isFinal(field.getModifiers())) { modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); ... |
void | setFieldStatic(final Object pBean, final String fieldname, final Object pInject) Set field, primary for injects try { final Field field = pBean.getClass().getDeclaredField(fieldname); field.setAccessible(true); field.set(pBean, pInject); field.setAccessible(false); } catch (final Exception e) { try { final Field field = pBean.getClass().getSuperclass().getDeclaredField(fieldname); ... |
void | setFieldVal(Object object, String name, Object val) set Field Val Field field = getFieldByName(object.getClass(), name); field.setAccessible(true); try { field.set(object, val); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); ... |
void | setFieldValeByType(Field field, Object obj, String value) set Field Vale By Type Class<?> type = field.getType(); String typeName = type.getName(); if (typeName.equals("int")) { if (value.equals("")) { value = "0"; field.set(obj, Integer.valueOf(value)); } else if (typeName.equals("long")) { ... |
void | setFieldViaReflection(String fieldName, V value, T classInstance) Sets the value of a field for a given instance using Reflection API. Field targetField = classInstance.getClass().getDeclaredField(fieldName); targetField.setAccessible(true); targetField.set(classInstance, value); |
Field | setFieldVisible(Class extends Object> classToModify, String fieldName) Modify the visibility of a field. Field field = null; try { field = classToModify.getDeclaredField(fieldName); field.setAccessible(true); } catch (SecurityException e) { } catch (NoSuchFieldException e) { if (classToModify.getSuperclass() != null) { field = setFieldVisible(classToModify.getSuperclass(), fieldName); ... |
boolean | setFieldWithReflection(Object object, String fieldName, Object fieldValue) set Field With Reflection Class<?> clazz = object.getClass(); while (clazz != null) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, fieldValue); return true; } catch (NoSuchFieldException e) { ... |