Java Reflection Field Value Set setFieldValue(Field field, Object value, Object target)

Here you can find the source of setFieldValue(Field field, Object value, Object target)

Description

set Field Value

License

Open Source License

Declaration

public static void setFieldValue(Field field, Object value, Object target)
            throws IllegalArgumentException, IllegalAccessException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import java.lang.reflect.Field;

public class Main {
    public static void setFieldValue(Field field, Object value, Object target)
            throws IllegalArgumentException, IllegalAccessException {

        boolean declaredAccessible = field.isAccessible();

        if (!declaredAccessible) {
            field.setAccessible(true);//from  ww w  .  j  av a 2s .c o  m
        }

        field.set(target, value);

        if (!declaredAccessible) {
            field.setAccessible(false);
        }
    }
}

Related

  1. setFieldValue(Field field, Object obj, Object value)
  2. setFieldValue(Field field, Object object, Object value)
  3. setFieldValue(Field field, Object object, Object value)
  4. setFieldValue(Field field, Object target, Object value)
  5. setFieldValue(Field field, Object value, Object instance)
  6. setFieldValue(final Class clazz, final String fieldName, final Object fieldValue)
  7. setFieldValue(final Field field, final Object instance, final Object value)
  8. setFieldValue(final Field field, final Object obj, final Object value)
  9. setFieldValue(final Field field, final Object value, final Object object)