APT, short for Annotation Processing Tool, is the ability to process annotations during code compilation and generate Java files to reduce manual code input. Some of the best-known frameworks, Retrofit and Arouter, use APT technology.

In this article, we will use APT technology to achieve routing functions similar to ARouter.

Without further ado, attach the code address in this article: APTdemo

Github.com/square/java…

Implementation effect

Common activities to jump to:

startActivity(new Intent(MainActivity.this, TwoActivity.class));
Copy the code

We want to do the Activity jump in the following ways:

SRouter.getInstance().navigate(MainActivity.this."router_two");
Copy the code

annotations

To create annotationLibrary, we first define annotations in the library to find out which activities need to be added to the route

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface Router {
    String name(a);
}
Copy the code

Annotation processor – compile

Create the Compilelibrary, which depends on the Annotation module, java-Libray, and create a RouterProcessor class that inherits from AbstractProcessor. We need to implement 4 methods, and the first 3 methods are very simple to write, so let’s talk a little bit about what they do;

  1. The init() method initializes some of the tools used, such as the file-related helper class Filer; Elements; The log related helper class Messager;
  2. GetSupportedSourceVersion () method returns a Java version;
  3. GetSupportedAnnotationTypes annotations () method returns to deal with;

At the heart of this is the process() method, which javapoet and annotations allow us to form corresponding classes. Ultimately we want to generate a class that looks like this:

public final class Router?GroupApp implements IRouterGroup {
    private Map<String, String> routeMap = new HashMap();

    publicRouter? GroupApp() {this.routeMap.put("router_two"."com.slyser.aptdemo.TwoActivity");
    }

    public String getActivityName(String routeName) {
        return null! =this.routeMap && !this.routeMap.isEmpty() ? (String)this.routeMap.get(routeName) : ""; }}Copy the code

The implementation code is not complicated, just tedious to write, but javapoet saves us a lot of work.

Register annotation handlers

How does the JVM find our custom annotation handler at compile time? This is where the SPI mechanism comes in. The easy way to do this is to create a new resources/ meta-INF /services file in the compile module. This file is very error-prone, and you can use Google auto-service

Add auto – service gradle dependence: implementation ‘com. Google. Auto. Service: auto – service: 1.0 -rc3’

Core library – API

Create API Library, this library is very simple, only a SRouter Class, the core is to reflect the annotation processor generated by the object, get the Activity Class, complete the Activity jump.

use

Add annotations to the Activity

@Router(name = "router_two")
public class TwoActivity extends AppCompatActivity
Copy the code

jump

SRouter.getInstance().navigate(this."router_two");
Copy the code

This is a simple route jump

conclusion

APT technology is actually a custom annotation and annotation processor, which generates Java files during compilation, similar to IOC inversion of control, which can be easily decouple. In multi-module development, APT technology can be used to construct a set of framework and remove class dependencies caused by startActivity.