Here you can find the source of setField(Object object, String name, boolean value)
Parameter | Description |
---|---|
object | the object |
name | the field name |
value | the field value |
Parameter | Description |
---|---|
Exception | if anything bad happens |
public static void setField(Object object, String name, boolean value) throws Exception
//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); } }