introduce

  1. AOP for the abbreviation of Aspect Oriented Programming, meaning: section-oriented Programming, through pre-compilation and runtime dynamic proxy to achieve unified maintenance of program functions of a technology.
  2. AspectJ is a framework for faceted programming. AspectJ is an extension to Java and is fully Java-compatible. AspectJ defines AOP syntax and has a specialized compiler for generating Class files that comply with Java’s byte-encoding specifications. AspectJ also supports native Java, with annotations provided by AspectJ. In Android development, it generally provides annotations and some simple syntax can achieve most of the functional requirements.

Common noun definition

  1. A Joinpoint is a point that is intercepted
  2. Which Joinpoint join points are intercepted by the Pointcut
  3. Advice After interception to Joinpoint, all you need to do is notify, specific business logic processing, including: @before indicates that the notification is executed Before the target execution, @after indicates that the notification is executed After the target execution, and @around indicates that the notification is executed during the target execution to control the timing of the target execution
  4. Execution is used to match join points for method execution
  5. The @target execution method is used to match the current target object type, where the target object holds the specified annotation
  6. @annotation is used to match methods that the currently executing method holds the specified annotation
  7. An Aspect is a combination of a pointcut and an introduction

For example

Note: This article uses the latest version of AspectJ, version 1.9.4. MinSdkVersion 24 cannot be lower than Android 7.0

The classpath 'org. Aspectj: aspectjtools: 1.9.4'Copy the code
Implementation 'org. Aspectj: aspectjrt: 1.9.4'Copy the code

Login annotation, to cut the point

/** * @author YangTianFu * @date 2019/9/29 19:48 * @description */ @target (ElementType.METHOD) @Retention(retentionPolicy.runtime) public @interface LoginFilter {int userDefine() default 0; }Copy the code

The section of the login

package com.bliss.aspectjlogin.annotation.aspect; import android.content.Context; import com.bliss.aspectjlogin.annotation.LoginFilter; import com.bliss.aspectjlogin.core.ILogin; import com.bliss.aspectjlogin.core.LoginAssistant; import com.bliss.aspectjlogin.exception.AnnotationException; import com.bliss.aspectjlogin.exception.NoInitException; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import java.security.SignatureSpi; /** * @author YangTianFu * @date 2019/9/29 19:49 * @description */ public class LoginFilterAspect {private static  final String TAG = "LoginFilterAspect"; / / start the LoginFilter * wildcard all parameters of the type and number of the presence of the @pointcut return value method (" execution (@ com. Bliss. Aspectjlogin. The annotation. LoginFilter * * (..) Public void loginFilter(){} public void loginFilter(){} Call joinPoint.proceed() @around ("loginFilter()") public void aroundLoginPoint(ProceedingJoinPoint joinPoint) throws Throwable {/ / access to all the methods in the currently executing method and parameter Joinpoint (join) the point being intercepted ILogin ILogin = LoginAssistant. GetInstance (). GetiLogin (); If (iLogin == null){throw new NoInitException("LoginSDK not initialized!" ); } // Get the current method Signature Signature = joinPoint.getSignature(); if (! (Signature Instanceof MethodSignature)){throw new AnnotationException("LoginFilter annotations can only be used on methods "); } MethodSignature methodSignature = (MethodSignature) signature; LoginFilter loginFilter = methodSignature.getMethod().getAnnotation(LoginFilter.class); if ((loginFilter == null)) return; Context param = LoginAssistant.getInstance().getApplicationContext(); If (ilogin.isLogin (param)){// If you have already logged in, proceed with joinPoint.proceed(); } else {/ / not login to login iLogin. Login (param, loginFilter userDefine ()); }}}Copy the code

Registration method

package com.bliss.aspectjlogin; import android.app.Application; import android.content.Context; import android.content.Intent; import android.widget.Toast; import com.bliss.aspectjlogin.core.ILogin; import com.bliss.aspectjlogin.core.LoginSDK; import com.bliss.aspectjlogin.util.SharePreferenceUtil; /** * @author YangTianFu * @date 2019/9/29 19:05 * @description */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); LoginSDK.getInstance().init(this, iLogin); } ILogin iLogin = new ILogin() { @Override public void login(Context applicationContext, int userDefine) { switch (userDefine) { case 0: Intent intent = new Intent(applicationContext, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); break; Case 1: toast.maketext (applicationContext, "You have not logged in, please log in and execute ", toast.length_short).show(); break; Default: toast. makeText(applicationContext, "execution failed because you are not logged in!" , Toast.LENGTH_SHORT).show(); break; } } @Override public boolean isLogin(Context applicationContext) { return SharePreferenceUtil.getBooleanSp(SharePreferenceUtil.IS_LOGIN, applicationContext); } @Override public void clearLoginStatus(Context applicationContext) { SharePreferenceUtil.setBooleanSp(SharePreferenceUtil.IS_LOGIN, false, applicationContext); }}; }Copy the code

The source address