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

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

Description

set Field

License

Apache License

Declaration

public static void setField(Object object, String name, Object value)
            throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException 

Method Source Code


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

import java.lang.reflect.Field;

public class Main {
    public static void setField(Object object, String name, Object value)
            throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Class<?> clazz = object.getClass();
        Field field = clazz.getDeclaredField(name);
        boolean accessable = field.isAccessible();
        if (!accessable)
            field.setAccessible(true);//from  w  w w. j a  v a 2s.  c o m
        field.set(object, value);
        if (!accessable)
            field.setAccessible(false);
    }
}

Related

  1. setField(Object object, String fieldName, Object value)
  2. setField(Object object, String fieldName, Object value)
  3. setField(Object object, String fieldName, Object value)
  4. setField(Object object, String fieldName, Object value)
  5. setField(Object object, String name, boolean value)
  6. setField(Object object, String name, Object value)
  7. setField(Object owner, String name, Object newValue, Class definedIn)
  8. setField(Object owner, String targetClass, String fieldName, Object value)
  9. setField(Object service, String name, Object obj)