Java Reflection Field Value Set setFieldValue(Object object, String fieldName, Object value)

Here you can find the source of setFieldValue(Object object, String fieldName, Object value)

Description

set Field Value

License

Open Source License

Declaration

public static void setFieldValue(Object object, String fieldName, Object value)
        throws NoSuchFieldException, IllegalAccessException 

Method Source Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {

    public static void setFieldValue(Object object, String fieldName, Object value)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = getDeclaredField(object, fieldName);
        if (!field.isAccessible()) {
            field.setAccessible(true);//w w w.  j  a v  a  2  s  .c  o m
        }
        try {
            field.set(object, value);
        } catch (IllegalAccessException e) {
            throw e;
        }
    }

    public static Field getDeclaredField(Object object, String fieldName) throws NoSuchFieldException {
        return getDeclaredField(object.getClass(), fieldName);
    }

    @SuppressWarnings("unchecked")
    public static Field getDeclaredField(Class clazz, String fieldName) throws NoSuchFieldException {
        return clazz.getDeclaredField(fieldName);
    }
}

Related

  1. setFieldValue(Object object, String fieldName, Object fieldValue)
  2. setFieldValue(Object object, String fieldName, Object fieldValue)
  3. setFieldValue(Object object, String fieldName, Object value)
  4. setFieldValue(Object object, String fieldName, Object value)
  5. setFieldValue(Object object, String fieldName, Object value)
  6. setFieldValue(Object object, String name, Object value)
  7. setFieldValue(Object object, String name, Object value)
  8. setFieldValue(Object objectInstance, String fieldName, Object valueToSet)
  9. setFieldValue(Object pojo, Field field, Object value)