Java Reflection Field Value Set setFieldValue(Object obj, String fieldName, Object value)

Here you can find the source of setFieldValue(Object obj, String fieldName, Object value)

Description

Sets the field value for the given object instance.

License

Apache License

Parameter

Parameter Description
obj the object instance
fieldName the field name
value the value to set

Exception

Parameter Description
IllegalAccessException if any error occurs when setting the field value
NoSuchFieldException if the field of the given name could not be found

Declaration

public static void setFieldValue(Object obj, String fieldName, Object value)
        throws IllegalAccessException, NoSuchFieldException 

Method Source Code


//package com.java2s;
/*//from   w w w  . ja v  a 2  s.c o  m
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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;

public class Main {
    /**
     * <p>Sets the field value for the given object instance.</p>
     *
     * @param obj       the object instance
     * @param fieldName the field name
     * @param value     the value to set
     *
     * @throws IllegalAccessException if any error occurs when setting the field value
     * @throws NoSuchFieldException   if the field of the given name could not be found
     */
    public static void setFieldValue(Object obj, String fieldName, Object value)
            throws IllegalAccessException, NoSuchFieldException {

        Field field = getField(obj, fieldName);
        field.setAccessible(true);

        field.set(obj, value);
    }

    /**
     * <p>Retrieves the field from class by it's name.</p>
     *
     * @param obj       the object instance
     * @param fieldName the name of the field
     *
     * @return the class field
     *
     * @throws NoSuchFieldException if the field of the given name could not be found
     */
    private static Field getField(Object obj, String fieldName) throws NoSuchFieldException {

        Class<?> tmpClass = obj.getClass();
        do {
            for (Field field : tmpClass.getDeclaredFields()) {
                if (field.getName().equals(fieldName)) {

                    return field;
                }
            }
            tmpClass = tmpClass.getSuperclass();
        } while (tmpClass != null);

        throw new NoSuchFieldException("The field with name " + fieldName + " hasn't been found.");
    }
}

Related

  1. setFieldValue(Object obj, Field field, Object value)
  2. setFieldValue(Object obj, Field field, Object value)
  3. setFieldValue(Object obj, String field, Object value)
  4. setFieldValue(Object obj, String fieldName, Object value)
  5. setFieldValue(Object obj, String fieldname, Object value)
  6. setFieldValue(Object obj, String fieldName, Object value)
  7. setFieldValue(Object obj, String fieldName, Object value)
  8. setFieldValue(Object obj, String fieldName, String fieldValue)
  9. setFieldValue(Object obj, String name, Object value)