Here you can find the source of setField(Object object, Field field, Object value)
Parameter | Description |
---|---|
object | the instance of the class to which the field belongs |
field | the field to set the value of |
value | the value to set |
Parameter | Description |
---|---|
Exception | an exception |
private static void setField(Object object, Field field, Object value) throws Exception
//package com.java2s; /*/*w ww. ja va 2s . c om*/ -------------------------------------------------------------------------------- SPADE - Support for Provenance Auditing in Distributed Environments. Copyright (C) 2015 SRI International This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. -------------------------------------------------------------------------------- */ import java.lang.reflect.Field; public class Main { /** * Sets the given value for the field inside the given object * * Throws all exceptions that can be thrown by Field.set * * Changes the accessibility of the field if it is false * * @param object the instance of the class to which the field belongs * @param field the field to set the value of * @param value the value to set * @throws Exception */ private static void setField(Object object, Field field, Object value) throws Exception { boolean changedAccessible = false; boolean isAccessible = field.isAccessible(); if (!isAccessible) { changedAccessible = true; field.setAccessible(true); } field.set(object, value); if (changedAccessible) { field.setAccessible(false); } } }