1 注释
注释的分类
单行注释: //
多行注释: /* */
文档注释:/** */
作用:传递额外的信息,给程序员看,不参与编译
2 注释
Annotation是代码里边的特殊的标记,可以在编译、类加载、运行的时候被读取
,并执行相应的处理。
2.1 注释与注解的不同
2.2 自定义注解
package com.yq.day72;
public @interface MyAnno {
//属性类型的范围 基本的数据类型 string class 枚举类型 注解类型
// 以及以上类型的数组
int age();
double price();
String name();
String[] names();
Class c();
MyAnno3 anno();
// 注解不能继承
}
@interface MyAnno3 {
}
注解不能被继承
2.3 注解的使用
实例化对象不是通过new,而是 属性名=属性值
使用 的时候 给每个属性完成赋值
@注解名(属性名1=value,属性名2=value,....)
注意:
1.每个属性都要赋值
2.有默认值可以不用
3.只有一个属性,并且为value,可以简化赋值
4.数组的复制使用{}
5.引用类型的值不能是null
2.4 注解处理器
结合反射去应用
使用的具体的步骤
1.通过反射获取注册的信息
2.获取文件字节码对象
3.获取对象的方法,判断上面的方法是否使用了注解
4.使用了获取注解的实例
5.获取属性值,进行操作
使用的方法:属于类Class
boolean isAnnotation() | 如果此 Class 对象表示一个注释类型则返回 true。 |
---|---|
A getAnnotation(Class annotationClass) | 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。 |
代码实例:
package com.yq.day72;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/**
* @author 3307235355@qq.com
* @description
* @since 2022/07/02 11:41
* 创建一个学会对象,不使用new的方式,使用反射和注解实现
* name的长度在5之间 ,年龄在18-25之间
*/
//测试类
public class Demo04 {
//定义一个字节码的文件对象,进行赋值
static Class<?> aClass;
static {
try {
aClass = Class.forName("com.yq.day72.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, InstantiationException, IllegalAccessException {
Student zhansa = getInstance("zga", 293);
System.out.println(zhansa);
}
public static Student getInstance(String name,int age) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
//名字的校验 获取student的属性name
Field name1 = aClass.getDeclaredField("name");
//判断是否使用注解 判断属性name上是否有注解
boolean a1 = name1.isAnnotationPresent(NameLimit.class);
if (a1){
//获取注解的实例
NameLimit a2 = name1.getAnnotation(NameLimit.class);
//获取注解的属性值
int length = a2.length();
//判断
if (name.length()>length){
throw new IllegalArgumentException("长度大");
}
}
//对年龄的校验 获取age的属性
Field age1 = aClass.getDeclaredField("age");
//判断是否有注解
boolean annotationPresent = age1.isAnnotationPresent(AgeLimit.class);
if (annotationPresent){
//获取注解的实例
AgeLimit annotation = age1.getAnnotation(AgeLimit.class);
//获取属性值
int i = annotation.maxAge();
int i1 = annotation.minAge();
if (age>i || age <i1){
throw new IllegalArgumentException("年龄不合法!!!!!");
}
}
//获取构造方法
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class);
//忽略限制
declaredConstructor.setAccessible(true);
//创建学生对象
Student o = (Student) declaredConstructor.newInstance(name, age);
return o;
}
}
class Student{
//注解
@NameLimit
String name;
//注解
@AgeLimit
int age;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
private Student(String name, int age) {
this.name = name;
this.age = age;
}
}
//元注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface NameLimit{
int length() default 5;
}
//元注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface AgeLimit{
int maxAge() default 25;
int minAge() default 18;
}
2.5 元注解
描述注解的注解,元数据 meta data
常用的元注解
**@Retention元注解,来定义我们自己定义的注解的保留级别.**
- RetentionPolicy.RUNTIME
- RetentionPolicy.CLASS 默认
- RetentionPolicy.SOURCE
**@Target元注解,注解可以作用的目标**
对于注解而言,可以作用的目标:
1. 整个类 ElementType.TYPE
2. 成员变量 ElementType.FIELD
3. 构造方法 ElementType.CONSTRUCTOR
4. 成员方法 ElementType.METHOD