Java Utililty Methods Reflection Field Set

List of utility methods to do Reflection Field Set

Description

The list of methods to do Reflection Field Set are organized into topic(s).

Method

voidsetFieldPrimitive(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);
...
booleansetFields(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":
...
voidsetFields(final Map fields, final Object target)
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);
...
voidsetFieldStatic(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);
...
voidsetFieldVal(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();
...
voidsetFieldValeByType(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")) {
...
voidsetFieldViaReflection(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);
FieldsetFieldVisible(Class 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);
...
booleansetFieldWithReflection(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) {
...