java注解在 Android 中有两种应用方式,一种方式是基于反射的,在程序的运行期间获取类信息进行反射调用;另一种是使用注解处理,在编译期间生成相关代码,然后在运行期间通过调用这些代码来实现相关功能。 我们先了解一下注解的分类及其关键字
标准注解(java默认提供) | 元注解(用户自己定义用) |
---|---|
@Override | @Target |
@Deprecated | @Retention |
@SuppressWarnnings | @Documented |
@SafeVarags | @Inherited |
@Repeatable |
由表格可以看出java中的注解主要分为两类,标准注解和元注解,。标准注解是 Java 为我们提供的预定义注解,这个我们没多大关系,主要是元注解,元注解是用来提供给用户自定义注解用的,接下来我们来学习一下元注解。
先解释每个注解的含义:
@Target: 注解的作用:
@Retention:注解的保留位置
了解完这些注解的含义,我们来自定义一个,java注解有两种实现方式。
首先我们的目标是制作一个用户信息表,是一个User对象,表中有其对应的属性,将注解和属性等关联,然后再通过反射拿到对应的注解值和属性值打印,思路如下: 1.1. 首先定义两个注解UserAnnotation和UserAttribute,创建一个注解遵循: public @interface 注解名 {方法参数} 1.2. 将注解和User对象关联给用户赋值。 1.3. 获取注解,打印注解的注解值和属性值。 定义注解代码如下
image.png
将注解和User对象绑定
image.png
主要通过获取注解打印
public static void main(String [] args){
UserBean mUserBean =new UserBean();
mUserBean.setUserName("张三");
mUserBean.setUserSex("男");
mUserBean.setUserAge(18);
StringBuffer mStringBuffer= printData(mUserBean);
System.out.println(mStringBuffer);
}
我们主要看一下printData这个打印的代码,都有注释不解释
private static StringBuffer printData(UserBean userBean){
//创建一个StringBuffer对象拼接数据
StringBuffer mStringBuffer =new StringBuffer();
//根据对象获取注解Class
Class annotationClass =userBean.getClass();
//判断对象中有没有我们定义的userAnnotation注解
boolean bUserAnnotation =annotationClass.isAnnotationPresent(CustomeAnnotation.UserAnnotation.class);
if(bUserAnnotation){
//获取userAnnotation注解
CustomeAnnotation.UserAnnotation mUserAnnotation= (CustomeAnnotation.UserAnnotation) annotationClass.getAnnotation(CustomeAnnotation.UserAnnotation.class);
//获取获取userAnnotation注解的值
String userAnnotationName =mUserAnnotation.userClassName();
mStringBuffer.append(userAnnotationName+"信息如下:");
//进而获取对象中的所有属性
//获取对象中的所有属性
Field[] fields = annotationClass.getDeclaredFields();
//便遍历属性
for(Field field : fields){
//判断属性中是否有我们定义的UserAttribute
boolean bUserAttribute =field.isAnnotationPresent(CustomeAnnotation.UserAttribute.class);
if(bUserAttribute){
//获取自定义UserAttribute注解
CustomeAnnotation.UserAttribute userAttribute = field.getAnnotation(CustomeAnnotation.UserAttribute.class);
//获取去userAttribute中的值
String name = userAttribute.userAttribute();
Object value = "";
try {
//获取对应属性的值,toUpperCase()把字符串转换为大写
Method method = annotationClass.getMethod("get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1));
value = method.invoke(userBean);
} catch (Exception e) {
e.printStackTrace();
}
//string类型
if (value instanceof String) {
mStringBuffer.append(name + "=").append(value).append(",");
} else if (value instanceof Integer) {
mStringBuffer.append(name + "=").append(value).append(",");
}
}else{
throw new NullPointerException("UserAttribute not find");
}
}
}else{//如果不存在抛一个异常
throw new NullPointerException("userAnnotation not find");
}
return mStringBuffer;
}
这样的话我们就可以成功打印出信息了,请问明白了没,如果没有明白,没关系我们继续在写一个,类似Butterknife的:
image.png 第二步:
image.png
第三步:
image.png
这个看到过一篇文章比较专业,我就不再这里说了
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有