android abstract,Android应用开发之Android AbstractProcessor和apt环境配置

本文详细介绍了Android应用开发中AbstractProcessor的使用和apt环境配置。通过实现AbstractProcessor,开发者可以在编译时处理注解并生成代码。文章涵盖了init方法、process方法的实现,以及如何配置apt环境。同时,给出了一个简单的注解和处理器的示例,帮助读者更好地理解和应用AbstractProcessor。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文将带你了解Android应用开发Android AbstractProcessor和apt环境配置,希望本文对大家学Android有所帮助。

AbstractProcessor

AbstractProcessor是抽象处理器,开发apt时都必须实现这个类来生成java文件,实现这个类后叫做

注解处理器,他是在编译时搜索注解并给与相应处理并生成相应的代码。对个注解的处理,是重点,重要在它能够获取所注解的元素的信息。不过这次只重点介绍AbstractProcessor,不把重心放在代码生成上。以后做apt时必定需要这个类。

首先说一下我们要实现这个类的函数

@Override

public   synchronized void init(ProcessingEnvironment   processingEnvironment)

{   super.init(processingEnvironment);

//初始化我们需要的基础工具

mTypeUtils   = processingEnv.getTypeUtils();

mElementUtils =   processingEnv.getElementUtils();

mFiler =   processingEnv.getFiler();

mMessager =   processingEnv.getMessager();

}

在这个类获取很多工具类为了方便获取所注解的类的信息,工具类的具体使用后面说明

@Override

public   boolean process(Set

set, RoundEnvironment   roundEnvironment)

{

Set

elements =   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;

}

遍历被注解的元素并给与相应处理

@Override

public   Set

getSupportedAnnotationTypes()

{

//支持的注解

Set  annotations = new   LinkedHashSet<>();

annotations.add(ZyaoAnnotation.class.getCanonicalName());

return   annotations;

}

指定注解类型

@Override

public   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引入这个两个module

compile   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   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

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值