Java Reflection Field Set setField(Object target, String name, Object value)

Here you can find the source of setField(Object target, String name, Object value)

Description

Set the java.lang.reflect.Field field with the given name on the provided Object target object to the supplied value.

License

Open Source License

Parameter

Parameter Description
target the target object on which to set the field
name the name of the field to set
value the value to set

Declaration

public static void setField(Object target, String name, Object value) 

Method Source Code

//package com.java2s;
/*//from   ww w . j  a v  a  2 s .c  o  m
 * Artifactory is a binaries repository manager.
 * Copyright (C) 2012 JFrog Ltd.
 *
 * Artifactory is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Artifactory 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.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Artifactory.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Field;

public class Main {
    /**
     * Set the {@link java.lang.reflect.Field field} with the given <code>name</code> on the
     * provided {@link Object target object} to the supplied <code>value</code>.
     * <p/>
     * Assumes the field is declared in the specified target class.
     *
     * @param target the target object on which to set the field
     * @param name   the name of the field to set
     * @param value  the value to set
     */
    public static void setField(Object target, String name, Object value) {
        try {
            Field field = findField(target.getClass(), name);
            if (field == null) {
                throw new IllegalArgumentException("Could not find field ["
                        + name + "] on target [" + target + "]");
            }
            field.setAccessible(true);
            field.set(target, value);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Attempt to find a {@link Field field} on the supplied {@link Class} with
     * the supplied <code>name</code>. Searches all
     * superclasses up to {@link Object}.
     *
     * @param clazz the class to introspect
     * @param name  the name of the field (may be <code>null</code> if type is specified)
     * @return the corresponding Field object, or <code>null</code> if not found
     */
    public static Field findField(Class<?> clazz, String name) {
        Class<?> searchType = clazz;
        while (searchType != null) {
            Field[] fields = searchType.getDeclaredFields();
            for (Field field : fields) {
                if ((name == null || name.equals(field.getName()))) {
                    return field;
                }
            }
            searchType = searchType.getSuperclass();
        }
        return null;
    }
}

Related

  1. setField(Object sourceObj, Object targetObj, Field valueField, List targetFields)
  2. setField(Object target, Field field, Object value)
  3. setField(Object target, Field field, Object value)
  4. setField(Object target, String fieldName, Class fieldType, T value)
  5. setField(Object target, String fieldname, Object value)
  6. setField(Object target, String name, Object value)
  7. setField(Object target, String name, Object value)
  8. setField(Object target, String name, Object value)
  9. setField(Object targetObject, String fieldName, Object value, boolean failIfError)