EgovObjectUtil { public static Object instantiate(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception { Class<?> clazz; try { clazz = loadClass(className); return clazz.newInstance(); } catch (ClassNotFoundException e) { LOGGER.error("{} : Class is can not instantialized.", className); throw new ClassNotFoundException(); } catch (InstantiationException e) { LOGGER.error("{} : Class is can not instantialized.", className); throw new InstantiationException(); } catch (IllegalAccessException e) { LOGGER.error("{} : Class is not accessed.", className); throw new IllegalAccessException(); } catch (Exception e) { LOGGER.error("{} : Class is not accessed.", className); throw new Exception(e); } } private  EgovObjectUtil(); static Class<?> loadClass(String className); static Object instantiate(String className); static Object instantiate(String className, String[] types, Object[] values); static boolean isNull(Object object);  }
@Test @SuppressWarnings("unchecked") public void testInstantiate() throws Exception { String className = "java.lang.String"; String wrongClassName = "java.lang.String1"; Object object = EgovObjectUtil.instantiate(className); assertNotNull(object); String string = (String) object; string = "eGovFramework"; assertEquals("Framework", string.substring(4)); object = null; assertNull(object); Class<Exception> exceptionClass = null; try { object = EgovObjectUtil.instantiate(wrongClassName); } catch (Exception e) { LOGGER.error("### Exception : {}", e.toString()); exceptionClass = (Class<Exception>) e.getClass(); } finally { assertEquals(ClassNotFoundException.class, exceptionClass); assertNotSame(InstantiationException.class, exceptionClass); assertNotSame(IllegalAccessException.class, exceptionClass); } }
@Test public void testInstantiateParamConstructor() throws Exception { String className = "java.lang.StringBuffer"; String[] types = new String[]{"java.lang.String"}; Object[] values = new Object[]{"전자정부 공통서비스"}; StringBuffer sb = (StringBuffer)EgovObjectUtil.instantiate(className, types, values); sb.append(" 및 개발프레임워크 구축 사업"); assertEquals("전자정부 공통서비스 및 개발프레임워크 구축 사업", sb.toString()); LOGGER.debug(sb.toString()); }