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

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

Description

Try to directly set a (possibly private) field on an Object.

License

Apache License

Parameter

Parameter Description
target Target to set the field on.
fieldname Name of field.
value Value to set on target.

Declaration

public static void setField(Object target, String fieldname, Object value)
        throws NoSuchFieldException, IllegalAccessException 

Method Source Code


//package com.java2s;
/*/*from  w w  w  .  j  av a  2 s  . com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.*;

public class Main {
    /**
     * Try to directly set a (possibly private) field on an Object.
     *
     * @param target    Target to set the field on.
     * @param fieldname Name of field.
     * @param value     Value to set on target.
     */
    public static void setField(Object target, String fieldname, Object value)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = findDeclaredField(target.getClass(), fieldname);
        field.setAccessible(true);
        field.set(target, value);
    }

    /**
     * Find a declared field in a class or one of its super classes
     *
     * @param inClass   Class to search for declared field.
     * @param fieldname Field name to search for
     * @return Field or will throw.
     * @throws NoSuchFieldException When field not found.
     */
    private static Field findDeclaredField(Class<?> inClass, String fieldname) throws NoSuchFieldException {
        while (!Object.class.equals(inClass)) {
            for (Field field : inClass.getDeclaredFields()) {
                if (field.getName().equalsIgnoreCase(fieldname)) {
                    return field;
                }
            }
            inClass = inClass.getSuperclass();
        }
        throw new NoSuchFieldException();
    }

    /**
     * Get the underlying class for a type, or null if the type is
     * a variable type.
     *
     * @param type the type
     * @return the underlying class
     */
    public static Class<?> getClass(Type type) {
        if (type instanceof Class) {
            return (Class<?>) type;
        } else if (type instanceof ParameterizedType) {
            return getClass(((ParameterizedType) type).getRawType());
        } else if (type instanceof GenericArrayType) {
            Type componentType = ((GenericArrayType) type).getGenericComponentType();
            Class<?> componentClass = getClass(componentType);
            if (componentClass != null) {
                return Array.newInstance(componentClass, 0).getClass();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}

Related

  1. setField(Object source, String fieldName, Object value)
  2. setField(Object sourceObj, Object targetObj, Field valueField, List targetFields)
  3. setField(Object target, Field field, Object value)
  4. setField(Object target, Field field, Object value)
  5. setField(Object target, String fieldName, Class fieldType, T 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 target, String name, Object value)