摘要:本文将带你了解Android开发之如何进行环境配置AbstractProcessor,希望本文对大家学Android有所帮助。
AbstractProcessor是抽象处理器,开发apt时都必须实现这个类来生成java文件,实现这个类后叫做
注解处理器,他是在编译时搜索注解并给与相应处理并生成相应的代码。对个注解的处理,是重点,重要在它能够获取所注解的元素的信息。不过这次只重点介绍AbstractProcessor,不把重心放在代码生成上。以后做apt时必定需要这个类。
首先说一下我们要实现这个类的函数
@Overridepublic synchronized void init(ProcessingEnvironment processingEnvironment){ super.init(processingEnvironment);//初始化我们需要的基础工具mTypeUtils = processingEnv.getTypeUtils();mElementUtils = processingEnv.getElementUtils();mFiler = processingEnv.getFiler();mMessager = processingEnv.getMessager();}在这个类获取很多工具类为了方便获取所注解的类的信息,工具类的具体使用后面说明@Overridepublic boolean process(Setset, RoundEnvironment roundEnvironment){Setelements = roundEnvironment.getElementsAnnotatedWith(ZyaoAnnotation.class);// 遍历所有被注解了@Factory的元素for (Element annotatedElement : elements) {// 检查被注解为@Factory的元素是否是一个类if (annotatedElement.getKind() != ElementKind.CLASS) {error(annotatedElement, "Only classes can be annotated with @%s",ZyaoAnnotation.class.getSimpleName());return true; // 退出处理}analysisAnnotated(annotatedElement);}return true;}遍历被注解的元素并给与相应处理@Overridepublic Set getSupportedAnnotationTypes(){//支持的注解Set annotations = new LinkedHashSet<>();annotations.add(ZyaoAnnotation.class.getCanonicalName());return annotations;}指定注解类型@Overridepublic SourceVersion getSupportedSourceVersion(){//支持的java版本return SourceVersion.RELEASE_7;}指定编译这个类使用的Java版本 apt环境配置 首先新建一个project,然后再新建两个JavaLibrary的 module,名字为annotation、compiler其中compiler需要添加依赖compile 'com.google.auto.service:auto-service:1.0-rc2'compile 'com.squareup:javapoet:1.7.0'app这个主module引入这个两个modulecompile project(':annotation')annotationProcessor project(':compiler')annotation负责注解代码,compiler负责将生成代码注解负责向依附的类添加运行代码,并且还能够将依附的类的信息传给compiler,使compiler能够生成符合依附的类的代码,这个代码给与注解,让注解在依附的类里运行代码。因为包名不好判断,代码我就连包名也贴出来这是annotaion的类 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Created by ZTH on 2018/1/22. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS) public @interface ZyaoAnnotation { String name() default "undefined"; String text() default ""; } 这是compiler的类 import com.example.annotation.ZyaoAnnotation; import com.google.auto.service.AutoService; import java.io.IOException; import java.io.Writer; import java.util.LinkedHashSet; 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.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.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.tools.Diagnostic; import javax.tools.JavaFileObject; @AutoService(Processor.class) public class MyProcessor extends AbstractProcessor { private Types mTypeUtils; private Elements mElementUtils; private Filer mFiler; private Messager mMessager; @Override public synchronized void init(ProcessingEnvironment processingEnvironment) { super.init(processingEnvironment); //初始化我们需要的基础工具 mTypeUtils = processingEnv.getTypeUtils(); mElementUtils = processingEnv.getElementUtils(); mFiler = processingEnv.getFiler(); mMessager = processingEnv.getMessager(); } @Override public SourceVersion getSupportedSourceVersion() { //支持的java版本 return SourceVersion.RELEASE_7; } @Override public Set<string> getSupportedAnnotationTypes() { //支持的注解 Set<string> annotations = new LinkedHashSet<>(); annotations.add(ZyaoAnnotation.class.getCanonicalName()); return annotations; } @Override public boolean process(Set<!--? extends TypeElement--> set, RoundEnvironment roundEnvironment) { if (set == null || set.isEmpty()) { return true; } Set<!--? extends Element--> elements = roundEnvironment.getElementsAnnotatedWith(ZyaoAnnotation.class); if (elements == null || elements.isEmpty()) { return true; } // 遍历所有被注解了@Factory的元素 for (Element annotatedElement : elements) { // 检查被注解为@Factory的元素是否是一个类 if (annotatedElement.getKind() != ElementKind.CLASS) { return true; // 退出处理 } analysisAnnotated(annotatedElement); } return true; } private static final String SUFFIX = "$$ZYAO"; private void analysisAnnotated(Element classElement) { ZyaoAnnotation annotation = classElement.getAnnotation(ZyaoAnnotation.class); String name = annotation.name(); String text = annotation.text(); // TypeElement superClassName = mElementUtils.getTypeElement(name); String newClassName = name + SUFFIX; StringBuilder builder = new StringBuilder() .append("package com.example.zth.myapplication;\n\n") .append("public class ") .append(newClassName) .append(" {\n\n") // open class .append("\tpublic String getMessage() {\n") // open method .append("\t\treturn \""); // this is appending to the return statement builder.append(text).append(name).append(" !\\n"); builder.append("\";\n") // end return .append("\t}\n") // close method .append("}\n"); // close class try { // write the file JavaFileObject source = mFiler.createSourceFile("com.example.zth.myapplication."+newClassName); Writer writer = source.openWriter(); writer.write(builder.toString()); writer.flush(); writer.close(); } catch (IOException e) { // Note: calling e.printStackTrace() will print IO errors // that occur from the file already existing after its first run, this is normal } } } </string></string> 这是app的类 @ZyaoAnnotation( name = "Zyao", text = "Hello !!! Welcome " ) public class MainActivity extends Activity { TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView)findViewById(R.id.tv1); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Zyao$$ZYAO zyao$$ZYAO = new Zyao$$ZYAO(); String message = zyao$$ZYAO.getMessage(); tv1.setText(message); } }); } }
如果MainActivity说没有什么类,那就先make project再试一下导包
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号