在使用反射執行一個方法時常遇到object is not an instance of declaring class的異常,如下代碼:
第34行會報object is not an instance of declaring class錯 對象不是聲明類的一個實例。解決辦法如下:
第一種:反射執行的方法 getPrimaryKey() 改成靜態的
第二種:在執行方法前先實例化類。m.invoke(mothed,null)改爲m.invoke(c.newInstance(),null)或者m.invoke(new PrimaryKeyUtils(),null)