The following is learning Red orange Darren Big brother's article, and then make your own understanding notes
AOP is the abbreviation of Aspect Oriented Programming, and Chinese is tangential oriented programming. OK, next, you need to download a file. This file is downloaded here AspectJ Downloads | The Eclipse Foundation
The file indicated by the arrow in the figure aspectj-1.8.10.jar Click download
After downloading, open the jar package and click next all the way to automatically install it. This thing is installed on Disk C by default. By default, find the following directory
Remember the goods in the red box in the picture. We'll use it later
Next, create a new Android project and add aspectjrt.jar in the red box above Put the file into libs Directory, and then under the build.gradle file in the app directory, write the following
import org.aspectj.bridge.IMessage import org.aspectj.bridge.MessageHandler import org.aspectj.tools.ajc.Main final def log = project.logger final def variants = project.android.applicationVariants variants.all { variant -> if (!variant.buildType.isDebuggable()) { log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.") return; } JavaCompile javaCompile = variant.javaCompile javaCompile.doLast { String[] args = ["-showWeaveInfo", "-1.8", "-inpath", javaCompile.destinationDir.toString(), "-aspectpath", javaCompile.classpath.asPath, "-d", javaCompile.destinationDir.toString(), "-classpath", javaCompile.classpath.asPath, "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)] log.debug "ajc args: " + Arrays.toString(args) MessageHandler handler = new MessageHandler(true); new Main().run(args, handler); for (IMessage message : handler.getMessages(null, true)) { switch (message.getKind()) { case IMessage.ABORT: case IMessage.ERROR: case IMessage.FAIL: log.error message.message, message.thrown break; case IMessage.WARNING: log.warn message.message, message.thrown break; case IMessage.INFO: log.info message.message, message.thrown break; case IMessage.DEBUG: log.debug message.message, message.thrown break; } } } }
After completion, the project is shown in the figure below
OK, the next step is to write the code. The first step is to add an annotation file CheckNet
package com.kabun.aopdemo; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * * @Author: Kabun * @Time: 2021-09-28 * @description: */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface CheckNet { }
Step 2: add the java file SectionAspect. The code is as follows
package com.kabun.aopdemo; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; /** * * @Author: Kabun * @Time: 2021-09-28 * @description: */ @Aspect public class SectionAspect { @Pointcut("execution(@com.kabun.aopdemo.CheckNet * *(..))") public void checkNetBehavior(){ } @Around("checkNetBehavior()") public Object checkNet(ProceedingJoinPoint joinPoint) throws Throwable { GyLog.d("Cut to the right!!!!!!!!!!"); return joinPoint.proceed(); } }
Step 3: add a MainActivity file. The code is as follows
package com.kabun.aopdemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * Click button operation * @param view */ @CheckNet public void onclick(View view) { } }
Step 4, in activity_ Add a button to main, as shown in the figure below
Then, click compile and run, click button in the simulator, and the log print as shown in the following figure will appear
All right, it's over.
From the log output in the figure above, the In SectionAspect The log content in the checkNet method. This means that the The checkNet method was called. So, God, in MainActivity
How does the onclick method jump to the execution method? Regardless of the principle, you can find the @ CheckNet annotation on the onclick method from the clues on the scene. In that case, we can only start with it for the time being. After entering, we found that it was just built by us
CheckNet annotation class, and then it's gone. Is the clue interrupted? Don't panic, directly search the contents related to CheckNet string in the whole project. The mouse pays off. We found some traces in the newly created SectionAspect class, as shown in the following figure
In SectionAspect, the only place associated with CheckNet is checkNetBehavior() Annotation on method A string of things on the right of Pointcut. This string of things
("execution(@com.kabun.aopdemo.CheckNet * *(..))")
Did you find anything in this thing com.kabun.aopdemo.CheckNet
As if telling Which package does CheckNet come from, and then it's gone, because I don't know anything else...
Look again checkNetBehavior() , I found that the goods also appeared in In the annotation on checknet (proceeding joinpoint),
And then, in The log in the checknet (proceedingjoinpoint) method is printed. In this way, force the way of calling:
Class MainActivity (onclick method) - > @ interface checknet annotation ----- > Class SectionAspect annotation @ Pointcut ---> Class SectionAspect (checkNetBehavior) Method) --- >
Class SectionAspect (@ Around annotation) checkNetBehavior Method) -- > Class SectionAspect (checkNet method) -- > print log
Well, it seems that the forced explanation has passed, but in fact, I know how ignorant I am. So,
ok, that's all for this chapter. The next chapter will explore what the above-mentioned things do not understand, such as Pointcut , Pointcut Execution, Around, and even (* * asterisk in "execution(@com.kabun.aopdemo.CheckNet * * (..)"), and ellipsis in parentheses to the right of asterisk What the hell is separation