Java Reflection Field Value Set setFieldValue(Object target, String fieldName, Object fieldValue)

Here you can find the source of setFieldValue(Object target, String fieldName, Object fieldValue)

Description

Sets the field value by using reflection.

License

Apache License

Parameter

Parameter Description
target The object whose field value is set.
fieldName The name of the field whose value is set.
fieldValue The new field value.

Declaration

public static void setFieldValue(Object target, String fieldName, Object fieldValue) 

Method Source Code


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

import java.lang.reflect.Field;

public class Main {
    /**/*  w w w .  jav  a2 s .  c  o  m*/
     * Sets the field value by using reflection.
     * @param target        The object whose field value is set.
     * @param fieldName     The name of the field whose value is set.
     * @param fieldValue    The new field value.
     */
    public static void setFieldValue(Object target, String fieldName, Object fieldValue) {
        try {
            Field targetField = findFieldFromClassHierarchy(target.getClass(), fieldName);
            targetField.setAccessible(true);
            targetField.set(target, fieldValue);
        } catch (Exception ex) {
            throw new RuntimeException(
                    String.format("Cannot set the value of the field: %s because of an error", fieldName), ex);
        }
    }

    /**
     * Finds the requested field from the class hierarchy.
     * @param clazz     The type of the actual object.
     * @param fieldName The name of the requested field.
     * @return
     */
    private static Field findFieldFromClassHierarchy(Class<?> clazz, String fieldName) throws NoSuchFieldException {
        Class<?> current = clazz;

        //Iterate the class hierarchy from the actual class
        //to the super class and try to find the requested field.
        do {
            try {
                return current.getDeclaredField(fieldName);
            } catch (Exception e) {
                //An exception simply means that field is not found.
            }
        } while ((current = current.getSuperclass()) != null);

        //This signals that the requested field is not found.
        throw new NoSuchFieldException(String.format("No field found with the field name %s", fieldName));
    }
}

Related

  1. setFieldValue(Object target, Class targetClass, String fieldName, Object value)
  2. setFieldValue(Object target, Field field, Object newValue)
  3. setFieldValue(Object target, Field field, Object value)
  4. setFieldValue(Object target, Object mock, final Field field)
  5. setFieldValue(Object target, String field, Object value)
  6. setFieldValue(Object target, String fieldName, Object value)
  7. setFieldValue(Object target, String fieldName, Object value)
  8. setFieldValue(Object target, String fieldName, String fieldValue)
  9. setFieldValue(Object target, String fname, Class ftype, Object fvalue)