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