通过反射实现Java下的委托机制代码详解

 更新时间:2017年12月21日 11:19:44   作者:三向板砖  
这篇文章主要介绍了通过反射实现Java下的委托机制代码详解,具有一定借鉴价值,需要的朋友可以参考下。

简述

一直对Java没有现成的委托机制耿耿于怀,所幸最近有点时间,用反射写了一个简单的委托模块,以供参考。

模块API

public Class Delegater()//空参构造,该类管理委托实例并实现委托方法 
//添加一个静态方法委托,返回整型值ID代表该方法与参数构成的实例。若失败,则返回-1。 
public synchronized int addFunctionDelegate(Class<?> srcClass,String methodName,Object... params);
//添加一个实例方法委托,返回整型值ID代表该方法与参数构成的实例。若失败,则返回-1。 
public synchronized int addFunctionDelegate(Object srcObj,String methodName,Object... params);
//根据整型ID从委托实例中删除一个方法委托,返回是否成功 
public synchronized Boolean removeMethod(int registerID);
//依次执行该委托实例中的所有方法委托(无序) 
public synchronized void invokeAllMethod();
//将参数表转换为参数类型表 
private Class<?>[] getParamTypes(Object[] params);
//由指定的Class、方法名、参数类型表获得方法实例 
private Method getDstMethod(Class<?> srcClass,String methodName,Class<?>[] paramTypes);
class DelegateNode(Method refMethod,Object[] params)//DelegateNode类在不使用Object构造时叙述了一个静态方法委托,包括方法实例及参数表 
class DelegateNode(Object srcObj,Method refMethod,Object[] params)//DelegateNode类在使用Object构造时叙述了一个实例方法委托,包括类实例、方法实例及参数表 
public void invokeMethod();
//执行该节点叙述的方法委托

源代码

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;
/**Delegater类使用RTTI及反射实现Java下的委托机制 
 * @author 三向板砖 
 * */
public class Delegater {
	static int register = Integer.MIN_VALUE;
	//ID分配变量 
	Hashtable<Integer,DelegateNode> nodeTable;
	//管理ID与对应委托的容器 
	public Delegater() 
	  {
		nodeTable = new Hashtable<Integer,DelegateNode>();
	}
	//添加静态方法委托 
	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;
		}
	}
	//添加动态方法委托 
	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;
		}
	}
	//删除一个方法委托 
	public synchronized Boolean removeMethod(int registerID) 
	  {
		if(nodeTable.containsKey(registerID)) 
		    {
			nodeTable.remove(registerID);
			return true;
		}
		return false;
	}
	//无序地执行委托方法 
	public synchronized void invokeAllMethod() 
	  {
		for (DelegateNode node:nodeTable.values()) 
		    {
			node.invokeMethod();
		}
	}
	//将参数表转化为参数类型表 
	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;
	}
	//根据Class类实例、方法名、参数类型表获得一个Method实例 
	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!");
		}
	}
}

模块测试

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

执行结果:

StaticCustom!

StaticDelegate!

Custom!

HelloDelegate!

------------------

StaticCustom!

StaticDelegate!

Custom!

其他事项

一些public方法使用synchronized是为了保证register变量的线程安全,使其不会因为多线程而出错。

对于有返回值的委托,会报出警告,但模块还是接受这样的委托的,不过在执行委托时您将不能得到返回值。

添加的委托最大值是Integer.MAX_VALUE-Integer.MIN_VALUE超出后的容错处理没有考虑(一般也没这么多函数需要委托的吧。

委托执行是无序的,而且,需要性能要求时,委托的函数尽量不要有阻塞过程,否则会影响其他委托函数的执行。

还有什么问题可以发上来一同探讨。

总结

以上就是本文关于通过反射实现Java下的委托机制代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他Java相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • 关于java中构造函数的一些知识详解

    关于java中构造函数的一些知识详解

    下面小编就为大家带来一篇关于java中构造函数的一些知识详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Java的Struts框架中配置国际化的资源存储的要点解析

    Java的Struts框架中配置国际化的资源存储的要点解析

    这篇文章主要介绍了Java的Struts框架中配置国际化的资源存储的要点解析,针对用户所使用的语言来配置资源文件,需要的朋友可以参考下
    2016-04-04
  • Java网络编程实现的简单端口扫描器示例

    Java网络编程实现的简单端口扫描器示例

    这篇文章主要介绍了Java网络编程实现的简单端口扫描器,涉及Java网络编程Socket组建、swing组建及多线程相关操作技巧,需要的朋友可以参考下
    2018-04-04
  •  java中Thread.sleep()的具体使用

     java中Thread.sleep()的具体使用

    本文主要介绍了 java中Thread.sleep()的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 使用Springboot封装一个自适配的数据单位转换工具类

    使用Springboot封装一个自适配的数据单位转换工具类

    我们在接收前台传输的数据时,往往SpringBoot使用内置的数据类型转换器把我们提交的数据自动封装成对象等类型,下面这篇文章主要给大家介绍了关于使用Springboot封装一个自适配的数据单位转换工具类的相关资料,需要的朋友可以参考下
    2023-03-03
  • java发送kafka事务消息的实现方法

    java发送kafka事务消息的实现方法

    本文主要介绍了java发送kafka事务消息的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 解决mybatisPlus 中的field-strategy配置失效问题

    解决mybatisPlus 中的field-strategy配置失效问题

    这篇文章主要介绍了解决mybatisPlus 中的field-strategy配置失效问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Java date format时间格式化操作示例

    Java date format时间格式化操作示例

    这篇文章主要介绍了Java date format时间格式化操作,结合具体实例形式分析了java针对日期时间进行格式化操作的相关实现技巧,需要的朋友可以参考下
    2017-03-03
  • JAVA8 十大新特性详解

    JAVA8 十大新特性详解

    本教程将Java8的新特新逐一列出,并将使用简单的代码示例来指导你如何使用默认接口方法,lambda表达式,方法引用以及多重Annotation,之后你将会学到最新的API上的改进,比如流,函数式接口,Map以及全新的日期API
    2014-03-03
  • springboot实现发送QQ邮箱

    springboot实现发送QQ邮箱

    这篇文章主要为大家详细介绍了springboot实现发送QQ邮箱,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06

最新评论