Java注解

Java包含4个元注解,所谓元注解,就是定义自定义注解的注解,我们可以通过他们自定义我们的注解。

下面首先讲解下4个元注解:

@Target

@Target说明了Annotation所修饰的对象范围:Annotation可被用于packagestypes(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

 取值(ElementType)有:

1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型)enum声明

(若定义一个类型为用于描述方法的注解,则该注解只能描述方法)

@Retention

@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotationclass在使用上是被分离的)。使用这个meta-Annotation可以对Annotation生命周期限制。

作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)

 取值(RetentionPoicy)有:

1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)

@Documented:

@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。

@Inherited

@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

  注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation

  当@Inherited annotation类型标注的annotationRetentionRetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。

弄明白以上的元注解,下面我们就可以进行自定义注解的编写:

自定义一个名为:MyFirstAnno的注解(以类型为描述类为例):

package test.hashmap;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({java.lang.annotation.ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyFirstAnno
{

  public abstract String name() default "";
  
  public abstract String version() default "V1.0";
  
  public abstract boolean ismust() default true;  
  
  

}

测试demo:
package test.hashmap;

public class LPTest {
	
	public static void main(String[] args) throws Exception {
		MyFirstAnno anno=LP.class.getAnnotation(MyFirstAnno.class);
		System.out.println(anno.name());
		System.out.println(anno.version());
		System.out.println(anno.ismust());
		
	}
}


@MyFirstAnno(name="LP Class Annotation",ismust=true)
class LP{
	public void annoMethod(){
		System.out.println("this is annoMethod");
	}
	
}


输出结果:

再自定义一个描述字段的注解:

package test.hashmap;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({java.lang.annotation.ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnno {

	public String filedType() default "default type";
	
	public String filedName() default "default name";
}

测试demo:

package test.hashmap;

import java.lang.reflect.Field;

public class LPTest {
	
	public static void main(String[] args) throws Exception {
		/*MyFirstAnno anno=LP.class.getAnnotation(MyFirstAnno.class);
		System.out.println(anno.name());
		System.out.println(anno.version());
		System.out.println(anno.ismust());*/
		
		
		for(Field fields:LP.class.getFields()){
			
			FieldAnno fanno=fields.getAnnotation(FieldAnno.class);
			
			System.out.println(fanno.filedName());
			System.out.println(fanno.filedType());
			
		}
		
		
		
	}
}


@MyFirstAnno(name="LP Class Annotation",ismust=true)
class LP{
	@FieldAnno(filedName="a",filedType="String")
	public String a;
	
	@FieldAnno(filedName="b",filedType="int")
	public int b;
	

	public void annoMethod(){
		System.out.println("this is annoMethod");
	}
	
}

输出结果:


【备注】:在注解类中,申明抽象方法后,需要以default ”“;的形式给定该注解属性的默认值。若不给顶默认值,则在使用注解时,必须将所有的属性赋值。

【总结】:注解可以帮我们很方便的干很多事情。若定义一个描述类的注解,获取注解时可直接用XX.class.getAnnotation(Class<T> annotationClass)的方式去获取对应的注解类。若定义一个描述字段的注解,获取注解时可通过反射获取Field,然后通过Field.getAnnotation(Class<T> annotationClass)的方式获取。方法等以此类推。

【声明】:我对java注解也是略知一二,谈不上精通。写博客最主要的目的也是为了记录自己学习的足迹。文中若有不足之处,请不吝斧正!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值