通過(guò)反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解
簡(jiǎn)述
一直對(duì)Java沒(méi)有現(xiàn)成的委托機(jī)制耿耿于懷,所幸最近有點(diǎn)時(shí)間,用反射寫了一個(gè)簡(jiǎn)單的委托模塊,以供參考。
模塊API
public Class Delegater()//空參構(gòu)造,該類管理委托實(shí)例并實(shí)現(xiàn)委托方法 //添加一個(gè)靜態(tài)方法委托,返回整型值ID代表該方法與參數(shù)構(gòu)成的實(shí)例。若失敗,則返回-1。 public synchronized int addFunctionDelegate(Class<?> srcClass,String methodName,Object... params); //添加一個(gè)實(shí)例方法委托,返回整型值ID代表該方法與參數(shù)構(gòu)成的實(shí)例。若失敗,則返回-1。 public synchronized int addFunctionDelegate(Object srcObj,String methodName,Object... params); //根據(jù)整型ID從委托實(shí)例中刪除一個(gè)方法委托,返回是否成功 public synchronized Boolean removeMethod(int registerID); //依次執(zhí)行該委托實(shí)例中的所有方法委托(無(wú)序) public synchronized void invokeAllMethod(); //將參數(shù)表轉(zhuǎn)換為參數(shù)類型表 private Class<?>[] getParamTypes(Object[] params); //由指定的Class、方法名、參數(shù)類型表獲得方法實(shí)例 private Method getDstMethod(Class<?> srcClass,String methodName,Class<?>[] paramTypes); class DelegateNode(Method refMethod,Object[] params)//DelegateNode類在不使用Object構(gòu)造時(shí)敘述了一個(gè)靜態(tài)方法委托,包括方法實(shí)例及參數(shù)表 class DelegateNode(Object srcObj,Method refMethod,Object[] params)//DelegateNode類在使用Object構(gòu)造時(shí)敘述了一個(gè)實(shí)例方法委托,包括類實(shí)例、方法實(shí)例及參數(shù)表 public void invokeMethod(); //執(zhí)行該節(jié)點(diǎn)敘述的方法委托
源代碼
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Hashtable; /**Delegater類使用RTTI及反射實(shí)現(xiàn)Java下的委托機(jī)制 * @author 三向板磚 * */ public class Delegater { static int register = Integer.MIN_VALUE; //ID分配變量 Hashtable<Integer,DelegateNode> nodeTable; //管理ID與對(duì)應(yīng)委托的容器 public Delegater() { nodeTable = new Hashtable<Integer,DelegateNode>(); } //添加靜態(tài)方法委托 public synchronized int addFunctionDelegate(Class<?> srcClass,String methodName,Object... params) { Class<?>[] paramTypes = getParamTypes(params); Method refMethod; if((refMethod = getDstMethod(srcClass,methodName,paramTypes)) != null) { register++; nodeTable.put(register,new DelegateNode(refMethod, params)); return register; } else { return -1; } } //添加動(dòng)態(tài)方法委托 public synchronized int addFunctionDelegate(Object srcObj,String methodName,Object... params) { Class<?>[] paramTypes = getParamTypes(params); Method refMethod; if((refMethod = getDstMethod(srcObj.getClass(),methodName,paramTypes)) != null) { register++; nodeTable.put(register,new DelegateNode(srcObj,refMethod, params)); return register; } else { return -1; } } //刪除一個(gè)方法委托 public synchronized Boolean removeMethod(int registerID) { if(nodeTable.containsKey(registerID)) { nodeTable.remove(registerID); return true; } return false; } //無(wú)序地執(zhí)行委托方法 public synchronized void invokeAllMethod() { for (DelegateNode node:nodeTable.values()) { node.invokeMethod(); } } //將參數(shù)表轉(zhuǎn)化為參數(shù)類型表 private Class<?>[] getParamTypes(Object[] params) { Class<?>[] paramTypes = new Class<?>[params.length]; for (int i = 0;i < params.length;i++) { paramTypes[i] = params[i].getClass(); } return paramTypes; } //根據(jù)Class類實(shí)例、方法名、參數(shù)類型表獲得一個(gè)Method實(shí)例 private Method getDstMethod(Class<?> srcClass,String methodName,Class<?>[] paramTypes) { Method result = null; try { result = srcClass.getMethod(methodName, paramTypes); if(result.getReturnType() != void.class) { System.out.println("Warning,Method:"+methodName+" has a return value!"); } } catch (NoSuchMethodException | SecurityException e) { System.out.println("Can Not Found Method:"+methodName+",ensure it's exist and visible!"); } return result; } } class DelegateNode { Object srcObj; Method refMethod; Object[] params; public DelegateNode(Method refMethod,Object[] params) { this.refMethod = refMethod; this.params = params; } public DelegateNode(Object srcObj,Method refMethod,Object[] params) { this.srcObj = srcObj; this.refMethod = refMethod; this.params = params; } public void invokeMethod() { try { refMethod.invoke(srcObj,params); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { System.out.println("Method:"+refMethod.toString()+" invoke fail!"); } } }
模塊測(cè)試
public class DelegaterTest { public void showInfo() { System.out.println("Hello Delegate!"); } public void showCustomInfo(String info) { System.out.println(info); } public static void showStaticInfo() { System.out.println("Static Delegate!"); } public static void showCustomStaticInfo(String info) { System.out.println(info); } public static void main(String[] args) { Delegater dele = new Delegater(); DelegaterTest tester = new DelegaterTest(); int ID = dele.addFunctionDelegate(tester,"showInfo"); dele.addFunctionDelegate(tester,"showCustomInfo","Custom!"); dele.addFunctionDelegate(DelegaterTest.class,"showStaticInfo"); dele.addFunctionDelegate(DelegaterTest.class,"showCustomStaticInfo","StaticCustom!"); dele.invokeAllMethod(); dele.removeMethod(ID); System.out.println("------------------"); dele.invokeAllMethod(); } }
執(zhí)行結(jié)果:
StaticCustom!
StaticDelegate!
Custom!
HelloDelegate!
------------------
StaticCustom!
StaticDelegate!
Custom!
其他事項(xiàng)
一些public方法使用synchronized是為了保證register變量的線程安全,使其不會(huì)因?yàn)槎嗑€程而出錯(cuò)。
對(duì)于有返回值的委托,會(huì)報(bào)出警告,但模塊還是接受這樣的委托的,不過(guò)在執(zhí)行委托時(shí)您將不能得到返回值。
添加的委托最大值是Integer.MAX_VALUE-Integer.MIN_VALUE超出后的容錯(cuò)處理沒(méi)有考慮(一般也沒(méi)這么多函數(shù)需要委托的吧。
委托執(zhí)行是無(wú)序的,而且,需要性能要求時(shí),委托的函數(shù)盡量不要有阻塞過(guò)程,否則會(huì)影響其他委托函數(shù)的執(zhí)行。
還有什么問(wèn)題可以發(fā)上來(lái)一同探討。
總結(jié)
以上就是本文關(guān)于通過(guò)反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他Java相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
詳解Java執(zhí)行g(shù)roovy腳本的兩種方式
這篇文章主要介紹了Java執(zhí)行g(shù)roovy腳本的兩種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Springboot 實(shí)現(xiàn)跨域訪問(wèn)無(wú)需使用jsonp的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot 實(shí)現(xiàn)跨域訪問(wèn) 無(wú)需使用jsonp的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09Spring事務(wù)處理Transactional,鎖同步和并發(fā)線程
本文詳細(xì)講解了Spring事務(wù)處理Transactional,鎖同步和并發(fā)線程。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12java Person,Student,GoodStudent 三個(gè)類的繼承、構(gòu)造函數(shù)的執(zhí)行
這篇文章主要介紹了java Person,Student,GoodStudent 三個(gè)類的繼承、構(gòu)造函數(shù)的執(zhí)行,需要的朋友可以參考下2017-02-02Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢
本文主要介紹了Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08關(guān)于Mybatis-Plus?Wrapper是否應(yīng)該出現(xiàn)在Servcie類中
最近在做代碼重構(gòu),代碼工程采用了Controller/Service/Dao分層架構(gòu),Dao層使用了Mybatis-Plus框架,本文帶領(lǐng)大家學(xué)習(xí)Mybatis-Plus?Wrapper應(yīng)該出現(xiàn)在Servcie類中嗎,需要的朋友可以參考下2023-05-05