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