实战CGLib系列之proxy篇(五):接口生成器InterfaceMaker

本文介绍了CGLib库中的InterfaceMaker工具,它能够动态生成包含指定类所有方法的接口。通过示例展示了如何使用InterfaceMaker创建接口,并结合Enhancer实现方法拦截,进一步展示了接口在代理类中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者shensy及出处http://shensy.iteye.com/blog/1887625

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

实战CGLib系列文章

本篇介绍接口生成器InterfaceMaker。

一、作用:

InterfaceMaker会动态生成一个接口,该接口包含指定类定义的所有方法。

二、示例:

比较简单,先定义一个类,仍使用本系列第一篇中的那个ConcreteClassNoInterface类,该类包含3个方法:

Java代码 复制代码  收藏代码
  1. public class ConcreteClassNoInterface {   
  2.     public String getConcreteMethodA(String str){   
  3.         System.out.println("ConcreteMethod A ... "+str);   
  4.         return str;   
  5.     }   
  6.     public int getConcreteMethodB(int n){   
  7.         System.out.println("ConcreteMethod B ... "+n);   
  8.         return n+10;   
  9.     }   
  10.     public int getConcreteMethodFixedValue(int n){   
  11.         System.out.println("getConcreteMethodFixedValue..."+n);   
  12.         return n+10;   
  13.     }   
  14. }  

用这个类内定义的方法来生成一个接口:

Java代码 复制代码  收藏代码
  1. InterfaceMaker im=new InterfaceMaker();   
  2. im.add(ConcreteClassNoInterface.class);   
  3. Class interfaceOjb=im.create();   
  4. System.out.println(interfaceOjb.isInterface());//true   
  5. System.out.println(interfaceOjb.getName());//net.sf.cglib.empty.Object$$InterfaceMakerByCGLIB$$13e205f  

interfaceOjb就是InterfaceMaker生成的接口,从接口名字可以看出。

看一下该接口内部的方法:

Java代码 复制代码  收藏代码
  1. Method[] methods = interfaceOjb.getMethods();   
  2. for(Method method:methods){   
  3.     System.out.println(method.getName());   
  4. }  
Method[] methods = interfaceOjb.getMethods();
for(Method method:methods){
	System.out.println(method.getName());
}

输出结果,与ConcreteClassNoInterface类内定义的方法完全相同:

控制台代码 复制代码  收藏代码
  1. getConcreteMethodA   
  2. getConcreteMethodB   
  3. getConcreteMethodFixedValue  
getConcreteMethodA
getConcreteMethodB
getConcreteMethodFixedValue

下面通过生成的接口,可以对某个类进行Enhancer(本系列前面介绍过Enhancer,此处不再讲解)。

Java代码 复制代码  收藏代码
  1. Object obj = Enhancer.create(Object.classnew Class[]{ interfaceOjb },    
  2. new MethodInterceptor() {   
  3.    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {   
  4.       return "intercept!";   
  5.    }   
  6. });   
  7.   
  8. Method method = obj.getClass().getMethod("getConcreteMethodA"new Class[]{String.class});   
  9. System.out.println(method.invoke(obj, new Object[]{"12345"}));  
Object obj = Enhancer.create(Object.class, new Class[]{ interfaceOjb }, 
new MethodInterceptor() {
   public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
      return "intercept!";
   }
});

Method method = obj.getClass().getMethod("getConcreteMethodA", new Class[]{String.class});
System.out.println(method.invoke(obj, new Object[]{"12345"}));

此处让Object生成的代理类实现了由InterfaceMaker生成的接口,但是由于Object类并没有覆写其中的方法,因此,每当对生成接口内方法进行MethodInterceptor方法拦截时,都返回一个字符串,并在最后打印出来。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值