我選擇在Android項(xiàng)目中來學(xué)習(xí)研究Java注解猫胁。從JDK 5開始箱亿,Java增加了注解,注解是代碼里的特殊標(biāo)記杜漠,這些標(biāo)記可以在編譯极景、類加載、運(yùn)行時(shí)被讀取驾茴,并執(zhí)行相應(yīng)的處理。通過注解氢卡,開發(fā)人員可以在不改變原有邏輯的情況下锈至,在源文件中嵌入一些補(bǔ)充的信息。
讓我們從以下示例入門Java注解:
- 創(chuàng)建兩個(gè)Java Module译秦,命名分別是annotations和processor
- 在annotations包中定義一個(gè)注解BindView
package com.example.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.FIELD)
public @interface BindView {
int value() default 1;
}
- 在processor包中定義一個(gè)注解處理器
package com.example.processor;
import com.example.annotations.BindView;
import com.google.auto.service.AutoService;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
@AutoService(Processor.class)
public class ClassProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
Messager messager = processingEnv.getMessager();
for (Element element:roundEnvironment.getElementsAnnotatedWith(BindView.class)) {
if (element.getKind() == ElementKind.FIELD) {
messager.printMessage(Diagnostic.Kind.NOTE,"printMessage:" + element);
}
}
return true;
}
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet<>();
annotations.add(BindView.class.getCanonicalName());
return annotations;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
- Messager 用于在注解處理期間打印消息(如錯(cuò)誤峡捡、警告、信息等)筑悴。
- roundEnv.getElementsAnnotatedWith(BindView.class) 返回所有被 @BindView 注解的元素
- element.getKind() 返回元素的類型们拙,ElementKind.FIELD 表示這是一個(gè)字段。
- 使用 Messager 的 printMessage 方法打印消息阁吝,Diagnostic.Kind.NOTE 表示這是一個(gè)注解處理期間的通知信息砚婆。
build.gradle
dependencies {
...
api 'com.google.auto.service:auto-service:1.0-rc6'
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'
}
- 在app Module中使用注解
package com.example.testannotation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.annotations.BindView;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_text)
TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
build.gradle
dependencies {
...
implementation project(':annotations')
annotationProcessor project(':processor')
}
- 進(jìn)行編譯,先clean突勇,再make
Executing tasks: [:processor:assemble, :processor:testClasses, :app:assembleDebug] in project /home/fukaiqiang/AndroidStudioProjects/TestAnnotation
> Task :annotations:compileJava UP-TO-DATE
> Task :processor:compileJava UP-TO-DATE
> Task :processor:processResources NO-SOURCE
> Task :processor:classes UP-TO-DATE
> Task :processor:jar UP-TO-DATE
> Task :processor:assemble UP-TO-DATE
> Task :processor:compileTestJava NO-SOURCE
> Task :processor:processTestResources NO-SOURCE
> Task :processor:testClasses UP-TO-DATE
> Task :app:createDebugVariantModel UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:mergeDebugNativeDebugMetadata NO-SOURCE
> Task :annotations:processResources NO-SOURCE
> Task :annotations:classes UP-TO-DATE
> Task :annotations:jar UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:javaPreCompileDebug UP-TO-DATE
> Task :app:checkDebugAarMetadata UP-TO-DATE
> Task :app:generateDebugResValues UP-TO-DATE
> Task :app:mapDebugSourceSetPaths UP-TO-DATE
> Task :app:generateDebugResources UP-TO-DATE
> Task :app:mergeDebugResources UP-TO-DATE
> Task :app:packageDebugResources UP-TO-DATE
> Task :app:parseDebugLocalResources UP-TO-DATE
> Task :app:createDebugCompatibleScreenManifests UP-TO-DATE
> Task :app:extractDeepLinksDebug UP-TO-DATE
> Task :app:processDebugMainManifest UP-TO-DATE
> Task :app:processDebugManifest UP-TO-DATE
> Task :app:processDebugManifestForPackage UP-TO-DATE
> Task :app:processDebugResources UP-TO-DATE
> Task :app:compileDebugJavaWithJavac
The following annotation processors are not incremental: processor.jar (project :processor).
Make sure all annotation processors are incremental to improve your build speed.
Note: printMessage:tv_text
> Task :app:mergeDebugShaders UP-TO-DATE
> Task :app:compileDebugShaders NO-SOURCE
> Task :app:generateDebugAssets UP-TO-DATE
> Task :app:mergeDebugAssets UP-TO-DATE
> Task :app:compressDebugAssets UP-TO-DATE
> Task :app:processDebugJavaRes NO-SOURCE
> Task :app:mergeDebugJavaResource UP-TO-DATE
> Task :app:checkDebugDuplicateClasses UP-TO-DATE
> Task :app:desugarDebugFileDependencies UP-TO-DATE
> Task :app:mergeExtDexDebug UP-TO-DATE
> Task :app:mergeLibDexDebug UP-TO-DATE
> Task :app:dexBuilderDebug
> Task :app:mergeDebugJniLibFolders UP-TO-DATE
> Task :app:mergeDebugNativeLibs NO-SOURCE
> Task :app:stripDebugDebugSymbols NO-SOURCE
> Task :app:validateSigningDebug UP-TO-DATE
> Task :app:writeDebugAppMetadata UP-TO-DATE
> Task :app:writeDebugSigningConfigVersions UP-TO-DATE
> Task :app:mergeProjectDexDebug
> Task :app:packageDebug
> Task :app:createDebugApkListingFileRedirect UP-TO-DATE
> Task :app:assembleDebug
BUILD SUCCESSFUL in 327ms
36 actionable tasks: 4 executed, 32 up-to-date
Build Analyzer results available
當(dāng)出現(xiàn) Note: printMessage:tv_text 時(shí)装盯,證明注解處理器配置成功。