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

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

Description

Used to set value of specified field irrespective of the field modifier

License

Open Source License

Parameter

Parameter Description
bean Bean from which field value needs to be set
field field from which value needs to be set
value Value to be set

Declaration

public static void setFieldValue(Object bean, String field, Object value) 

Method Source Code

//package com.java2s;
/*/*from w w  w. j  ava 2 s  . c  o  m*/
 * The MIT License (MIT)
 * Copyright (c) 2015 "Yukthi Techsoft Pvt. Ltd." (http://yukthi-tech.co.in)
    
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.lang.reflect.Field;

public class Main {
    /**
     * Used to set value of specified field irrespective of the field modifier
     * @param bean Bean from which field value needs to be set
     * @param field field from which value needs to be set
     * @param value Value to be set
     */
    public static void setFieldValue(Object bean, String field, Object value) {
        try {
            Field fieldObj = bean.getClass().getDeclaredField(field);
            fieldObj.setAccessible(true);
            fieldObj.set(bean, value);
        } catch (Exception ex) {
            throw new IllegalStateException("An error occurred while seting field value - " + field, ex);
        }
    }
}

Related

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