예제
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) {
try {
Class clazz = this .getClass();
Method toString = clazz.getDeclaredMethod("toString", new Class[0]);
Object result = toString.invoke(this , new Object[0]);
arg0.setAttribute(clazz.getName(), result);
}
catch (NoSuchMethodException exc) {
LogUtility.log(exc);
}
catch (InvocationTargetException exc) {
LogUtility.log(exc);
}
catch (IllegalAccessException exc) {
LogUtility.log(exc);
}
}
솔루션
리플렉션 대신 인터페이스를 사용하도록 코드를 재구성하십시오.
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) {
arg0.setAttribute(this .getClass().getName(), toString());
}