Java Reflection Field Value Set setFieldValue(Object target, Field field, Object newValue)

Here you can find the source of setFieldValue(Object target, Field field, Object newValue)

Description

set Field Value

License

Open Source License

Declaration

public static void setFieldValue(Object target, Field field, Object newValue) throws NoSuchFieldException 

Method Source Code


//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    public static void setFieldValue(Object target, Field field, Object newValue) throws NoSuchFieldException {
        boolean accessible = field.isAccessible();
        field.setAccessible(true);/*from  ww  w. j  a  v  a2  s  .co  m*/
        try {
            field.set(target, newValue);
        } catch (IllegalAccessException e) {
            throw new NoSuchFieldException("No such field: " + target.getClass() + '.' + field.getName());
        } finally {
            field.setAccessible(accessible);
        }
    }
}

Related

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