艾滋病新闻,太初天诺,补水护肤品
AbstractProcessor是抽象处理器,开发apt时都必须实现这个类来生成java文件,实现这个类后叫做
注解处理器,他是在编译时搜索注解并给与相应处理并生成相应的代码。对个注解的处理,是重点,重要在它能够获取所注解的元素的信息。不过这次只重点介绍AbstractProcessor,不把重心放在代码生成上。以后做apt时必定需要这个类。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 getSupportedAnnotationTypes() { //支持的注解 Set annotations = new LinkedHashSet<>(); annotations.add(ZyaoAnnotation.class.getCanonicalName()); return annotations; } @Override public boolean process(Set set, RoundEnvironment roundEnvironment) { if (set == null || set.isEmpty()) { return true; } Set 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 } } }这是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 apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论