Detailed explanation of the use of AspectJ in Android AOP framework

preface

I have learned about the AOP framework of Android before, and its usage is mainly used to log; Now there is a requirement that the function should be executed in the new thread, and after the function body is executed, the result should be returned in the UI thread. If you want to write by hand, you have to operate new thread every time, which is more troublesome; Therefore, we try to solve this problem by annotation.

The core of AspectJ is its compiler. It does one thing. It inserts AspectJ code into the target program during compilation, and the runtime is no different from that in other places. Therefore, the key to using AspectJ is to use its compiler to compile code AJC. AJC will build the connection between the target program and AspectJ code, and insert AspectJ code into the cut-out pointcut during compilation, which has achieved the purpose of AOP.

Therefore, no matter on any IDE (if you use the command line, you can compile directly with AJC), the problem is to let the IDE compile code with AJC as the compiler.

code implementation

Annotation usage

The code is mainly associated with the AOP container through three annotations: tracelog, runonnewthread and runonnewthreadwithuicallback. The method of use is as follows:

Checkandrestartdownloadtask method, which wants the method body to execute in a new thread and print the log of the method execution; For the isshowtips forfirstvideocache method, you want the method body to execute in a new thread, return the result of the function to the UI thread through the dbquerycallback callback, and print the log of the method execution.

The AOP container recognizes these three annotations and implements the annotation interpreter.

Use the gradle script to load the AOP container

remarks

@The matching rule of runonnewthreadwithuicallback annotation requires the last parameter of the function to be dbquerycallback (there must be a callback parameter, otherwise it will be returned to the UI thread ~). The return value of the function must be consistent with the generic type of dbquerycallback, because the return value needs to be passed into the callback;

Note that final object obj = joinpoint. Processed();, After executing the function body, we get the return value of an object type by default, so we can't use the basic data type (Boolean for bool and interger for int). In addition, null in Java can be converted to any type, so even if NULL is returned directly in the function body, execute final object obj = joinpoint. Processed();, This type conversion will not be a problem. The pro test is effective and can be used safely

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>