set Java Bean Property Value - Java Reflection

Java examples for Reflection:Java Bean

Description

set Java Bean Property Value

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {

    public static void setValue(String field, Object bean, Object value) {
        try {/*from ww  w  . j  ava2  s .  co m*/
            Field f = bean.getClass().getDeclaredField(field);
            boolean b = f.isAccessible();
            f.setAccessible(true);
            f.set(bean, value);
            f.setAccessible(b);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials