Skip to content

Instantly share code, notes, and snippets.

@debop
Created January 19, 2013 09:48
Show Gist options
  • Save debop/4571649 to your computer and use it in GitHub Desktop.
Save debop/4571649 to your computer and use it in GitHub Desktop.
Java Refelection으로 객채 생성
/**
* 지정된 수형의 새로운 인스턴스를 생성합니다.
*
* @param clazz 생성할 수형
* @param <T> 수형
* @return 지정한 수형의 새로운 인스턴스, 생성 실패시에는 null을 반환합니다.
*/
public static <T> T createInstance(Class<T> clazz) {
Guard.shouldNotBeNull(clazz, "clazz");
if (log.isDebugEnabled())
log.debug("수형 [{}] 의 새로운 인스턴스를 생성합니다...", clazz.getName());
try {
return (T) clazz.newInstance();
} catch (Exception e) {
if (log.isWarnEnabled())
log.warn(clazz.getName() + " 수형을 생성하는데 실패했습니다.", e);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment