AOP

Aspect-oriented programming (AOP) is a technique that can dynamically and uniformly add functionality to programs without modifying source code through precompilation and runtime dynamic proxy implementation. AOP is the continuation of OOP, is a hot spot in the software development, is a derived paradigm of functional programming, the code into the designated method of class, designated location on the programming idea. Using AOP, each part of the business logic can be isolated, thus reducing the degree of coupling between each part of the business logic, improving the reusability of the program, and improving the efficiency of development

AspectJ

An extension for faceted programming that works seamlessly with the Java language (available for Android).

  1. Import dependence

/ / 1 classpath 'org. Aspectj: aspectjtools: 1.9.6'Copy the code
/ / 2 API 'org. Aspectj: aspectjrt: 1.9.6'Copy the code
//3 import org.aspectj.bridge.IMessage import org.aspectj.bridge.MessageHandler import org.aspectj.tools.ajc.Main final Def log = project.logger // "LibraryVariants Final Def =" would be replaced by "applicationVariants" in the line below the Library Module Project. Android. ApplicationVariants / / in building engineering, perform weaving 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 } } } }Copy the code
  1. code

Custom annotation classes that are annotated by methods can display log@Retention (retentionPolicy.runtime)// compile @target ({elementtype.method}) at RUNTIME // act on methods public @interface IsLoginInject { boolean value() default true; LanguageEnum Language () default Languageenm.cn_sim; } public enum LanguageEnum {CN_SIM, EN, JP}Copy the code
@aspect //Aspect annotation Public class CusAspectJAnnotationBind {// @around override function execution added the @isLoginInject method to execute the method override logic at compile time @Around("execution(@com.example.scrollviewd.ui.annotation.IsLoginInject * *(..) )") public void aspectAnalyzeAnnotation(ProceedingJoinPoint joinPoint) { MethodSignature ms = (MethodSignature) joinPoint.getSignature(); String TAG=joinPoint.getTarget().getClass().getSimpleName(); try { Method me = joinPoint.getTarget().getClass().getDeclaredMethod(ms.getName(), ms.getParameterTypes()); IsLoginInject isLoginInject = me.getAnnotation(IsLoginInject.class); if (isLoginInject.value()) { if (AppDevice.getInstance().isLogin()) { joinPoint.proceed(); } else {if (isLoginInject.language()==LanguageEnum.CN_SIM) {log.d (TAG, "account not logged in "); } else if (isLoginInject. Language () = = LanguageEnum. JP) {the d (TAG, "ア カ ウ ン ト が login さ れ て い ま せ ん"); }else if (isLoginInject.language()==LanguageEnum.EN) { Log.d(TAG, "Account not logged in"); }else{ Log.d(TAG, "Error"); } } } else { joinPoint.proceed(); }} catch (NoSuchMethodException e) {e.printstackTrace (); } catch (Throwable throwable) { throwable.printStackTrace(); }}}Copy the code
  1. The results of

AppDevice.getInstance().isLogin()==true? Go original method code: intercept method, do not execute the following code, and print a log