0x01 继承AbstractProcessor抽象类
当定义好Annotation注解后,接下来就需要一个注解处理器来处理我们的自定义注解了。实现Java Annotation一般需要继承AbstractProcessor抽象类,并且重写其四个方法来实现提取,解析并处理自定义注解的逻辑如下:
class WondertwoProcessor extends AbstractProcessor {
//返回注解处理器可处理的注解操作
@Override
public Set<String> getSupportedOptions() {
return super.getSupportedOptions();
}
//得到注解处理器可以支持的注解类型
@Override
public Set<String> getSupportedAnnotationTypes() {
return super.getSupportedAnnotationTypes();
}
//执行一些初始化逻辑
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
}
//核心方法,扫描,解析并处理自定义注解,生成***.java文件
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}
}
0x02 重写核心方法process()
由上可知process()方法才是扫描,解析,处理注解的核心方法,动手实战一下写一个简单的WondertwoProcessor来提取自定义注解@CustomizeInterface
,然后借助JavaPoet生成Java接口文件。
/**
* 自定义注解处理器,将类中public方法提取为接口方法(不含static方法)
* {
* Exec: apt -factory annotation3.WondertwoFactory
* ProvinceDefiner.java -s ../annotaion3
* }
* Created by wondertwo on 2016/10/18.
*/
class WondertwoProcessor extends AbstractProcessor {
private ProcessingEnvironment envir;
public WondertwoProcessor(ProcessingEnvironment env) {
this.envir = env;
}
@Override
public Set<String> getSupportedOptions() {
return super.getSupportedOptions();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return super.getSupportedAnnotationTypes();
}
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement typeEle : annotations) {
WondertwoInterface wondertwoInterface = typeEle.getAnnotation(WondertwoInterface.class);
if (wondertwoInterface == null) break;
Class clazz = typeEle.getClass();
if (clazz.getDeclaredMethods().length > 0) {
try {
if (typeEle.getModifiers().contains(Modifier.PUBLIC)
&& !typeEle.getModifiers().contains(Modifier.STATIC)) {
PrintWriter writer = (PrintWriter) envir.getFiler()
.createSourceFile(wondertwoInterface.value());
writer.println("package " + clazz.getPackage().getName() + ";");
writer.println("public interface " + wondertwoInterface.value() + " {");
for (Method method : clazz.getDeclaredMethods()) {
writer.print(" public ");
writer.print(method.getReturnType() + " ");
writer.print(method.getName() + " (");
int i = 0;
for (TypeParameterElement parameter : typeEle.getTypeParameters()) {
writer.print(parameter.asType() + " " + parameter.getSimpleName());
if (++i < typeEle.getTypeParameters().size())
writer.print(", ");
}
writer.println(");");
}
writer.println("}");
writer.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return true;
}
}
看过《Java编程思想》的同学肯定对上面的实例非常眼熟,书中对应的实例也是提取非静态公有方法生成接口源文件,但由于是JDK6.0标准已经有很多API发生了很大的变化,本例基于JDK8!
可以看到我们只在process()方法中加入了处理注解,生成.java文件的逻辑,这里是的逻辑是根据自定义注解提取对应类的非静态public方法,然后将抽取的非静态共有方法拼接成对应的接口!
0x03 实例探究:Android依赖注解库ButterKnife
不会偷懒的程序员不是一个好程序员,Android开发者对ButterKnife依赖注解库一定耳熟能详,当我们UI布局中控件很多的时候ButterKnife无疑显著提高了开发效率。
作为一个注解库其实现的原理依然是Java Annotation的方式,我们在Github翻出ButterKnife源码文件,找到其核心类——注解处理类ButterKnifeProcessor.java,源码较长删减后如下:
public final class ButterKnifeProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env) {
super.init(env);
elementUtils = env.getElementUtils();
typeUtils = env.getTypeUtils();
filer = env.getFiler();
}
@Override public Set<String> getSupportedAnnotationTypes() {
Set<String> types = new LinkedHashSet<String>();
types.add(Bind.class.getCanonicalName());
for (Class<? extends Annotation> listener : LISTENERS) {
types.add(listener.getCanonicalName());
}
types.add(BindBool.class.getCanonicalName());
types.add(BindColor.class.getCanonicalName());
types.add(BindDimen.class.getCanonicalName());
types.add(BindDrawable.class.getCanonicalName());
types.add(BindInt.class.getCanonicalName());
types.add(BindString.class.getCanonicalName());
return types;
}
@Override public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
Map<TypeElement, BindingClass> targetClassMap = findAndParseTargets(env);
for (Map.Entry<TypeElement, BindingClass> entry : targetClassMap.entrySet()) {
TypeElement typeElement = entry.getKey();
BindingClass bindingClass = entry.getValue();
try {
JavaFileObject jfo = filer.createSourceFile(bindingClass.getFqcn(), typeElement);
Writer writer = jfo.openWriter();
writer.write(bindingClass.brewJava());
writer.flush();
writer.close();
} catch (IOException e) {
error(typeElement, "Unable to write view binder for type %s: %s", typeElement,
e.getMessage());
}
}
return true;
}
@Override public Set<String> getSupportedOptions() {
return Collections.singleton(OPTION_SDK_INT);
}
}
如果想要进一步了解ButteKnife扫描,解析,处理注解,生成Java代码的每一部细节,可以参考文章:浅析ButterKnife
网友评论