Directory:

1. Basic knowledge

2. Write a beggar’s version of a ButterKnife

3, summarize

This is the first article about porting ButterKnife:

Start with the basics, understand what APT is, and finally output a Beggar-edition BufferKnife injection tool.

This is the basis for reading the BufferKnife source code.

The second chapter: explain the BufferKnife porting.

Basic knowledge of

Practical application of APT technology:

  • Android Databinding binds the View

  • ButterKnife binding view

  • Dagger2 injects variables

  • ARouter Generates the routing table

It’s a lot. It’s important. Apt is a productivity tool!

What is APT?

Apt is a plug-in that javac opens to the outside world. It enables JavAC to obtain annotations during compilation and process them accordingly (mostly generating Java code).

As you can see from the figure above, APT processes Java source files and comes in at compile time.

In contrast, tools such as ASM work with bytecode files and intervene late in the compilation process.

Apt convention with Javac

Apt and javac agreement in the meta-inf/services/javax.mail annotation. Processing. The Processor register file apt plug-in. Thus APT participates in the compilation process of javac.

Write a Beggar version of BufferKnife

Don’t want to see the text, directly look at the source, click gitee.com/andych008/a…

Initial code fork from github.com/LiMubai2017… Thank the author in advance.

As a View injection tool, The Beggar BufferKnife does three things.

  1. Parsing the annotation
  2. Working with annotations (generating template class files)
  3. Inject view objects through the template class

Step 0: Preparation

Define the annotation BindView first, and the variables marked by @bindView will be injected.

Annotations are usually defined in a separate module (named apt-annotation, for example) because they are relied on by both apt-Compiler and apt-API and are common code.

Apt-compiler is the main code of APT, which is used to parse annotations and generate template files.

Apt-api is an external utility class for users to use to complete injection operations.

App is a demo, and it’s defined

@BindView(value = ResourceTable.Id_text_helloworld)
public Text testTextView;
Copy the code

Step 1: Parse the annotations

Defined in apt – the compiler class BindViewProcessor javax.mail inheritance. The annotation. Processing. AbstractProcessor, Implementation of getSupportedAnnotationTypes annotations () registered to parse the method.

Step 2: Work with annotations (generate template files)

Implement the process() method in BindViewProcessor to handle annotations.

To understand javax.mail. Lang. Model. The element. The element and javax.mail lang. Model. The TypeMirror, reference here are explained in detail.

In short:

Element is a class that describes Java language elements, such as packages, classes, variables, parameters, and so on.

TypeMirror is a class that describes Element types, such as various primitive types, arrays, classes, and so on.

It’s tricky. You can only really understand it if you use it more. For example, testTextView in demo is the element type VariableElement

TypeElement enclosingElement = (TypeElement) variableElement.getEnclosingElement(); / / to get representative MainAbility TypeElement String field = variableElement. GetSimpleName (). The toString (); //testTextView TypeMirror typeMirror = variableElement.asType(); //ohos.agp.components.TextCopy the code

With the log() method, you can use Messager to log and verify our understanding.

log(String.format("element : (%s) %s ", element.getKind(), element)); log(String.format("bind : (%s) %s <--> id = %d", typeMirror, field, id)); Element: (FIELD) testTextView Note: Bind: (ohos.agp.components.Text) testTextView <--> id = 16777222Copy the code

GenerateCodeByPoet () method, Using javapoet generating code template MainAbility $$Autobind. Java (file path app/build/generated/source/annotation/debug/com/example/apt_demo/MainAbil Ity $$Autobind. Java)

For details about the use of Javapoet, go directly to the official documentation: github.com/square/java…

To give you a sense of javapoet, explain the following code

InjectMethod = injectMethod. MethodBuilder ("inject")// Generate a method, AddAnnotation (override.class)// Add "override.class" to the method. AddModifiers (modification.public)// Add access controls to the method .addStatement("$T substitute = ($T)target", className, className); // Define a language name in the method bodyCopy the code

The above code generates the following code (I use Java code to generate Java code, which is what Javapoet does) :

@Override
public void inject(Object target) {
  MainAbility substitute = (MainAbility)target;
}
Copy the code

Look at this mess up here, if you’re having trouble. Use JavaWriter to generate Java files. You’ll think Javapoet smells good.

Step 3: Inject the View object through the template class

In apt-API, we define an AutoBind. Java class to encapsulate operations on the template class MainAbility$$AutoBind.

MainAbility$$Autobind. Java is instantiated by reflection, and the inject method is called to complete view injection.

conclusion

Apt is just a tool, in this tool framework, how to deal with annotations is difficult.

BufferKnife is essentially no different from our “Beggar BufferKnife”. In addition to view injection, event binding and incremental compilation are supported.

Akik: Useless cat uncle

For more information, visit Harmonyos.51cto.com, a collaboration between 51CTO and Huawei