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

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

Description

Set the field, even if it's private, to a boolean value.

License

Open Source License

Parameter

Parameter Description
object the object
name the field name
value the field value

Exception

Parameter Description
Exception if anything bad happens

Declaration

public static void setField(Object object, String name, boolean value) throws Exception 

Method Source Code


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

public class Main {
    /**//from   w  w  w .j a  v a2  s.co  m
     * Set the field, even if it's private, to a boolean value.
     * 
     * @param object
     *            the object
     * @param name
     *            the field name
     * @param value
     *            the field value
     * @throws Exception
     *             if anything bad happens
     */
    public static void setField(Object object, String name, boolean value) throws Exception {
        Field field = findDeclaredField(object.getClass(), name);
        field.setAccessible(true);
        field.setBoolean(object, value);
    }

    /**
     * Set the field.
     * 
     * @param object
     *            the object
     * @param name
     *            the field name
     * @param value
     *            the field value
     * @throws Exception
     *             if anything bad happens
     */
    public static void setField(Object object, String name, Object value) throws Exception {
        Field field = findDeclaredField(object.getClass(), name);
        field.setAccessible(true);
        field.set(object, value);
    }

    /**
     * Find a declared field in a class, including super classes.
     * 
     * @param targetClass
     *            the class to search through
     * @param name
     *            the name of the field
     * @return the found Field
     * @throws NoSuchFieldException
     *             if the Field can't be found
     */
    public static Field findDeclaredField(Class targetClass, String name) throws NoSuchFieldException {

        // Keep backing up the inheritance hierarchy.
        do {
            try {
                Field field = targetClass.getDeclaredField(name);
                return field;
            } catch (NoSuchFieldException e) {

            }
            targetClass = targetClass.getSuperclass();
        } while (targetClass != null);
        throw new NoSuchFieldException(name);
    }
}

Related

  1. setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind)
  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 fieldName, Object value)
  6. setField(Object object, String name, Object value)
  7. setField(Object object, String name, Object value)
  8. setField(Object owner, String name, Object newValue, Class definedIn)
  9. setField(Object owner, String targetClass, String fieldName, Object value)