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

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

Description

Set the field represented by the supplied Field field object on the specified Object target object to the specified value .

License

Apache License

Parameter

Parameter Description
field the field to set
target the target object on which to set the field
value the value to set (may be null )

Declaration

public static void setField(Field field, Object target, Object value) 

Method Source Code

//package com.java2s;
/**/*from  w w  w .  j a v a2  s .  co m*/
 * Copyright 2008-2016 Juho Jeong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Field;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    /**
     * Set the field represented by the supplied {@link Field field object} on the
     * specified {@link Object target object} to the specified {@code value}.
     * In accordance with {@link Field#set(Object, Object)} semantics, the new value
     * is automatically unwrapped if the underlying field has a primitive type.
     * 
     * @param field the field to set
     * @param target the target object on which to set the field
     * @param value the value to set (may be {@code null})
     */
    public static void setField(Field field, Object target, Object value) {
        try {
            boolean accessibled = makeAccessible(field);
            field.set(target, value);
            if (accessibled)
                field.setAccessible(false);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Could not access field: " + e.getMessage());
        }
    }

    /**
     * Make the given field accessible, explicitly setting it accessible if
     * necessary. The {@code setAccessible(true)} method is only called when
     * actually necessary, to avoid unnecessary conflicts with a JVM
     * SecurityManager (if active).
     *
     * @param field the field to make accessible
     * @return true, if successful
     * @see java.lang.reflect.Field#setAccessible
     */
    public static boolean makeAccessible(Field field) {
        if ((!Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers())
                || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
            field.setAccessible(true);
            return true;
        }
        return false;
    }

    /**
     * Make the given method accessible, explicitly setting it accessible if
     * necessary. The {@code setAccessible(true)} method is only called when
     * actually necessary, to avoid unnecessary conflicts with a JVM
     * SecurityManager (if active).
     *
     * @param method the method to make accessible
     * @return true, if successful
     * @see java.lang.reflect.Method#setAccessible
     */
    public static boolean makeAccessible(Method method) {
        if ((!Modifier.isPublic(method.getModifiers())
                || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
            method.setAccessible(true);
            return true;
        }
        return false;
    }
}

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, Object target, Object value)
  6. setField(Field field, Object target, Object value)
  7. setField(Field field, T newValue, Object instance)
  8. setField(Field field, U instance, Object value)
  9. setField(final Field field, final Object instance, final Object value)