背景
一直在用,感谢有些荒废最近总结下APT,这里APT啊其实就是在编译期生成.java文件。
APT(Annotation Processing Tool)是一种注解处理工具,它对源文件代码进行检测,找出其中的注解,并对此进行额外的处理,如检查代码书写规范,生成额外的源文件等(具体内容由注解处理器的实现所决定)。我们基本就是生成java文件用的多。
场景
1、多模块开发,解耦
2、无缝衔接实现类
这里我们就直接来干货、总结为主。
材料
- 注解类Annotation
- 注解处理类
注解类
作用:标识当前APT姿势处理范围。
这里我们是针对类处理@Target(ElementType.TYPE) ,这里我们是要在编译期生成类,所以保留位置@Retention(RetentionPolicy.CLASS)
Java中元注解有四个: @Retention @Target @Document @Inherited;
@Retention:注解的保留位置
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Target:注解的作用目标
@Target(ElementType.TYPE) //接口、类、枚举
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
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.TYPE)
public @interface TestAnno {
}
注解处理类
作用:处理注解类所标识的类
1、配置gradle
apply plugin: 'java'
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.auto.service:auto-service:1.0-rc6'//谷歌的帮助我们快速实现注解处理器
compile project(':annotion_a')//自己定义的注解的java lib
compile 'com.squareup:javapoet:1.7.0'//用来生成java文件的,避免字符串拼接的尴尬
}
2、创建注解类处理类processer
package notification.violet.com.annotion_compile;
import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
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.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
/**
* Created by violet_k on 2020/4/6.
*/
//@AutoService(Processor.class)
public class ActivityInjectProcesser extends AbstractProcessor {
private Filer mFiler; //文件相关的辅助类
private Elements mElementUtils; //元素相关的辅助类 许多元素
private Messager mMessager; //日志相关的辅助类
private Map<String, AnnotatedClass> mAnnotatedClassMap;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
mFiler = processingEnv.getFiler();
mElementUtils = processingEnv.getElementUtils();
mMessager = processingEnv.getMessager();
mAnnotatedClassMap = new TreeMap<>();
log("-------------------------初始化----------------------");
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
mAnnotatedClassMap.clear();
log("-------------------------执行----------------------");
try {
processActivityCheck(roundEnv);
} catch (Exception e) {
e.printStackTrace();
error(e.getMessage());
}
for (AnnotatedClass annotatedClass : mAnnotatedClassMap.values()) {
try {
annotatedClass.generateActivityFile().writeTo(mFiler);
} catch (Exception e) {
error("Generate file failed, reason: %s", e.getMessage());
}
}
return true;
}
private void processActivityCheck(RoundEnvironment roundEnv) throws IllegalArgumentException, ClassNotFoundException {
//check ruleslass forName(String className
for (Element element : roundEnv.getElementsAnnotatedWith((Class<? extends Annotation>) Class.forName(TypeUtil.ANNOTATION_PATH))) {
if (element.getKind() == ElementKind.CLASS) {
getAnnotatedClass(element);
} else
error("ActivityInject only can use in ElementKind.CLASS");
}
}
private AnnotatedClass getAnnotatedClass(Element element) {
// tipe . can not use chines so ....
// get TypeElement element is class's --->class TypeElement typeElement = (TypeElement) element
// get TypeElement element is method's ---> TypeElement typeElement = (TypeElement) element.getEnclosingElement();
TypeElement typeElement = (TypeElement) element;
String fullName = typeElement.getQualifiedName().toString();
AnnotatedClass annotatedClass = mAnnotatedClassMap.get(fullName);
if (annotatedClass == null) {
annotatedClass = new AnnotatedClass(typeElement, mElementUtils, mMessager);
mAnnotatedClassMap.put(fullName, annotatedClass);
}
return annotatedClass;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> types = new LinkedHashSet<>();
types.add(TypeUtil.ANNOTATION_PATH);
return types;
}
private void error(String msg, Object... args) {
mMessager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args));
}
private void log(String msg, Object... args) {
mMessager.printMessage(Diagnostic.Kind.NOTE, String.format(msg, args));
}
}
/**
* Created by violet_k on 2020/4/6.
*/
public class AnnotatedClass {
private TypeElement mTypeElement;//activity //fragmemt
private Elements mElements;
private Messager mMessager;//日志打印
public AnnotatedClass(TypeElement typeElement, Elements elements, Messager messager) {
mTypeElement = typeElement;
mElements = elements;
this.mMessager = messager;
}
public JavaFile generateActivityFile() {
// build inject method
MethodSpec.Builder injectMethod = MethodSpec.methodBuilder(TypeUtil.METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.addParameter(TypeName.get(mTypeElement.asType()), "activity", Modifier.FINAL);
injectMethod.addStatement("android.widget.Toast.makeText" +
"(activity, $S,android.widget.Toast.LENGTH_SHORT).show();", "from build");
//generaClass
TypeSpec injectClass = TypeSpec.classBuilder(mTypeElement.getSimpleName() + "$$InjectActivity")
.addModifiers(Modifier.PUBLIC)
.addMethod(injectMethod.build())
.build();
String packgeName = mElements.getPackageOf(mTypeElement).getQualifiedName().toString();
return JavaFile.builder(packgeName, injectClass).build();
}
}
/**
* Created by violet_k on 2020/4/6.
*/
public class TypeUtil {
public static final String METHOD_NAME = "inject";
public static final String ANNOTATION_PATH = "notification.violet.com.annotion_a.TestAnno";
}
使用
1、在project 的gradle中增加apt依赖
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
2、新建APP,在APP gradle中依赖之前定义注解和注解处理类
apply plugin: 'com.neenbedankt.android-apt'
依赖增加如下:
compile project(':annotion_a')
apt project(':annotion_compile')
在MainActivity 头上加定义注解
@TestAnno
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用build生成的类
InjectActivity.inject(this);
}
}
3、然后直接通过类加载方式获取即可
public class InjectActivity {
private static final ArrayMap<String, Object> injectMap = new ArrayMap<>();
public static void inject(AppCompatActivity activity) {
String className = activity.getClass().getName();
try {
Object inject = injectMap.get(className);
if (inject == null) {
Class<?> aClass = Class.forName(className + "$$InjectActivity");
inject = aClass.newInstance();
injectMap.put(className, inject);
}
Method m1 = inject.getClass().getDeclaredMethod("inject", activity.getClass());
m1.invoke(inject, activity);
} catch (Exception e) {
e.printStackTrace();
}
}
}
小建议
这里有一个auto-service,说到这个就要谈到SPI,这里不展开,我们这里小记下,如果auto-service和我们项目配置冲突,如何解决。
这里auto-service作用:就是帮助我们配置对注解处理类生成META-INF说明, 如果说我们无法使用autoService,这里不要着急,解决如下:
我们在main包下新建resources包,然后把META-INF放入,里面按照图中新建就OK,最后把你的注解类处理类全路径加入就OK。
META-INF的路径.png
javax.annotation.processing.Processor文件里面配置内容就是你的注解类处理类的全路径
notification.violet.com.annotion_compile.ActivityInjectProcesser
以上也是看了简书上优秀文章总结,代码经过一个简书文章改良而来,忘记链接了对不住,只记得代码作者:JokAr,非常感谢,如果作者看到了在下方留言链接,我贴到这里补充上~。
补上github地址:
https://github.com/violet520/Journey
网友评论