java注解
来源:互联网 发布:淘宝的宝贝详情在哪 编辑:程序博客网 时间:2024/06/03 19:24
了解一下java1.5起默认的三个annotation类型:
@Override:只能用在方法之上的,用来告诉别人这一个方法是改写父类的。
@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会 用产生 警告信息,要了解详细信息,请使用 Xlint:deprecation 重新编译。
@SuppressWarnings:这一个类型可以来暂时把一些警告信息消息关闭.@Override的代码简单,用于注解类的方法,在Source级别可用.
@Deprecated 级别为Runtime .
@SuppressWarnings 级别为source,经常的使用方式为@SuppressWarnings("unchecked").
级别
第一个SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
第二个ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS.
第三个,是RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息;相反,@Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated
定义注解
使用元注解定义注解,元注解有四种:@Retention(RetentionPolicy.[policy])
[policy]={SOURCE, CLASS, RUNTIME(反射机制可读取)}
@Target(ElementType.[type])
[type]={METHOD, FIELD, TYPE(类、接口、枚举声明), CONSTRUCTOR, LOCAL_VARIABLE, PARAMETER}
@Documented 表示将此注解包含到Javadoc中
@Inherited 表示允许子类继承父类的注解
使用注解
注意:
注解元素必须有确定的值,要么在定义注解的默认值中指定,要么在使用注解时指定
非基本类型的注解元素的值不可为null
注解快捷方式: 如果注解元素声明为value(),则在使用注解时如果只声明value,可以只写值,不必写名值对。例如可写为@UseCase(10)
编写注解处理器
通过反射机制获取注解元素的值: Method.getAnnotation(), Field.getDeclaredAnnotations()等方法
注解的使用场景
统计系统用例实现情况
由JavaBean自动生成数据库建表SQL
JDK提供的注解工具apt
程序中用到的枚举类
package annotation;enum WeekDay {SUN,MON,TUE,WED,THI,FRI,SAT}
程序中用到的注解类
package annotation;@interface MetaAnnotation {String value();}
MyAnnotation 核心注解实例
package annotation;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.annotation.ElementType;@Retention(RetentionPolicy.RUNTIME)@Target({(ElementType.METHOD),ElementType.TYPE})//@Target((ElementType.METHOD))public @interface MyAnnotation {int value();String color();int id() default 0;int[] arr() default {1,2,3,4,5,6};MetaAnnotation annot() default @MetaAnnotation("好人");WeekDay meiju() default WeekDay.MON; }
应用注解的类
package annotation;//@MyAnnotation1(5)//@MyAnnotation1(color = "red",value=5)//@MyAnnotation1(color = "red",value=5,arr={9,8,7,6})//@MyAnnotation(color = "red",value=5,arr=100)//@MyAnnotation(color = "red",value=5,arr=100,annot=@MetaAnnotation("坏人"))@MyAnnotation(color = "red",value=5,arr=100,annot=@MetaAnnotation("坏人"),meiju=WeekDay.MON)public class AnnotationTest {//@MyAnnotation1@SuppressWarnings("deprecation")public static void main(String[] args) {System.runFinalizersOnExit(true);if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){MyAnnotation annotation = AnnotationTest.class.getAnnotation(MyAnnotation.class);System.out.println(annotation);}}}
package annotation;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.annotation.ElementType;@Retention(RetentionPolicy.RUNTIME)@Target({(ElementType.METHOD),ElementType.TYPE})//@Target((ElementType.METHOD))public @interface MyAnnotation {int value();String color();int id() default 0;int[] arr() default {1,2,3,4,5,6};MetaAnnotation annot() default @MetaAnnotation("好人");WeekDay meiju() default WeekDay.MON; }
- Java注解-自定义注解
- Java注解----自定义注解
- Java注解自定义注解
- Java注解--四种元注解
- Java注解-三种內建注解
- 【Java】【注解】自定义注解
- java注解
- java注解
- java注解
- java 注解
- Java注解
- java 注解
- Java 注解
- Java注解
- java 注解
- JAVA注解
- Java注解
- Java注解
- Codeforces Round 264(div2) C. Gargari and Bishops
- Ubuntu下用glade和GTK+开发C语言界面程序(二)——gcc常用命令介绍
- 外传:电脑网络连接显示正常,但是不能上网?
- TortoiseGit Disconnected: No supported authentication methods available (server sent: publickey)
- 回文字符串
- java注解
- Windows 2008 Server搭建Radius服务器的方法
- 关于更换手机的安全问题
- oracle中 connect by prior 递归算法 -- 理解
- Codeforce 264 B Caisa and Pylons(模拟)
- 题目1389:变态跳台阶
- 《念奴娇·断虹霁雨》 黄庭坚
- LeetCode 42 Balanced Binary Tree
- Codeforces Round #264 (div2)B. Caisa and Pylons