Java Reflection Field Value Set setFieldValue(Class clazz, Object instance, String fieldName, T value)

Here you can find the source of setFieldValue(Class clazz, Object instance, String fieldName, T value)

Description

Sets the value of a field using Reflection API.

License

Open Source License

Parameter

Parameter Description
clazz the class the instance is an 'instanceof'
instance the instance to get the field value from
fieldName the name of the field
value the new value to be set for the field
T the type of the field

Declaration

public static <T> void setFieldValue(Class clazz, Object instance, String fieldName, T value) 

Method Source Code


//package com.java2s;
/*/*from   w  w w. j av a 2 s.c  o  m*/
 * Copyright (C) 2013 Pascal Mazars
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Field;

public class Main {
    /**
     * Sets the value of a field using Reflection API.
     * The class the instance is an 'instanceof' is evaluated at runtime.
     * @param instance the instance to get the field value from
     * @param fieldName the name of the field
     * @param value the new value to be set for the field
     * @param <T> the type of the field
     */
    public static <T> void setFieldValue(Object instance, String fieldName, T value) {
        setFieldValue(instance.getClass(), instance, fieldName, value);
    }

    /**
     * Sets the value of a field using Reflection API.
     * @param clazz the class the instance is an 'instanceof'
     * @param instance the instance to get the field value from
     * @param fieldName the name of the field
     * @param value the new value to be set for the field
     * @param <T> the type of the field
     */
    public static <T> void setFieldValue(Class clazz, Object instance, String fieldName, T value) {
        try {
            getField(clazz, fieldName).set(instance, value);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * We cannot use <code>clazz.getField(String fieldName)</code> because it only returns public fields.
     */
    public static Field getField(Class clazz, String fieldName) {
        Class currentClass = clazz;
        while (currentClass != null) {
            try {
                Field field = currentClass.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field;
            } catch (NoSuchFieldException e) {
                currentClass = currentClass.getSuperclass();
            }
        }
        throw new RuntimeException(new NoSuchFieldException(fieldName));
    }
}

Related

  1. setFieldValue(Class c, Object instance, String name, Object value)
  2. setFieldValue(Class clazz, Object target, String fieldName, Object value)
  3. setFieldValue(Class clazz, E instance, Object value, String... names)
  4. setFieldValue(Class clazz, Object entity, String fieldName, V value)
  5. setFieldValue(Class clazz, String fieldName, Object value)