Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1317966
  • 博文数量: 932
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 10208
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-08 12:28
文章分类

全部博文(932)

文章存档

2021年(1)

2020年(134)

2019年(792)

2018年(5)

我的朋友

分类: 架构设计与优化

2019-12-14 08:47:18

What is cglib?A Byte Code Generation Library which is high level API to generate and transform Java byte code. It is used in various scenarios such as AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access.
See one example in unit test.
In line 17, a new dynamic proxy class is generated as mock.
In line 19, we tell the proxy, "if get(0) is called on this mock class, then return mocked data "hello, world".
As a result, in line 23 "result: hello, world" will be printed out.

In debugger you can find that the variable in line 17 is mocked by CGLIB:

Its byte code is generated dynamically and stored in variable byte[] b in line 217.


Let's see another example of injecting pre-exit and post-exit ( which ABAPers are very familiar with ) into a given method via dynamic proxy generated by CGLIB:
I have a class MyMEthodExitDemo which has a normal method myFun.
A new dynamic proxy class is generated in method createProxy which has a method with equal name as original class plus custom enhancement covered by class JerryEnhancement.

The pre-exit and post-exit are defined in class JerryEnhancement which implements interface MethodInterceptor defined in CGLIB library. The original method is generated in line 14, with pre-exit before it ( line 13 ) and post-exit after it ( line 15 ).

Execute result:

How can CGLIB be implemented in ABAP?

See my implementation here

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

阅读(864) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~