SAP ABAP和Java的动态代理实现

简介: SAP ABAP和Java的动态代理实现

In my blog Implement CGLIB in ABAP I demonstrate how to create dynamical proxy class via CGLIB in Java and ABAP. The generated proxy class is actually a subclass which inherits the base class. Such class created by CGLIB is transient, which means the life time of generated class is only within the current session where it is created, which will not be persisted.


In this blog I will show how to create a globally persistent proxy class dynamically in Java and ABAP.


The example is built based on Proxy design pattern.


Dynamic proxy in Java

There is one interface:image.pngThen I will create a dynamic proxy class ( which will be persisted to my laptop ) based on HelloWorldImp, with additional line System.out.println(\”Before Hello World!\”); before original method print() and System.out.println(\”After Hello World!\”); after method print().


The class object of generated proxy class is created via the following method which consists of four steps:image.pngstep2: create a new .java file in disk with source code generated in previous step:

image.pngimage.pngimage.pngDynamic proxy in ABAP

Again there is an interface IF_HELLOWORLD and an implementation class CL_HELLOWORLD based on which a new proxy class will be created dynamically.

image.png(1) The original instance of class CL_HELLOWORLD is passed to GET_PROXY method. Inside this method, it will inject the pre exit and post exit logic into the original implementation of print method. The injection is done in a new class, whose name is passed via parameter iv_new_class_name, in this example, ZCLABAP.

image.png(2) Once the above report is executed, the injected proxy instance returned in line 10 contains the enhanced logic so as expected you can now see the ABAP statement passed in iv_pre_exit and iv_post_exit are executed.

image.pngimage.pngBrief explanation about main logic of dynamic proxy generation

(1) extract_interface_info

extract the involved interface name and the name of method to be injected via RTTI against passed reference via parameter io_origin

(2) prepare_source_code

Inject the pre exit and post exit statement into method source code internal table.

(3) prepare_attr_and_signature

Prepare method signature and private attribute for new class

(4) generate_class

Call ABAP class generation function module based on metadata assembled by previous two steps.image.pngFurther reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection

Local class in ABAP, Java and JavaScript

Integer in ABAP, Java and JavaScript

Covariance in Java and simulation in ABAP

Various Proxy Design Pattern implementation variants in Java and ABAP


相关文章
|
1月前
|
开发者 供应链 BI
SAP ABAP CALL SUBSCREEN 代码解析
SAP ABAP CALL SUBSCREEN 代码解析
52 0
|
1月前
|
SQL Android开发
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
9 0
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
|
1月前
|
SQL 数据库 索引
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
20 1
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
|
1月前
|
存储
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
21 0
|
2天前
|
Java API 开发者
解密Java反射机制与动态代理
解密Java反射机制与动态代理
8 0
|
11天前
|
设计模式 Java 测试技术
滚雪球学Java(25):动态代理
【4月更文挑战第14天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
12 0
滚雪球学Java(25):动态代理
|
15天前
|
监控 Java 数据安全/隐私保护
Java中的动态代理
Java中的动态代理
|
15天前
|
设计模式 缓存 算法
Java 的静态代理和动态代理
Java 的静态代理和动态代理
|
16天前
|
设计模式 Java 索引
由反射引出的Java动态代理与静态代理
由反射引出的Java动态代理与静态代理
15 0
|
18天前
|
Java API Spring
Java基础教程(13)-Java中的反射和动态代理
【4月更文挑战第13天】Java反射机制允许程序在运行时获取类的信息并调用其方法。Class类是基础,提供获取类属性和方法的能力。通过Class对象,可以操作实例字段和方法,如getField、getDeclaredField等。动态代理是Java提供的创建接口实例的机制,其中JDK动态代理需目标类实现接口,而Cglib则可代理未实现接口的类。动态代理涉及Proxy和InvocationHandler接口。

热门文章

最新文章