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

Sets the new for field with given name.

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
/*//from  www  .java 2s. co  m
 * Copyright (c) 2012, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html
 * 
 * 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 org.eclipse.core.runtime.Assert;

public class Main {
    /**
     * Sets the new for field with given name.
     */
    public static void setField(Object target, String name, Object value) {
        Assert.isNotNull(target);
        Assert.isNotNull(name);
        Field field = getField(target, name);
        if (field == null) {
            Class<?> targetClass = getTargetClass(target);
            throw new IllegalArgumentException(name + " in " + targetClass);
        }
        try {
            field.set(target, value);
        } catch (Throwable e) {
            throw new RuntimeException(e);
            //&&& throw ExecutionUtils.propagate(e);
        }
    }

    /**
     * @return the declared {@link Field} with given name, may be private.
     */
    public static Field getField(Object target, String name) {
        Assert.isNotNull(target);
        Assert.isNotNull(name);
        Class<?> targetClass = getTargetClass(target);
        while (targetClass != null) {
            Field[] declaredFields = targetClass.getDeclaredFields();
            for (Field field : declaredFields) {
                if (field.getName().equals(name)) {
                    field.setAccessible(true);
                    return field;
                }
            }
            targetClass = targetClass.getSuperclass();
        }
        return null;
    }

    /**
     * @return the {@link Class} or the given "target" - "target" itself if it is {@link Class} or its
     *         {@link Class} if just some object.
     */
    private static Class<?> getTargetClass(Object target) {
        if (target instanceof Class) {
            return (Class<?>) target;
        }
        return target.getClass();
    }
}

Related

  1. setField(Object target, String fieldName, Class fieldType, T value)
  2. setField(Object target, String fieldname, Object value)
  3. setField(Object target, String name, Object value)
  4. setField(Object target, String name, Object value)
  5. setField(Object target, String name, Object value)
  6. setField(Object targetObject, String fieldName, Object value, boolean failIfError)
  7. setField(String field, Object object, Object value)
  8. setField(String field, Object value)
  9. setField(String fieldName, Object instance, Class instanceClass, Object value)