Java Reflection Field Set setField(Object object, Field field, Object value)

Here you can find the source of setField(Object object, Field field, Object value)

Description

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

License

Open Source License

Parameter

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

Exception

Parameter Description
Exception an exception

Declaration

private static void setField(Object object, Field field, Object value) throws Exception 

Method Source Code

//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);
        }
    }
}

Related

  1. setField(Object obj, String name, Object value)
  2. setField(Object obj, String name, Object value)
  3. setField(Object object, Class clazz, String fieldName, Object value)
  4. setField(Object object, Class clazz, String fieldName, Object value)
  5. setField(Object object, Field field, Object value)
  6. setField(Object object, Object value, String name)
  7. setField(Object object, String field, Object value)
  8. setField(Object object, String fieldName, int value)
  9. setField(Object object, String fieldName, Object newValue)