In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to analyze ButterKnife, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
No matter whether they are old drivers or new drivers developed by Android, everyone must be tired of the boilerplate code like findViewById, especially when developing complex UI interfaces, this code will be very bloated, affecting not only the efficiency of development, but also the beauty.
As the saying goes, programmers who don't want to be lazy are not called engineers, so is there any way to make us write such code more efficiently?
Using the dependency injection framework
If you don't want to write boring boilerplate code, you can try the existing dependency injection library. As an open source framework written by Jake Wharton, ButterKnife claims to be able to achieve dependency injection during compilation, without reflection, and will not degrade program performance. So the question is, how on earth did it do it?
Preliminary study on ButterKnife
ButterKnife is an open source dependency injection framework written by Jake Wharton, which is similar to Android Annotations. It uses Java Annotation Tool to generate auxiliary code during compilation to achieve the purpose of View injection.
The annotation processor is a tool introduced by Java1.5 that provides the ability to scan and process annotations during program compilation. Its principle is to read Java code during compilation, parse annotations, and then generate Java code dynamically. The following figure shows the process of compiling the code in Java. You can see that our annotation processor works in the Annotation Processing phase, and the code generated by the annotation processor will eventually be compiled into Java bytecode together with the source code. However, it is a pity that you cannot modify existing Java files, such as adding new methods to existing classes, so View dependency injection can only be achieved through auxiliary classes through Java Annotation Tool, which will slightly increase the number of methods and classes in the project, but as long as it is well controlled, it will not have much impact on the project.
I will not introduce the use of ButterKnife in the business layer. The old drivers must be familiar with the road. If we write frameworks like ButterKnife ourselves, then our idea is this: define annotations, scan annotations, and generate code. At the same time, we need to use the following tools: JavaPoet (of course you can directly use Java Annotation Tool, and then directly through string concatenation to generate java source code, if you are not lovelorn), Java Annotation Tool and APT plug-ins. In order to better read the source code of ButterKnife, let's first introduce the basics of JavaPoet.
JavaPoet generates code
JavaPoet is an open source project that can generate .java source code, and it is also written by JakeWharton. We can work with the annotation processor to dynamically generate the code we need during the program compilation phase. Let's start with a basic example of using JavaPoet:
Where:
MethodSpec: represents a constructor or method declaration
TypeSpec: represents a class, interface, or enumeration declaration
FieldSpec: represents a member variable declaration
JavaFile: represents a top-level JAVA file
Running result:
Isn't that amazing? Our example is just to write the generated code to the output, ButterKnife through Java Annotation Tool's Filer can help us to output the JAVA source code in the form of a file. Q: so if I were to generate the following code, how would we write it?
It's very simple, just replace the MethodSpec with the following paragraph:
Then the code is gorgeously generated:
Oh, wait, what seems to be wrong, the format of the generated code is so strange! Should I write like this:
I'm sure it will meet our requirements, but it's a bit too troublesome. In fact, JavaPoet provides an addStatement interface that can automatically wrap lines and add semicolons, so our code can be written like this:
Generated code:
Well, in fact, the format is not so good-looking, right? And addStatement also needs to be used together with addCode. Why is it so hard to write a for loop (crying face). In fact, JavaPoet has long considered this problem. It provides two interfaces, beginControlFlow () + endControlFlow (), to provide line breaks and indentation. Combined with addStatement (), which is responsible for semicolons and line breaks, our code can be written like this:
The generated code is quite pleasing to the eye:
In fact, JavaPoet also provides a lot of useful interfaces to help us generate code more easily. For more detailed usage, please visit https://github.com/square/javapoet, which I will not repeat here.
Java Annotation Tool
So how does ButterKnife generate our auxiliary code through Java Annotation Tool? Let's take the source code of the latest version of ButterKnife, 8.4.0, as an example. If we write a framework like ButterKnife ourselves, then the first step must be to define our own annotations. In the butterknife-annotations package of the ButterKnife source code, we can see all the comments customized by ButterKnife, as shown in the following figure.
With custom annotations, our next step is to implement our own annotation processor. Let's learn about annotation processors in conjunction with ButterKnife's ButterKnifeProcessor class. In order to implement a custom annotation handler, you must first inherit the AbstractProcessor class. ButterKnifeProcessor implements four methods by inheriting from AbstractProcessor, as shown in the following figure:
Init (ProcessingEnvironment env)
By entering the ProcessingEnvironment parameter, you can get a lot of useful utility classes, such as Elements,Types,Filer and so on.
Elements is a utility class that can be used to deal with Element. It can be understood as all the elements scanned during Java Annotation Tool scanning, such as PackageElement, TypeElement, ExecuteableElement, etc.
Types is a utility class that can be used to deal with TypeMirror. It represents a type in Java language. We can use TypeMirror with Elements to determine whether an element is the type we want.
Filer is a tool class that generates JAVA source code. Whether it can generate java source code or not depends on it.
GetSupportedAnnotationTypes ()
Represents the types of annotations that can be supported by the annotation processor. As can be seen from the previous analysis, ButterKnife supports annotations such as BindView, OnClick, and so on.
GetSupportedSourceVersion ()
The supported version of JDK, generally using SourceVersion.latestSupported (), is also possible to use Collections.singleton (OPTION_SDK_INT) here.
Process (Set
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.