Java Reflection Field Set setField(Field field, U instance, Object value)

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

Description

set Field

License

Open Source License

Declaration

public static <U> void setField(Field field, U instance, Object value) throws IllegalAccessException 

Method Source Code

//package com.java2s;
/*/*  ww w .j  ava2s . c  om*/
   Milyn - Copyright (C) 2006 - 2010
    
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License (version 2.1) as published by the Free Software
   Foundation.
    
   This library 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 Lesser General Public License for more details:
   http://www.gnu.org/licenses/lgpl.txt
*/

import java.lang.reflect.Field;

public class Main {
    public static <U> void setField(Field field, U instance, Object value) throws IllegalAccessException {
        boolean isAccessible = field.isAccessible();

        if (!isAccessible) {
            field.setAccessible(true);
        }

        try {
            field.set(instance, value);
        } finally {
            field.setAccessible(isAccessible);
        }
    }
}

Related

  1. setField(Field field, Object target, Object value)
  2. setField(Field field, Object target, Object value)
  3. setField(Field field, Object target, Object value)
  4. setField(Field field, Object target, Object value)
  5. setField(Field field, T newValue, Object instance)
  6. setField(final Field field, final Object instance, final Object value)
  7. setField(final Field field, final Object object, final Object value)
  8. setField(final O object, final String fieldName, final V value)
  9. setField(final Object instance, final String fieldName, final T value)