Java Reflection Field Value Set setFieldValueWithPath(Object object, String path, Object value)

Here you can find the source of setFieldValueWithPath(Object object, String path, Object value)

Description

Returns the value of a field identified using a path from the parent.

License

Apache License

Parameter

Parameter Description
object Parent object.
path Path to identify the field. May contain one or more dots, e.g. "address.city".
value Value to which to set the field.

Exception

Parameter Description
IllegalStateException if one of the intermediate objects on the path is null.

Declaration

public static void setFieldValueWithPath(Object object, String path, Object value)
        throws IllegalStateException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    /**/*w  w  w.jav a  2 s. co m*/
     * Returns the value of a field identified using a path from the parent.
     *
     *
     * @param object
     *            Parent object.
     * @param path
     *            Path to identify the field. May contain one or more dots, e.g.
     *            "address.city".
     * @param value
     *            Value to which to set the field.
     * @throws IllegalStateException
     *             if one of the intermediate objects on the path is null.
     */
    public static void setFieldValueWithPath(Object object, String path, Object value)
            throws IllegalStateException {

        int lastDot = path.lastIndexOf('.');

        if (lastDot > -1) {

            String parentPath = path.substring(0, lastDot);
            String field = path.substring(lastDot + 1);
            Object parentObject = getFieldValueWithPath(object, parentPath);

            if (parentObject == null) {
                throw new IllegalStateException(String.format("Null value for %s while accessing %s on object %s",
                        parentPath, path, object));
            }

            setFieldValue(parentObject, field, value);

        } else {
            setFieldValue(object, path, value);
        }

    }

    /**
     * Returns the value of a field identified using a path from the parent.
     *
     * @param object
     *            Parent object.
     * @param path
     *            Path to identify the field. May contain one or more dots, e.g.
     *            "address.city".
     * @return The value of the field, or null if any of the path components are
     *         null.
     */
    public static Object getFieldValueWithPath(Object object, String path) {

        int lastDot = path.lastIndexOf('.');

        if (lastDot > -1) {

            String parentPath = path.substring(0, lastDot);
            String field = path.substring(lastDot + 1);

            Object parentObject = getFieldValueWithPath(object, parentPath);

            if (parentObject == null) {
                return null;
            } else {
                return getFieldValue(parentObject, field);
            }
        } else {
            return getFieldValue(object, path);
        }

    }

    /**
     * Convenience method for setting the value of a private object field,
     * without the stress of checked exceptions in the reflection API.
     *
     * @param object
     *            Object containing the field.
     * @param fieldName
     *            Name of the field to set.
     * @param value
     *            Value to which to set the field.
     */
    public static void setFieldValue(Object object, String fieldName, Object value) {
        try {
            getDeclaredFieldInHierarchy(object.getClass(), fieldName).set(object, value);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Convenience method for getting the value of a private object field,
     * without the stress of checked exceptions in the reflection API.
     *
     * @param object
     *            Object containing the field.
     * @param fieldName
     *            Name of the field whose value to return.
     */
    public static Object getFieldValue(Object object, String fieldName) {
        try {
            return getDeclaredFieldInHierarchy(object.getClass(), fieldName).get(object);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static Field getDeclaredFieldInHierarchy(Class<?> clazz, String fieldName) {
        while (true) {
            try {
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field;
            } catch (SecurityException e) {
                throw new RuntimeException(e);
            } catch (NoSuchFieldException e) {
                if (clazz == Object.class) {
                    throw new RuntimeException(e);
                } else {
                    clazz = clazz.getSuperclass();
                }
            }
        }
    }
}

Related

  1. setFieldValue(String name, String obfuscatedName, Class clazz, Object object, Object value)
  2. setFieldvalue(T instance, E value, Field field)
  3. setFieldValue_internal(Object bean, Field field, Object value)
  4. setFieldValueForObject(Object object, String fieldName, Object value)
  5. setFieldValues(Object bean, Map valMap)
  6. setFieldValueWithSetterMethod(Object target, Object value, Class clazz, Field field)
  7. setFieldValueX(Object target, String field, Object value)