1、 运行时注解
运行时注解是通过反射在程序运行时获取注解信息,然后利用信息进行其他处理。下面是运行时注解的一个简单Damo,包含Company、EmployeeName、EmployeeSex注解定义以及EmployeeInfoUtil注解处理器,客户端包含EmployeeInfo类(成员变量使用注解)和一个main方法。
Compay代码:
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Company { public int id() default -1; public String name() default ""; public String address() default ""; }
EmployeeName代码: @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface EmployeeName { String value () default ""; } EmployeeSex代码: @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface EmployeeSex { enum Sex{Man,Woman}//定义性别枚举 Sex employeeSex() default Sex.Man; }
上一篇文章我们也介绍了,注解的关键是注解处理器,下面的注解处理器仅仅获取注解信息
public class EmployeeInfoUtil {
public static Map getEmployeeInfo(Class<?> clazz){
HashMap<String ,String> info = new HashMap<>();
Field[] fields = clazz.getDeclaredFields();//获取类成员变量
for (Field field: fields) {//遍历
if (field.isAnnotationPresent(EmployeeName.class)){//判断是不是EmployeeName类型注解
EmployeeName employeeName = field.getAnnotation(EmployeeName.class);
info.put("employeeName",employeeName.value());//获取注解的值
}
if (field.isAnnotationPresent(EmployeeSex.class)) {
EmployeeSex employeeSex = field.getAnnotation(EmployeeSex.class);
info.put("employeeSex",employeeSex.employeeSex().toString());
}
if (field.isAnnotationPresent(Company.class)) {
Company company = field.getAnnotation(Company.class);
info.put("company",company.id()+":"+company.name()+":"+company.address());
}
}
return info;
}
}
EmployeeInfo代码:
public class EmployeeInfo {
@EmployeeName("zfq")
private String employeeName;
@EmployeeSex(employeeSex = EmployeeSex.Sex.Woman)
private String employeeSex;
@Company(id = 1,name = "HYR集团",address = "河南开封")
private String company;
//省略set和get方法
}
客户端代码:
public class EmployeeRun { public static void main(String[] args) { Map fruitInfo = EmployeeInfoUtil.getEmployeeInfo(EmployeeInfo.class); System.out.println(fruitInfo); }
运行结果:
{employeeName=zfq, employeeSex=Woman, company=1:HYR集团:河南开封}
2、编译时注解
编译时注解是在程序编译的时候动态的生成一些类或者文件,所以编译时注解不会影响程序运行时的性能,而运行时注解则依赖于反射,反射肯定会影响程序运行时的性能,所以一些知名的三方库一般都是使用编译时时注解,比如大名鼎鼎的ButterKnife、Dagger、Afinal等。下面编译时注解是编译时打印使用指定注解的方法的方法信息,注解的定义和运行时注解一样,主要是注解处理器对注解的处理不同 。首先AS的工程中新加Module,选择java Library(指定library name)。
@InjectPrint注解代码:
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.CLASS) public @interface InjectPrint { String value(); }
InjectPrintProcessor处理器代码:
@SupportedAnnotationTypes("com.example.InjectPrint")//参数是指定注解类型的全路径
public class InjectPrintProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
//获取InjectPrint类型注解,然后遍历
for(Element element : roundEnvironment.getElementsAnnotatedWith(InjectPrint.class)){
//元素类型是一个方法
if(element.getKind() == ElementKind.METHOD){
//强转成方法对应的element,同
// 理,如果你的注解是一个类,那你可以强转成TypeElement
ExecutableElement executableElement = (ExecutableElement)element;
//打印方法名
System.out.println(executableElement.getSimpleName());
//打印方法的返回类型
System.out.println(executableElement.getReturnType().toString());
//获取方法所有的参数
List<? extends VariableElement> params = executableElement.getParameters();
for(VariableElement variableElement : params){//遍历并打印参数名
System.out.println(variableElement.getSimpleName());
}
//打印注解的值
System.out.println("AnnotationValue:"+executableElement.getAnnotation(InjectPrint.class).value());
}
}
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
转载原文:https://blog.csdn.net/qq_23901559/article/details/79116558