Java注解(Annotations)是Java SE 5引入的一项重要特性,允许在源代码中添加元数据(metadata)。注解可以分为两类:内置注解(Built-in Annotations)和自定义注解(Custom Annotations)。下面详细介绍这两类注解及其使用方法。
内置注解(Built-in Annotations)
Java内置注解主要有三种:@Override
、@Deprecated
和@SuppressWarnings
。这些注解主要用于提供编译时和运行时的元数据信息。
1. @Override
@Override
注解用于标记重写(override)父类方法的情况。使用此注解可以确保编译器检查方法签名是否正确。
示例
1public class BaseClass {
2 public void someMethod() {
3 System.out.println("BaseClass.someMethod()");
4 }
5}
6
7public class DerivedClass extends BaseClass {
8 @Override
9 public void someMethod() {
10 System.out.println("DerivedClass.someMethod()");
11 }
12}
13
14public class Main {
15 public static void main(String[] args) {
16 DerivedClass derived = new DerivedClass();
17 derived.someMethod(); // 输出 "DerivedClass.someMethod()"
18 }
19}
2. @Deprecated
@Deprecated
注解用于标记已经被废弃(deprecated)的类或方法。使用此注解可以提醒开发者不要使用这些废弃的代码。
示例
1public class Example {
2 @Deprecated
3 public void oldMethod() {
4 System.out.println("This is an old method.");
5 }
6
7 public void newMethod() {
8 System.out.println("This is a new method.");
9 }
10}
11
12public class Main {
13 public static void main(String[] args) {
14 Example example = new Example();
15 example.oldMethod(); // 输出 "This is an old method."
16 example.newMethod(); // 输出 "This is a new method."
17 }
18}
3. @SuppressWarnings
@SuppressWarnings
注解用于抑制编译警告。可以指定多个警告类型,例如"rawtypes"
、"unchecked"
等。
示例
1public class Example {
2 @SuppressWarnings("rawtypes")
3 public void uncheckedMethod() {
4 Object[] array = new Object[10];
5 array[0] = "Hello"; // 不会抛出警告
6 }
7}
8
9public class Main {
10 public static void main(String[] args) {
11 Example example = new Example();
12 example.uncheckedMethod();
13 }
14}
自定义注解(Custom Annotations)
自定义注解是由开发者自己定义的注解,可以包含任意数量的成员变量。自定义注解通常用于特定的应用场景,如框架开发、单元测试等。
创建自定义注解
- 定义注解:使用
@interface
关键字定义注解,并指定成员变量。 - 指定保留策略:使用
@Retention
元注解指定注解的保留策略。 - 指定目标类型:使用
@Target
元注解指定注解的目标类型。
示例
1import java.lang.annotation.ElementType;
2import java.lang.annotation.Retention;
3import java.lang.annotation.RetentionPolicy;
4import java.lang.annotation.Target;
5
6@Retention(RetentionPolicy.RUNTIME)
7@Target(ElementType.METHOD)
8public @interface MyAnnotation {
9 String value() default "";
10 int number() default 0;
11}
使用自定义注解
- 在类、方法或字段上使用自定义注解。
- 处理自定义注解:通过反射获取注解信息并进行处理。
示例
1public class Example {
2 @MyAnnotation(value = "Hello", number = 42)
3 public void myMethod() {
4 // 方法体
5 }
6}
处理自定义注解
- 获取注解信息:使用反射获取类、方法或字段上的注解。
- 处理注解信息:根据注解信息进行相应的处理。
示例
1import java.lang.reflect.Method;
2
3public class AnnotationProcessor {
4 public static void processAnnotations(Object obj) {
5 Class<?> clazz = obj.getClass();
6 Method[] methods = clazz.getDeclaredMethods();
7
8 for (Method method : methods) {
9 if (method.isAnnotationPresent(MyAnnotation.class)) {
10 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
11 String value = annotation.value();
12 int number = annotation.number();
13 System.out.println("Value: " + value + ", Number: " + number);
14 }
15 }
16 }
17}
18
19public class Main {
20 public static void main(String[] args) {
21 Example example = new Example();
22 AnnotationProcessor.processAnnotations(example);
23 }
24}
完整示例代码
1import java.lang.annotation.ElementType;
2import java.lang.annotation.Retention;
3import java.lang.annotation.RetentionPolicy;
4import java.lang.annotation.Target;
5
6@Retention(RetentionPolicy.RUNTIME)
7@Target(ElementType.METHOD)
8public @interface MyAnnotation {
9 String value() default "";
10 int number() default 0;
11}
12
13public class Example {
14 @MyAnnotation(value = "Hello", number = 42)
15 public void myMethod() {
16 // 方法体
17 }
18}
19
20public class AnnotationProcessor {
21 public static void processAnnotations(Object obj) {
22 Class<?> clazz = obj.getClass();
23 Method[] methods = clazz.getDeclaredMethods();
24
25 for (Method method : methods) {
26 if (method.isAnnotationPresent(MyAnnotation.class)) {
27 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
28 String value = annotation.value();
29 int number = annotation.number();
30 System.out.println("Value: " + value + ", Number: " + number);
31 }
32 }
33 }
34}
35
36public class Main {
37 public static void main(String[] args) {
38 Example example = new Example();
39 AnnotationProcessor.processAnnotations(example);
40 }
41}
总结
Java注解是一项重要的特性,允许在源代码中添加元数据。注解可以分为两类:
- 内置注解:如
@Override
、@Deprecated
和@SuppressWarnings
,主要用于提供编译时和运行时的元数据信息。 - 自定义注解:由开发者自己定义的注解,可以包含任意数量的成员变量,用于特定的应用场景。
通过使用内置注解和自定义注解,可以增强代码的功能和灵活性,实现多种用途,如文档生成、编译时检查、代码生成等。掌握Java注解的基本概念和用法后,可以更好地利用注解来编写灵活和动态的应用程序。