Java Reflection Field Value Set setFieldValue(final Object object, final String field, final Object value)

Here you can find the source of setFieldValue(final Object object, final String field, final Object value)

Description

Applies specified value to object field.

License

Open Source License

Parameter

Parameter Description
object object instance
field object field
value field value

Exception

Parameter Description
IllegalAccessException an exception
NoSuchFieldException an exception

Declaration

public static void setFieldValue(final Object object, final String field, final Object value)
        throws IllegalAccessException, NoSuchFieldException 

Method Source Code

//package com.java2s;
/*/*from   w w w  .j av  a2  s .  c  o  m*/
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.*;

public class Main {
    /**
     * Applies specified value to object field.
     * This method allows to access and modify even private object fields.
     *
     * @param object object instance
     * @param field  object field
     * @param value  field value
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public static void setFieldValue(final Object object, final String field, final Object value)
            throws IllegalAccessException, NoSuchFieldException {
        final Field actualField = getField(object.getClass(), field);
        actualField.setAccessible(true);
        actualField.set(object, value);
    }

    /**
     * Returns specified class field.
     * This method will also look for the field in super-classes if any exist.
     *
     * @param classType type of the class where field can be located
     * @param fieldName field name
     * @return specified class field
     * @throws NoSuchFieldException
     */
    public static Field getField(final Class classType, final String fieldName) throws NoSuchFieldException {
        final Field field = getFieldImpl(classType, fieldName);
        if (field != null) {
            return field;
        } else {
            throw new NoSuchFieldException(
                    "Field \"" + fieldName + "\" not found in class: " + classType.getCanonicalName());
        }
    }

    /**
     * Returns class for the specified canonical name.
     *
     * @param canonicalName class canonical name
     * @return class for the specified canonical name
     * @throws ClassNotFoundException
     */
    public static <T> Class<T> getClass(final String canonicalName) throws ClassNotFoundException {
        return (Class<T>) Class.forName(canonicalName);
    }

    /**
     * Returns specified class field.
     * This method will also look for the field in super-classes if any exist.
     *
     * @param classType type of the class where field can be located
     * @param fieldName field name
     * @return specified class field
     * @throws NoSuchFieldException
     */
    public static Field getFieldImpl(final Class classType, final String fieldName) throws NoSuchFieldException {
        Field field;
        try {
            field = classType.getDeclaredField(fieldName);
        } catch (final NoSuchFieldException e) {
            final Class superclass = classType.getSuperclass();
            field = superclass != null ? getFieldImpl(superclass, fieldName) : null;
        }
        return field;
    }
}

Related

  1. setFieldValue(final Field field, final Object instance, final Object value)
  2. setFieldValue(final Field field, final Object obj, final Object value)
  3. setFieldValue(final Field field, final Object value, final Object object)
  4. setFieldValue(final Object bean, final Field field, final Object value)
  5. setFieldValue(final Object obj, final String fieldName, final Object value)
  6. setFieldValue(final Object object, final String fieldName, final Object value)
  7. setFieldValue(final Object object, final String fieldName, final Object value)
  8. setFieldValue(final Object object, final String fieldName, final Object value)
  9. setFieldValue(Map map, Class cls)