生活随笔
收集整理的這篇文章主要介紹了
利用java反射调用类的的私有方法--转
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:http://blog.csdn.net/woshinia/article/details/11766567
1,今天和一位朋友談到父類(lèi)私有方法的調(diào)用問(wèn)題,本來(lái)以為利用反射很輕松就可以實(shí)現(xiàn),因?yàn)樵诜瓷淇磥?lái)根本不區(qū)分是否是private的,沒(méi)有想到調(diào)用本身的私有方法是可以的,但是調(diào)用父類(lèi)的私有方法則不行,后來(lái)糾其原因很有可能是因?yàn)間etDeclaredMethod方法和getMethod方法并不會(huì)查找父類(lèi)的私有方法,于是只好自己寫(xiě)遞歸了,經(jīng)過(guò)嘗試果然如此。把代碼放出來(lái)方便更多人。這段代碼可以解決很多實(shí)際問(wèn)題,不過(guò)利用反射來(lái)做的話性能不會(huì)太好。
[java]?view plaincopy
package?com.syj.util.reflect;????import?java.lang.reflect.Method;????public?class?PrivateUtil?{??????????public?static?Method?getMethod(Class?clazz,?String?methodName,??????????????final?Class[]?classes)?throws?Exception?{??????????Method?method?=?null;??????????try?{??????????????method?=?clazz.getDeclaredMethod(methodName,?classes);??????????}?catch?(NoSuchMethodException?e)?{??????????????try?{??????????????????method?=?clazz.getMethod(methodName,?classes);??????????????}?catch?(NoSuchMethodException?ex)?{??????????????????if?(clazz.getSuperclass()?==?null)?{??????????????????????return?method;??????????????????}?else?{??????????????????????method?=?getMethod(clazz.getSuperclass(),?methodName,??????????????????????????????classes);??????????????????}??????????????}??????????}??????????return?method;??????}????????????public?static?Object?invoke(final?Object?obj,?final?String?methodName,??????????????final?Class[]?classes,?final?Object[]?objects)?{??????????try?{??????????????Method?method?=?getMethod(obj.getClass(),?methodName,?classes);??????????????method.setAccessible(true);????????????return?method.invoke(obj,?objects);??????????}?catch?(Exception?e)?{??????????????throw?new?RuntimeException(e);??????????}??????}????????public?static?Object?invoke(final?Object?obj,?final?String?methodName,??????????????final?Class[]?classes)?{??????????return?invoke(obj,?methodName,?classes,?new?Object[]?{});??????}????????public?static?Object?invoke(final?Object?obj,?final?String?methodName)?{??????????return?invoke(obj,?methodName,?new?Class[]?{},?new?Object[]?{});??????}????????????public?static?void?main(String[]?args)?{??????????PrivateUtil.invoke(new?B(),?"printlnA",?new?Class[]?{?String.class?},??????????????????new?Object[]?{?"test"?});??????????PrivateUtil.invoke(new?B(),?"printlnB");??????}??}????class?A?{??????private?void?printlnA(String?s)?{??????????System.out.println(s);??????}??}????class?B?extends?A?{??????private?void?printlnB()?{??????????System.out.println("b");??????}??}?? ?
程序的輸出結(jié)果為
test
b
說(shuō)明private方法調(diào)用成功了不管是自己的私有方法還是父類(lèi)的私有方法。\
2,
?
[java]?view plaincopy
package?me.test;??import?java.lang.reflect.*;??public?class?ReflectTest?{???public?static?void?main(String[]?args)??throws??Exception???{??????????????Point?pt=new?Point(3,5);??????Field?fx=pt.getClass().getField("x")?;??????Field?fy=pt.getClass().getDeclaredField("y");????Method?m2=Point.class.getDeclaredMethod("showPrivate")?;??????????????m2.setAccessible(true)??;???????m2.invoke(pt)?;????????System.out.println(fx.getInt(pt));????fy.setAccessible(true)?;????System.out.println(fy.getInt(pt));??????????????????changeString(pt)?;?????System.out.println(pt);???????????????}???public?static?void?changeString(Object?obj)??throws?Exception?{?????Field[]?f=obj.getClass().getFields()??;???for(Field?tem?:?f)????{?????if(tem.getType()==String.class)?????{??????String?oldString=(String)tem.get(obj)?;??????String?newString=oldString.replace('d',?'x');????tem.setAccessible(true);???????tem.set(obj,?newString)?;???}?????????}???????}??}????public?class?Point??{???public?int?x??;???private?int?y??;???public?Point(int?x,?int?y)?{????super();????this.x?=?x;????this.y?=?y;???}?????public?String??a="dsfdsfd"??;?????public?String??b="fdsfdsfewewwwww"??;?????public?String??c="adddssss"??;?????private?void?showPrivate()???{????System.out.println("x="+this.x+"\n"+"y="+this.y);????System.out.println(this.a);????System.out.println(this.b);????System.out.println(this.c);????}?????public?String?toString()???{????return?this.a+"\n"+this.b+"\n"+this.c;???}????}??
3,??
Java利用反射來(lái)獲取一個(gè)方法的 范型化參數(shù) Vector<Integer>的類(lèi)型
[java]?view plaincopy
class???A????{?????????public??void???show(Vector<Integer>?v)??{}??}??????在我們不知道Vector中數(shù)據(jù)的類(lèi)型的時(shí)候?這時(shí)候我們只知道這個(gè)方法的名字??和參數(shù)的個(gè)數(shù)?,我們來(lái)獲取?范型化的實(shí)際類(lèi)型?。?????我們不可能通過(guò)?Vector對(duì)應(yīng)的Class類(lèi)來(lái)反射出??泛型集合中的類(lèi)型??,但是?我們卻可以通過(guò)?這個(gè)方法所對(duì)應(yīng)的Method類(lèi)來(lái)實(shí)現(xiàn)?。????具體如下??:????import?java.lang.reflect.Method;??import?java.lang.reflect.ParameterizedType;??import?java.lang.reflect.Type;??import?java.util.Vector;????public?class?Test3??{???public?static?void?main(String?[]args)?throws?SecurityException,?NoSuchMethodException???{????Method??m=A.class.getMethod("show",?Vector.class)?;????????Type[]t=m.getGenericParameterTypes()?;????????ParameterizedType?p=(ParameterizedType)t[0];???????System.out.println(p.getActualTypeArguments()[0]);????}????}??
轉(zhuǎn)載于:https://www.cnblogs.com/mochaMM/p/5016431.html
總結(jié)
以上是生活随笔為你收集整理的利用java反射调用类的的私有方法--转的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。