bianguai

JAVA注解示例详解[转]

  1 注解(Annotation) 为我们在代码中天界信息提供了一种形式化的方法,是我们可以在稍后某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据)。
2
3 注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种注解,定义在java.lang包中。
4
5 @Override 表示当前方法是覆盖父类的方法。
6
7 @Deprecated 表示当前元素是不赞成使用的。
8
9 @SuppressWarnings 表示关闭一些不当的编译器警告信息。
10
11 下面是一个定义注解的实例
12
13 Java代码
14 package Test_annotation;
15
16 import java.lang.annotation.Documented;
17 import java.lang.annotation.Inherited;
18 import java.lang.annotation.Retention;
19 import java.lang.annotation.Target;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.RetentionPolicy;
22
23 /*
24 * 元注解@Target,@Retention,@Documented,@Inherited
25 *
26 * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
27 * ElemenetType.CONSTRUCTOR 构造器声明
28 * ElemenetType.FIELD 域声明(包括 enum 实例)
29 * ElemenetType.LOCAL_VARIABLE 局部变量声明
30 * ElemenetType.METHOD 方法声明
31 * ElemenetType.PACKAGE 包声明
32 * ElemenetType.PARAMETER 参数声明
33 * ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
34 *
35 * @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
36 * RetentionPolicy.SOURCE 注解将被编译器丢弃
37 * RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
38 * RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
39 *
40 * @Documented 将此注解包含在 javadoc 中
41 *
42 * @Inherited 允许子类继承父类中的注解
43 *
44 */
45 @Target(ElementType.METHOD)
46 @Retention(RetentionPolicy.RUNTIME)
47 @Documented
48 @Inherited
49 /*
50 * 定义注解 Test
51 * 注解中含有两个元素 id 和 description
52 * description 元素 有默认值 "no description"
53 */
54 public @interface Test {
55 public int id();
56 public String description() default "no description";
57 }
58
59 package Test_annotation;
60
61 import java.lang.annotation.Documented;
62 import java.lang.annotation.Inherited;
63 import java.lang.annotation.Retention;
64 import java.lang.annotation.Target;
65 import java.lang.annotation.ElementType;
66 import java.lang.annotation.RetentionPolicy;
67
68 /*
69 * 元注解@Target,@Retention,@Documented,@Inherited
70 *
71 * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
72 * ElemenetType.CONSTRUCTOR 构造器声明
73 * ElemenetType.FIELD 域声明(包括 enum 实例)
74 * ElemenetType.LOCAL_VARIABLE 局部变量声明
75 * ElemenetType.METHOD 方法声明
76 * ElemenetType.PACKAGE 包声明
77 * ElemenetType.PARAMETER 参数声明
78 * ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
79 *
80 * @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
81 * RetentionPolicy.SOURCE 注解将被编译器丢弃
82 * RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
83 * RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
84 *
85 * @Documented 将此注解包含在 javadoc 中
86 *
87 * @Inherited 允许子类继承父类中的注解
88 *
89 */
90 @Target(ElementType.METHOD)
91 @Retention(RetentionPolicy.RUNTIME)
92 @Documented
93 @Inherited
94 /*
95 * 定义注解 Test
96 * 注解中含有两个元素 id 和 description
97 * description 元素 有默认值 "no description"
98 */
99 public @interface Test {
100 public int id();
101 public String description() default "no description";
102 }
103
104 下面是一个使用注解 和 解析注解的实例
105
106 Java代码
107 package Test_annotation;
108
109 import java.lang.reflect.Method;
110
111 public class Test_1 {
112 /*
113 * 被注解的三个方法
114 */
115 @Test(id = 1, description = "hello method_1")
116 public void method_1() {
117 }
118
119 @Test(id = 2)
120 public void method_2() {
121 }
122
123 @Test(id = 3, description = "last method")
124 public void method_3() {
125 }
126
127 /*
128 * 解析注解,将Test_1类 所有被注解方法 的信息打印出来
129 */
130 public static void main(String[] args) {
131 Method[] methods = Test_1.class.getDeclaredMethods();
132 for (Method method : methods) {
133 /*
134 * 判断方法中是否有指定注解类型的注解
135 */
136 boolean hasAnnotation = method.isAnnotationPresent(Test.class);
137 if (hasAnnotation) {
138 /*
139 * 根据注解类型返回方法的指定类型注解
140 */
141 Test annotation = method.getAnnotation(Test.class);
142 System.out.println("Test( method = " + method.getName()
143 + " , id = " + annotation.id() + " , description = "
144 + annotation.description() + " )");
145 }
146 }
147 }
148
149 }
150
151 package Test_annotation;
152
153 import java.lang.reflect.Method;
154
155 public class Test_1 {
156 /*
157 * 被注解的三个方法
158 */
159 @Test(id = 1, description = "hello method_1")
160 public void method_1() {
161 }
162
163 @Test(id = 2)
164 public void method_2() {
165 }
166
167 @Test(id = 3, description = "last method")
168 public void method_3() {
169 }
170
171 /*
172 * 解析注解,将Test_1类 所有被注解方法 的信息打印出来
173 */
174 public static void main(String[] args) {
175 Method[] methods = Test_1.class.getDeclaredMethods();
176 for (Method method : methods) {
177 /*
178 * 判断方法中是否有指定注解类型的注解
179 */
180 boolean hasAnnotation = method.isAnnotationPresent(Test.class);
181 if (hasAnnotation) {
182 /*
183 * 根据注解类型返回方法的指定类型注解
184 */
185 Test annotation = method.getAnnotation(Test.class);
186 System.out.println("Test( method = " + method.getName()
187 + " , id = " + annotation.id() + " , description = "
188 + annotation.description() + " )");
189 }
190 }
191 }
192
193 }
194
195 输出结果如下:
196
197 Test( method = method_1 , id = 1 , description = hello method_1 )
198 Test( method = method_2 , id = 2 , description = no description )
199 Test( method = method_3 , id = 3 , description = last method )

分类:

技术点:

相关文章: