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

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

Description

set Field Value

License

Open Source License

Declaration

static void setFieldValue(Object object, String name, Object value) 

Method Source Code


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

import java.lang.reflect.Field;

public class Main {
    static void setFieldValue(Object object, String name, Object value) {
        Field field = null;//from   w ww. j  av  a 2s  . co  m
        boolean isAccessible = true;
        try {
            field = object.getClass().getDeclaredField(name); //getField() for public fields
            isAccessible = field.isAccessible();
            field.setAccessible(true);
            field.set(object, value);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            if (field != null && !isAccessible) {
                field.setAccessible(false);
            }
        }
    }
}

Related

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