Java Reflection Field Set setField(Object object, String fieldName, Object value)

Here you can find the source of setField(Object object, String fieldName, Object value)

Description

set Field

License

Open Source License

Declaration

static void setField(Object object, String fieldName, Object value) 

Method Source Code

//package com.java2s;
/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2005-2009 RSSOwl Development Team                                  **
 **   http://www.rssowl.org/                                                 **
 **                                                                          **
 **   All rights reserved                                                    **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the Eclipse Public License v1.0 which accompanies this    **
 **   distribution, and is available at:                                     **
 **   http://www.rssowl.org/legal/epl-v10.html                               **
 **                                                                          **
 **   A copy is found in the file epl-v10.html and important notices to the  **
 **   license from the team is found in the textfile LICENSE.txt distributed **
 **   in this package.                                                       **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Contributors:                                                          **
 **     RSSOwl Development Team - initial API and implementation             **
 **                                                                          **
 **  **********************************************************************  */

import java.lang.reflect.Field;

public class Main {
    static void setField(Object object, String fieldName, Object value) {
        try {//from ww w  .  j  av a  2  s  . c  o m
            Field field = getField(object, fieldName);
            field.set(object, value);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }
    }

    static Field getField(Object object, String fieldName) {
        Class<?> klass = object.getClass();
        while (klass != Object.class) {
            for (Field field : klass.getDeclaredFields()) {
                if (field.getName().equals(fieldName)) {
                    field.setAccessible(true);
                    return field;
                }
            }
            klass = klass.getSuperclass();
        }
        throw new IllegalArgumentException(
                "No field with name: " + fieldName); //$NON-NLS-1$
    }
}

Related

  1. setField(Object object, Object value, String name)
  2. setField(Object object, String field, Object value)
  3. setField(Object object, String fieldName, int value)
  4. setField(Object object, String fieldName, Object newValue)
  5. setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind)
  6. setField(Object object, String fieldName, Object value)
  7. setField(Object object, String fieldName, Object value)
  8. setField(Object object, String fieldName, Object value)
  9. setField(Object object, String name, boolean value)