Java Reflection Field Set setField(Object object, Class clazz, String fieldName, Object value)

Here you can find the source of setField(Object object, Class clazz, String fieldName, Object value)

Description

Sets the value of a field in an object

License

Apache License

Parameter

Parameter Description
object The object that the field belongs to
clazz The class that the field is defined by
fieldName The name of the field being set
value The value that the field is being set to

Return

true if setting the field was successful, false if otherwise

Declaration

public static boolean setField(Object object, Class<?> clazz, String fieldName, Object value) 

Method Source Code


//package com.java2s;
/*/*from w  w  w.ja v  a 2 s  .  c  o  m*/
 * Copyright 2018 ImpactDevelopment
 *
 * 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.util.Arrays;

public class Main {
    /**
     * Sets the value of a field in an object
     *
     * @param object The object that the field belongs to
     * @param fieldName The name of the field being set
     * @param value The value that the field is being set to
     * @return true if setting the field was successful, false if otherwise
     */
    public static boolean setField(Object object, String fieldName, Object value) {
        return setField(object, object.getClass(), fieldName, value);
    }

    /**
     * Sets the value of a field in an object
     *
     * @param object The object that the field belongs to
     * @param clazz The class that the field is defined by
     * @param fieldName The name of the field being set
     * @param value The value that the field is being set to
     * @return true if setting the field was successful, false if otherwise
     */
    public static boolean setField(Object object, Class<?> clazz, String fieldName, Object value) {
        if (!clazz.isAssignableFrom(object.getClass()))
            return false;

        Field field = Arrays.stream(clazz.getDeclaredFields()).filter(f -> f.getName().equals(fieldName))
                .findFirst().orElse(null);

        return setField(object, field, value);
    }

    /**
     * Sets the value of a field in an object
     *
     * @param object The object that the field belongs to
     * @param field The field being set
     * @param value The value that the field is being set to
     * @return true if setting the field was successful, false if otherwise
     */
    public static boolean setField(Object object, Field field, Object value) {
        try {
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            field.set(object, value);
            field.setAccessible(accessible);
            return true;
        } catch (NullPointerException | IllegalAccessException ignored) {
        }
        return false;
    }
}

Related

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