Java Reflection Field Set setFields(final Map fields, final Object target)

Here you can find the source of setFields(final Map fields, final Object target)

Description

Allows for easy injection of mocks into private or protected fields.

License

Open Source License

Parameter

Parameter Description
fields Fields to be injected into the target object
target Object which will have mocks injected into it

Declaration

public static void setFields(final Map<String, Object> fields, final Object target) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;

public class Main {
    /**//  ww w. j  av  a2s  .  c  om
     * Allows for easy injection of mocks into private or protected fields.
     * <br/>
     * Example:
     * <pre>
     *     TestContainer container = new TestContainer();
     *     Map<String, Object> fields = new HashMap<String, Object>();
     *     TestObject mockTestObject = mock(TestObject.class);
     *     fields.put("privateMemberName", mockTestObject);
     *     MockUtils.setFields(fields, container);
     * </pre>
     * 
     * <b>This method can be used on private fields, static fields, or
     * final fields</b>
     *
     * @param fields Fields to be injected into the target object
     * @param target Object which will have mocks injected into it
     */
    public static void setFields(final Map<String, Object> fields, final Object target) {
        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);
                    }
                    if (Modifier.isStatic(field.getModifiers())) {
                        field.set(null, fields.get(field.getName()));
                    } else {
                        field.set(target, fields.get(field.getName()));
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

  1. setFieldObject(Class clazz, String field, Object object, Object newObj)
  2. setFieldObject(Class target, Object targetObject, String fieldName, Object object)
  3. setFieldObjectValue(Class targetClass, Object target, String fieldName, Object value)
  4. setFieldPrimitive(Object target, Field field, Object value)
  5. setFields(Field field, Object value, Object config)
  6. setFieldStatic(final Object pBean, final String fieldname, final Object pInject)
  7. setFieldVal(Object object, String name, Object val)
  8. setFieldValeByType(Field field, Object obj, String value)
  9. setFieldViaReflection(String fieldName, V value, T classInstance)