develog

Reflection Test 본문

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();
    }
    
}

'Dev > Java' 카테고리의 다른 글

Annotation 종류  (0) 2012.11.06
Reflection  (0) 2012.11.06
[Tomcat] jsp cache clear  (0) 2012.10.08
timestamp 출력  (0) 2012.09.21
callback test  (0) 2012.08.06
Comments