11.解析注解:如何解析注解???解析获取注解有哪九个常用方法???注解在项目中具体怎么应用???

学习:第7遍


1. 如何解析注解???

对于生命周期为运行期间(RetentionPolicy.RUNTIME)的注解,可以通过反射获取元素上的注解,实现特定的功能


2.解析获取注解有哪九个常用方法???

通过反射来获得注解,先得到class对象
Class cls = Student.class;

方法一: Annotation[] annotations = cls.getAnnotations()
作用: 获取所有注解

方法二: Annotation annotation = cls.getAnnotation(MyAnnotation.class)
作用:获MyAnnotation注解,类型是Annotation

方法三: Field field = cls.getDeclaredField(“name”)
annotations = field.getAnnotations()
作用: 获取属性上的注解

方法四:annotation = field.getAnnotation(MyAnnotation.class)
作用:获取属性上的MyAnnotation注解
annotation = field.getAnnotation(MyAnnotation.class);

方法五: Method method = cls.getDeclaredMethod(“show”, int.class);
annotations = method.getAnnotations()
作用:获取方法上的注解

方法六:annotation = method.getAnnotation(MyAnnotation.class)
作用:获取方法上的MyAnnotation注解

方法七:annotation instanceof MyAnnotation
作用:判断annotation是否是MyAnnotation类型

方法八:MyAnnotation ma=(MyAnnotation) annotation
System.out.println(ma.d());
System.out.println(ma.value());
作用: 获取注解上的属性值

方法九:method.isAnnotationPresent(SuppressWarnings.class)
作用: 判断method是否使用了指定的SuppressWarnings注解


3. 注解在项目中具体怎么应用???

通过注解加载属性文件,为类中属性赋值


/*
 * 作用域的取值:
 * 	TYPE 类、接口
 * 	FIELD 属性
 * 	METHOD 方法
 * 	PARAMETER 方法参数
 * 	CONSTRUCTOR 构造方法
 * 	注:可以同时指定多个值,需要将多个值放到大括号中{},以逗号隔开
 */
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
/*
 * 生命周期的取值:
 * 	SOURCE 注解只保留在源文件中,不参与编译,即class文件中没有
 * 	CLASS  注解参与编译,保留在class文件中,但类加载时不加载,即程序运行时没有
 * 	RUNTIME	注解可以保存在程序运行期间,可以通过反射获取该注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnnotation {
	
	/*
	 * 抽象方法,必须是无参无异常
	 */
//	String a();
	
//	int b();
	
	/*
	 * 返回数组类型时,使用大括号{}
	 */
//	String[] c();
	
	/*
	 * 使用default关键字为方法指定默认的返回值,在使用注解时可以不指定该属性
	 */
	String d() default "ddd";
	
	/*
	 * 推荐使用value()名称,在赋值时如果只为该属性赋值,则可以省略value=
	 */
	String value() default "defaultValue";
	
}
package annotation;

@MyAnnotation
@Deprecated
public class Student {

	// @MyAnnotation(a = "aaa", b = 6, c = { "c1", "c2", "c3" })
	// 当数组中只有一个元素时,可以省略大括号
	// @MyAnnotation(a = "aaa", b = 6, c = "c1")
	// 当没有属性要赋值时,可以省略小括号
	// @MyAnnotation
	// 当只为value属性赋值时,可以省略value=,即如果省略属性名,表示为value属性赋值
	// @MyAnnotation("defaultValue")
	// 如果同时为多个属性赋值,则不能省略value=
	@MyAnnotation(value = "welcome", d = "d1")
	private String name;

	@MyAnnotation("hello")
	public void show(int age) {

	}

}
package annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/*
 * 解析注解
 */
public class Test {
	public static void main(String[] args) throws Exception {
		//通过反射来获得注解,先得到class对象
		Class cls = Student.class;
		
		//方法:	Annotation[] annotations = cls.getAnnotations()
		//作用: 获取所有注解
		Annotation[] annotations = cls.getAnnotations(); 
		//方法:	Annotation annotation = cls.getAnnotation(MyAnnotation.class)
		//作用:获MyAnnotation注解,类型是Annotation
		Annotation annotation = cls.getAnnotation(MyAnnotation.class);
		
		// 获取属性上的注解
		//方法:	Field field = cls.getDeclaredField("name")
		//annotations = field.getAnnotations()
		Field field = cls.getDeclaredField("name");
		annotations = field.getAnnotations();
		
		//方法:annotation = field.getAnnotation(MyAnnotation.class)
		//作用:获取属性上的MyAnnotation注解
		annotation = field.getAnnotation(MyAnnotation.class);
		
		//方法:	Method method = cls.getDeclaredMethod("show", int.class);
		//annotations = method.getAnnotations()
		//作用:获取方法上的注解
		Method method = cls.getDeclaredMethod("show", int.class);
		annotations = method.getAnnotations();
		
		//方法:annotation = method.getAnnotation(MyAnnotation.class)
		//作用:获取方法上的MyAnnotation注解
		annotation = method.getAnnotation(MyAnnotation.class);
		
		for (Annotation a : annotations) {
			System.out.println(a);
		}
		System.out.println(annotation);
		
		
		//方法:annotation instanceof MyAnnotation
		//作用:判断annotation是否是MyAnnotation类型
		if(annotation instanceof MyAnnotation){
		//	方法:MyAnnotation ma=(MyAnnotation) annotation
	    //		System.out.println(ma.d());
		//	System.out.println(ma.value());
		//作用:	获取注解上的属性值
			MyAnnotation ma=(MyAnnotation) annotation;
			System.out.println(ma.d());
			System.out.println(ma.value());
		}
		
		//方法:method.isAnnotationPresent(SuppressWarnings.class)
		//作用: 判断method是否使用了指定的SuppressWarnings注解
		System.out.println(method.isAnnotationPresent(SuppressWarnings.class));
	}
}