APT(Annotation Processing Tool)
javaPoet What is it? ?
Actual project structure
Definition of annotation
package com.wust.arouter_annotations; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import
java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *
ClassName: ARouter <br/> * Description: <br/> * date: 2022/2/10 15:13<br/> * *
@author yiqi<br /> * @QQ 1820762465 * @ WeChat yiqiideallife * @ Technical exchange QQ group 928023749 */
@Target(ElementType.TYPE) // Class above @Retention(RetentionPolicy.CLASS) // Compilation period
public @interface ARouter { String path(); String group() default ""; }
annotation processor Dependency configuration Added
* Preparation of configuration file plugins { id 'java-library' } dependencies { implementation
fileTree(dir: 'libs', includes: ['*.jar']) // Service behind --> Help us automatically monitor the compilation of code compileOnly
'com.google.auto.service:auto-service:1.0-rc3' annotationProcessor
'com.google.auto.service:auto-service:1.0-rc4'
// Help us generate by class call Java code 【javaPoet】 implementation 'com.squareup:javapoet:1.9.0'
// Dependency annotation implementation project(":arouter-annotations") } java {
sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility =
JavaVersion.VERSION_1_7 }
* Configuration file authoring location
app Project dependent annotation configuration and use
* app Project dependency Annotation module Add configuration
* app Project use Annotation module Defined annotation package com.wust.newmodueljavapoet; import
android.app.Activity; import android.os.Bundle; import
androidx.annotation.Nullable; import com.wust.arouter_annotations.ARouter; /**
* ClassName: MainActivity <br/> * Description: <br/> * date: 2022/2/10
16:14<br/> * * @author yiqi<br /> * @QQ 1820762465 * @ WeChat yiqiideallife *
@ Technical exchange QQ group 928023749 */ @ARouter(path = "MainActivity") public class MainActivity
extends Activity { @Override protected void onCreate(@Nullable Bundle
savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } }
Improvement of annotation processor class processing method
package com.wust.compiler; import com.google.auto.service.AutoService; import
java.io.File; import java.util.Set; import
javax.annotation.processing.AbstractProcessor; import
javax.annotation.processing.Filer; import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment; import
javax.annotation.processing.RoundEnvironment; import
javax.annotation.processing.SupportedAnnotationTypes; import
javax.annotation.processing.SupportedSourceVersion; import
javax.lang.model.SourceVersion; import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements; import javax.lang.model.util.Types; /**
* ClassName: ARouterProcessor <br/> * Description: <br/> * date: 2022/2/10
16:20<br/> * * @author yiqi<br /> * @QQ 1820762465 * @ WeChat yiqiideallife *
@ Technical exchange QQ group 928023749 */ @AutoService(Processor.class) // Enable service , See clearly , Don't choose
Process.class, Otherwise you won't see the annotation processor execute
@SupportedSourceVersion(SourceVersion.RELEASE_7) // Configuration of environment
@SupportedAnnotationTypes("com.wust.arouter_annotations.ARouter") // Specify this annotation processor
handle Which annotation public class ARouterProcessor extends AbstractProcessor { // operation Element
Tool class for ( class , function , attribute , Actually, they are Element) private Elements elementTool;
//type( Class information ) Tool class for , Contains for operation TypeMirror Tools and methods private Types typeTool; //Message Used to print
Log related information private Messager messager; // File generator , class resources etc. , Is the final file to be generated Yes Filer To finish it
private Filer filer; @Override public synchronized void
init(ProcessingEnvironment processingEnv) { super.init(processingEnv); // initialization
elementTool = processingEnv.getElementUtils(); messager =
processingEnv.getMessager(); filer = processingEnv.getFiler(); } // Work while compiling
@Override public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) { return false; } }
How does the annotation processor get it Android code
General idea : Only through gradle To achieve
* app Under the project build.gradle Configure in // Transfer parameters javaCompileOptions {
annotationProcessorOptions { arguments = [MyName: 'yiqi'] } }
// Processor dependency annotation Annotation processor to work annotationProcessor project(":compiler")
* Receive parameters ··· ellipsis ··· /** * ClassName: ARouterProcessor <br/> * Description: <br/>
* date: 2022/2/10 16:20<br/> * * @author yiqi<br /> * @QQ 1820762465 * @ WeChat
yiqiideallife * @ Technical exchange QQ group 928023749 */ // receive Parameters passed from Android project
@SupportedOptions("MyName") ··· ellipsis ··· public class ARouterProcessor extends
AbstractProcessor { ··· ellipsis ··· @Override public synchronized void
init(ProcessingEnvironment processingEnv) { super.init(processingEnv); ··· ellipsis ···
// Output the obtained value String value = processingEnv.getOptions().get("MyName");
messager.printMessage(Diagnostic.Kind.NOTE, "heheheh>" + value); } // Work while compiling
@Override public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) { messager.printMessage(Diagnostic.Kind.NOTE,
"process is run"); return false; } }
Display of operation results
Project structure Sorting chart
Technology
Daily Recommendation