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:
public interface IHelloWorld{
void print();}
And one implementation class:
class HelloWorldImp implements IHelloWorld{
public void print()
{
System.out.println("Hello World");
}}
Then 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:
private static Class<?> getProxyClass() {
String sourceCode = getSourceCode();
String javaFile = createJavaFile(sourceCode);
compile(javaFile);
return loadClass();
}
step1: populate source code of proxy class
step2: create a new .java file in disk with source code generated in previous step:
step3: compile the generated .java file in step 2 via API exposed by interface in package javax.tools.JavaCompiler, after compilation .class file will be generated in disk.
step4: use URLClassLoader to load the generated .class file in step3. After that it is available to create a new instance based on this loaded class via reflection.
Here below is the code how to consume this getProxyClass() method:
Once the above code is executed, you can observe:
(1) The original method of print is successfully enhanced via generated proxy class:
(2) corresponding .java and .class are persisted in the disk.
Dynamic 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.
Let’s first see what could be achieved in ABAP:
zcl_abap_dynamic_proxy_factory=>get_proxy(
EXPORTING
io_origin = NEW cl_helloworld( )
iv_new_class_name = 'ZCLABAP'
iv_pre_exit = `WRITE:/ 'Before hello world'.`
iv_post_exit = `WRITE:/ 'After hello world'.`
RECEIVING
ro_proxy = DATA(proxy) ).DATA(lo_helloworld) = CAST if_helloworld( proxy ).lo_helloworld->print( ).
(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.
(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.
And you can also open the generated proxy class in SE24:
The pre exit and post exit logic are injected here:
Brief 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.
Further 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
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2713821/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 淺談Java和SAP ABAP的靜態代理和動態代理,以及ABAP面向切面程式設計的嘗試Java程式設計
- Java動態代理 jdk和cglib的實現比較JavaJDKCGLib
- Java代理設計模式(Proxy)的四種具體實現:靜態代理和動態代理Java設計模式
- Java中的靜態代理和動態代理Java
- Java代理(jdk靜態代理、動態代理和cglib動態代理)JavaJDKCGLib
- Java的Covariance設計原理和SAP ABAP的模擬實現Java
- java 反射和動態代理Java反射
- SAP ABAP 動態內表實現 ALV橫向按月份動態顯示資料
- Java基礎系列-靜態代理和動態代理Java
- JDK 和 CGLib 實現動態代理和區別JDKCGLib
- Java動態代理(JDK和cglib)JavaJDKCGLib
- Java 靜態代理和動態代理的使用及原理解析Java
- JAVA 靜態代理 & 動態代理Java
- SAP ABAP Gateway Client 的 ABAP 實現,重用 HTTP ConnectionGatewayclientHTTP
- java執行原理、靜態代理和動態代理區分Java
- Java動態代理和反射機制Java反射
- 靜態代理和動態代理
- java動態代理Java
- JavaScript代理模式,怎麼實現物件的動態代理?JavaScript模式物件
- SAP ABAP ADBC和Java JDBC的使用比較JavaJDBC
- Java-JDK動態代理(AOP)使用及實現原理分析JavaJDK
- 使用Netty和動態代理實現一個簡單的RPCNettyRPC
- 深入理解 Java 反射和動態代理Java反射
- CGLib動態代理原理及實現CGLib
- RPC核心實現原理-動態代理RPC
- 細說JDK動態代理的實現原理JDK
- java動態代理動態在哪裡?Java
- java動態代理(1)Java
- Java動態代理(AOP)Java
- Java 動態代理newProxyInstanceJava
- 輕鬆理解 Java 靜態代理/動態代理Java
- MyBatis進階--介面代理方式實現Dao 和動態SQLMyBatisSQL
- 如何實現Java 設定動態代理ip的具體操作步驟Java
- SAP ABAP OData 服務的 $count 操作實現
- 讓我們打一場動態代理的官司–Java動態代理Java
- 你必須會的 JDK 動態代理和 CGLIB 動態代理JDKCGLib
- Java進階--Java動態代理Java
- 純手寫實現JDK動態代理JDK