Dev/Java

Reflection Test

냐옴 2012. 11. 6. 19:15

 

private void reflectionTest() {

    try {
        // get class
        Class clazz = Class.forName("vo.UserVo");

        // create instance
        UserVo vo = (UserVo) clazz.newInstance();

        // get field
        Field field = clazz.getDeclaredField("privateField");

        // set field value
        field.setAccessible(true);
        field.set(vo, "aaaaaa");

        // get field value
        String value = (String) field.get(vo);

        System.out.println(value);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    
}