最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

java動(dòng)態(tài)代理,為什么代理的方法總是會(huì)拋異常?

java動(dòng)態(tài)代理,為什么代理的方法總是會(huì)拋異常?

kaguya 2016-09-23 22:41:59
想問(wèn)一下各位大神,java動(dòng)態(tài)代理時(shí),如果被代理的方法有返回值,程序就會(huì)拋出一個(gè)類(lèi)型不匹配異常,該如何處理?所做題目如下:寫(xiě)一個(gè)ArrayList類(lèi)的代理,實(shí)現(xiàn)和ArrayList類(lèi)中完全相同的功能,并可以計(jì)算每個(gè)方法運(yùn)行的時(shí)間。但是一直拋ClassCastException異常,自己研究一段時(shí)間發(fā)現(xiàn)是ArrayList 的.add(E e)方法傳回了一個(gè)booleanimport?java.lang.reflect.InvocationHandler; import?java.lang.reflect.Method; import?java.lang.reflect.Proxy; import?java.util.ArrayList; import?java.util.List; public?class?Demo?{ public?static?void?main(String[]?args)?{ ArrayList<String>?al?=?new?ArrayList<>(); MyInvocationHandler?m?=?new?MyInvocationHandler(al); List<String>?l?=?(List)Proxy.newProxyInstance(al.getClass().getClassLoader(),?al.getClass().getInterfaces(),?m); l.add("a"); } } class?MyInvocationHandler?implements?InvocationHandler?{ private?Object?obj; //構(gòu)造方法接收一個(gè)對(duì)象類(lèi) public?MyInvocationHandler(Object?obj)?{ this.obj?=?obj; } //重寫(xiě)?invoke方法 public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)?throws?Throwable?{ long?start?=?System.currentTimeMillis(); method.invoke(obj,?args); //System.out.println("代理執(zhí)行了"); long?end?=?System.currentTimeMillis(); System.out.println("添加完成"); System.out.println("共耗時(shí):"+(end-start)+"毫秒"); //默認(rèn)return?null?會(huì)造成空指針異常 return?obj; } }自己試著自定義一個(gè)類(lèi)進(jìn)行代理,無(wú)返回值的方法可以正常代理,后來(lái)在add(String str)方法里增加了返回值,同樣拋出異常import?java.lang.reflect.InvocationHandler; import?java.lang.reflect.Method; import?java.lang.reflect.Proxy; import?java.util.ArrayList; import?java.util.List; public?class?Demo?{ public?static?void?main(String[]?args)?{ //下面這個(gè)拋出ClassCastException,確認(rèn)是被代理方法有返回值造成的 UserImp?ui?=?new?UserImp(); MyInvocationHandler?m?=?new?MyInvocationHandler(ui); User?u?=?(User)Proxy.newProxyInstance(ui.getClass().getClassLoader(),?ui.getClass().getInterfaces(),?m); u.add("aaa"); } } class?MyInvocationHandler?implements?InvocationHandler?{ private?Object?obj; //構(gòu)造方法接收一個(gè)對(duì)象類(lèi) public?MyInvocationHandler(Object?obj)?{ this.obj?=?obj; } //重寫(xiě)?invoke方法 public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)?throws?Throwable?{ long?start?=?System.currentTimeMillis(); method.invoke(obj,?args); //System.out.println("代理執(zhí)行了"); long?end?=?System.currentTimeMillis(); System.out.println("添加完成"); System.out.println("共耗時(shí):"+(end-start)+"毫秒"); return?obj; } } //User接口//TODO interface?User?{ String[]?arr?=?new?String[10]; public?String?add(String?str); public?void?del(); } //User的實(shí)現(xiàn)類(lèi) class?UserImp?implements?User?{ public?String?add(String?str)?{ System.out.println(str); arr[0]?=?str; return?"aa"; } public?void?del()?{ System.out.println("刪除"); } }
查看完整描述

2 回答

已采納
?
阿旭_

TA貢獻(xiàn)217條經(jīng)驗(yàn) 獲得超353個(gè)贊

善用instanceof

if?(args?instanceof?type)?{
	type?new_name?=?(type)?args;	
}


查看完整回答
2 反對(duì) 回復(fù) 2016-09-24
  • kaguya
    kaguya
    是在 method.invoke(obj,args); 那里強(qiáng)轉(zhuǎn)么?求細(xì)說(shuō),我剛接觸java一個(gè)月不太懂...
  • 阿旭_
    阿旭_
    OK,給你重新發(fā)個(gè)完整的代碼
  • 2 回答
  • 1 關(guān)注
  • 3059 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)