反射

反射类使用范围。
@Target(ElementType.TYPE) //接口、类、枚举
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包

@Retention:注解的保留位置         
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含(一般用于代码检查 比如:@Override 和 @SuppressWarnings,)

@Retention(RetentionPolicy.CLASS) //注解被保留到class文件,但jvm加载class文件时候被遗弃,这也是默认的生命周期;(预处理操作,比如生成一些辅助代码(如 ButterKnife))

@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到(这个比较常用,会一直存在在代码中)

生命周期长度 SOURCE < CLASS < RUNTIME

@Document:说明该注解将被包含在javadoc中

@Inherited:子类会继承父类使用的注解中被@Inherited修饰,但是注意了(仅针对 类,属性和方法无效

demo图下:

@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedNo {
	String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface InheritedYes {
	String value();
}
@InheritedYes("我是带Inherited的注解")
@InheritedNo("我不带Inherited的注解")
public class Parent {
	@InheritedYes("我是带 Inherited的注解")
	@InheritedNo("我不带 Inherited的注解")
	public String getName(){
		return name;
	}
	@InheritedYes("我是带 Inherited的注解")
	@InheritedNo("我不带Inherited的注解")
	public String name;
}
public class Child extends Parent {
	public String name;
	@Override
	public String getName() {
		return name;
	}
}
public class test {
 
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
		Class<Child> clazz = Child.class;
		//对类进行测试 
		System.out.println("==========带 @Inherited 注解的class 会被打印 ===============");
		if(clazz.isAnnotationPresent(InheritedYes.class)){
			System.out.println(clazz.getAnnotation(InheritedYes.class).value());
		}
		if(clazz.isAnnotationPresent(InheritedNo.class)){
			System.out.println(clazz.getAnnotation(InheritedNo.class).value());
		}
		//对子类的方法进行测试
		System.out.println("=========@Inherited 对 方法无效 所以都不会打印 =============");
		Method method = clazz.getMethod("getName", null);
		if(method.isAnnotationPresent(InheritedYes.class)){
			System.out.println(method.getAnnotation(InheritedYes.class).value());
		}
		if(method.isAnnotationPresent(InheritedNo.class)){
			System.out.println(method.getAnnotation(InheritedNo.class).value());
		}
		//对属性测试
		System.out.println("=============@Inherited 对 Field无效 所以都不会打印==============");
		Field field = clazz.getField("name");
		if(field.isAnnotationPresent(InheritedYes.class)){
			System.out.println(field.getAnnotation(InheritedYes.class).value());
		}
		if(field.isAnnotationPresent(InheritedNo.class)){
			System.out.println(field.getAnnotation(InheritedNo.class).value());
		}
	}
}

打印结果:

==========带 @Inherited 注解的class 会被打印 ===============
我是带Inherited的注解
=========@Inherited 对 方法无效 所以都不会打印 =============
=============@Inherited 对 Field无效 所以都不会打印==============
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值