SAP ABAP CGLIB(Code Generation Library)的模拟实现-阿里云开发者社区

开发者社区> jerrywangsap> 正文

SAP ABAP CGLIB(Code Generation Library)的模拟实现

简介: SAP ABAP CGLIB(Code Generation Library)的模拟实现
+关注继续查看

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.image.png

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


image.png


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


image.png


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:


(1) I have a class MyMEthodExitDemo which has a normal method myFun.

(2) 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.


image.png


(3) 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 ).


image.png


Execute result:


image.png


How does this example work under the hood?

I implement a demo in ABAP with the same logic.


(1) I have a very simple ABAP class with only one public method which will print “Hello World”:


image.png


(2) The CGLIB utility class in line 5 simulates the logic in Java, which will construct a proxy class based on existing global class ZCL_JAVA_CGLIB. The generated proxy class will be DYNAMICALLY injected with two enhancement implemented by zcl_jerry_preexit and zcl_jerry_postexit.


image.png


There are two interfaces defined for pre-exit and post-exit logics which contain only one EXECUTE method without any parameters:


image.png



And zcl_jerry_preexit and zcl_jerry_postexit implement these two interfaces accordingly:



image.png


once method greet in line 16 is called, the enhanced version of method greet is called:


image.png


By this way, the method greet is enhanced in a non-invasive approach – no modification on original class, no new class are persisted in repository.


The method get_proxy of zcl_abap_cglib_tool does the magic, I just follow the idea of CGLIB implementation in Java:


image.png


The pre-exit and post-exit passed by consumer are dynamically injected into proxy class here:


image.png


Till now you should be familiar with CGLIB idea, and it is quite easy to understand why it is NOT possible to create a proxy class based on a final class by CGLIB, simply because a final class could not be subclassed.


The report to consume it:

image.png

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Spring Boot 基础教程:入门简介
作为 Spring Boot 基础教程的开篇,本文主要介绍了 Spring Boot 的定义、特点以及它的优缺点,并探讨了我们为何要使用 Spring Boot 这一利器,使用它又能给我们带来什么好处。
16 0
Spring Boot 基础教程:配置详解
本文主要对 Spring Boot 项目中的配置文件进行了简单的介绍,并学习如何自定义属性并配置多环境项目,最后则是对比了当前主流的两种方式不同格式配置文件 yml 和 properties 的异同。
5 0
tddl连接池获取和释放流程疑问
最近遇到线上机器连接池满的情况,排查发现tddl里面在连接池获取和释放流程中有些问题
9 0
冬季实战营学习报告 第一期:从零到一上手玩转云服务器
这一期训练营从1月17号开始,为期5天。主要内容是了解学习阿里云的云服务(ECS) 因为提前加入了阿里云社区活动群,管理员每天都会提醒我们登录账号打卡学习,这里感谢管理员对我们的监督😊😊。
13 0
iLogtail 与Filebeat 性能对比
前段时间, iLogtail 阿里千万实例可观测采集器开源,其中介绍了iLogtail采集性能可以达到单核100MB/s,相比开源采集Agent有5-10倍性能优势。很多小伙伴好奇iLogtail具体的性能数据和资源消耗如何,本文将针对目前业界使用度较高且性能相对较优的Agent FileBeat进行对比,测试这两个Agent在不同压力场景下的表现如何。
5 0
Spring Boot 基础教程:创建 SpringBoot 项目的 3 种方式
上一篇中我们讲了 Spring Boot 的一些特点及优缺点,这一篇我们就来看看,如何创建一个最简单的 Spring Boot 项目!
7 0
阿里云初体验
web开发打包部署到linux服务器
16 0
使用APICloud AVM框架开发预约应用
前段时间跟朋友一起搞了一个预约的项目,前端用的APICloud的AVM框架做的,后端用的php开发的,用的tp5框架,没几天就搞出来了。简单跟大家分享一下开发中的一些功能点的实现吧。也欢迎大家一起探讨。
5 0
+关注
2628
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载