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

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

Description

Set the value of the field 'name' to be 'value' on object 'object'.

License

Open Source License

Parameter

Parameter Description
object The object to modify
value The value for the field
name The name of the field to set

Exception

Parameter Description
RuntimeException If the field doesn't exist or there is atype inconsistency.

Declaration

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

Method Source Code


//package com.java2s;
/*//  ww  w .  j a va 2 s . c  o m
 ************************************************************************
 *******************  CANADIAN ASTRONOMY DATA CENTRE  *******************
 **************  CENTRE CANADIEN DE DONN?ES ASTRONOMIQUES  **************
 *
 *  (c) 2014.                            (c) 2014.
 *  Government of Canada                 Gouvernement du Canada
 *  National Research Council            Conseil national de recherches
 *  Ottawa, Canada, K1A 0R6              Ottawa, Canada, K1A 0R6
 *  All rights reserved                  Tous droits r?serv?s
 *
 *  NRC disclaims any warranties,        Le CNRC d?nie toute garantie
 *  expressed, implied, or               ?nonc?e, implicite ou l?gale,
 *  statutory, of any kind with          de quelque nature que ce
 *  respect to the software,             soit, concernant le logiciel,
 *  including without limitation         y compris sans restriction
 *  any warranty of merchantability      toute garantie de valeur
 *  or fitness for a particular          marchande ou de pertinence
 *  purpose. NRC shall not be            pour un usage particulier.
 *  liable in any event for any          Le CNRC ne pourra en aucun cas
 *  damages, whether direct or           ?tre tenu responsable de tout
 *  indirect, special or general,        dommage, direct ou indirect,
 *  consequential or incidental,         particulier ou g?n?ral,
 *  arising from the use of the          accessoire ou fortuit, r?sultant
 *  software.  Neither the name          de l'utilisation du logiciel. Ni
 *  of the National Research             le nom du Conseil National de
 *  Council of Canada nor the            Recherches du Canada ni les noms
 *  names of its contributors may        de ses  participants ne peuvent
 *  be used to endorse or promote        ?tre utilis?s pour approuver ou
 *  products derived from this           promouvoir les produits d?riv?s
 *  software without specific prior      de ce logiciel sans autorisation
 *  written permission.                  pr?alable et particuli?re
 *                                       par ?crit.
 *
 *  This file is part of the             Ce fichier fait partie du projet
 *  OpenCADC project.                    OpenCADC.
 *
 *  OpenCADC is free software:           OpenCADC est un logiciel libre ;
 *  you can redistribute it and/or       vous pouvez le redistribuer ou le
 *  modify it under the terms of         modifier suivant les termes de
 *  the GNU Affero General Public        la ?GNU Affero General Public
 *  License as published by the          License?? telle que publi?e
 *  Free Software Foundation,            par la Free Software Foundation
 *  either version 3 of the              : soit la version 3 de cette
 *  License, or (at your option)         licence, soit (? votre gr?)
 *  any later version.                   toute version ult?rieure.
 *
 *  OpenCADC is distributed in the       OpenCADC est distribu?
 *  hope that it will be useful,         dans l?espoir qu?il vous
 *  but WITHOUT ANY WARRANTY;            sera utile, mais SANS AUCUNE
 *  without even the implied             GARANTIE : sans m?me la garantie
 *  warranty of MERCHANTABILITY          implicite de COMMERCIALISABILIT?
 *  or FITNESS FOR A PARTICULAR          ni d?AD?QUATION ? UN OBJECTIF
 *  PURPOSE.  See the GNU Affero         PARTICULIER. Consultez la Licence
 *  General Public License for           G?n?rale Publique GNU Affero
 *  more details.                        pour plus de d?tails.
 *
 *  You should have received             Vous devriez avoir re?u une
 *  a copy of the GNU Affero             copie de la Licence G?n?rale
 *  General Public License along         Publique GNU Affero avec
 *  with OpenCADC.  If not, see          OpenCADC ; si ce n?est
 *  <http://www.gnu.org/licenses/>.      pas le cas, consultez :
 *                                       <http://www.gnu.org/licenses/>.
 *
 *  $Revision: 4 $
 *
 ************************************************************************
 */

import java.lang.reflect.Field;

public class Main {
    /**
     * Set the value of the field 'name' to be 'value' on object 'object'.
     *
     * This method will set the field in any superclasses that the object
     * may inherit too.
     *
     * @param object The object to modify
     * @param value The value for the field
     * @param name The name of the field to set
     * @throws RuntimeException If the field doesn't exist or there is a
     *     type inconsistency.
     */
    public static void setField(Object object, Object value, String name) {
        ReflectiveOperationException firstError = null;
        Class<? extends Object> clazz = object.getClass();
        while (clazz != null) {
            try {
                Field field = clazz.getDeclaredField(name);
                field.setAccessible(true);
                field.set(object, value);
                clazz = null;
                firstError = null;
            } catch (ReflectiveOperationException e) {
                // try the superclass
                firstError = e;
                clazz = clazz.getSuperclass();
            }
        }
        if (firstError != null) {
            throw new RuntimeException("failed to set field value", firstError);
        }
    }
}

Related

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