• java入坑之注解


    一、注解入门 

    注解:Annotation

    • 从JDK 1.5 引入
    • 位于源码中(代码/注释/注解),使用其他工具进行处理的标签
    • 注解用来修饰程序的元素,但不会对被修饰的对象有直接的影响
    • 只有通过某种配套的工具才会对注解信息进行访问和处理

    主要用途

    • 提供信息给编译器/IDE工具
    • 可用于其他工具来产生额外的代码/配置文件等
    • 有一些注解可在程序运行时访问,增加程序的动态性

     1.1常见普通注解

    1.2常见元注解

     1.3自定义注解

    二、Java预定义的普通注解

    Java预定义的普通注解是一些用于修饰类、方法、字段等元素的注解,它们可以提供一些编译器或运行时的信息,例如检查重写方法是否正确、标记过时的元素、忽略警告信息等。Java预定义的普通注解有以下几种:

    • @Override:用于修饰方法,表示该方法重写了父类或接口中的方法,如果没有匹配的父类或接口方法,编译器会报错。
    • @Deprecated:用于修饰类、方法、字段等元素,表示该元素已经过时,不建议使用,如果使用该元素,编译器会发出警告。
    • @SuppressWarnings:用于修饰类、方法、字段等元素,表示忽略指定的编译器警告,可以指定一个或多个警告类型。
    • @SafeVarargs:用于修饰可变参数的方法或构造器,表示该方法或构造器不会对泛型参数进行不安全的操作,可以忽略堆污染警告。
    • @FunctionalInterface:用于修饰函数式接口,表示该接口只有一个抽象方法,可以用lambda表达式实现。

     2.1@Override

    @Override注解是一种用于表示方法重写的注解,它可以让编译器检查子类的方法是否正确地覆盖了父类或接口中的方法,如果没有匹配的父类或接口方法,编译器会报错。@Override注解可以提高代码的可读性和可维护性,也可以避免一些潜在的错误。

    在子类中重写父类的方法,使用@Override注解标明

    1. class Animal {
    2. public void eat() {
    3. System.out.println("Animal eats");
    4. }
    5. }
    6. class Dog extends Animal {
    7. @Override // 表示重写父类的eat方法
    8. public void eat() {
    9. System.out.println("Dog eats");
    10. }
    11. }

     在实现接口的类中重写接口的方法,使用@Override注解标明

    1. interface Shape {
    2. public void draw();
    3. }
    4. class Circle implements Shape {
    5. @Override // 表示重写接口的draw方法
    6. public void draw() {
    7. System.out.println("Draw a circle");
    8. }
    9. }

    2.2@Deprecated

    @Deprecated注解是一种用于标记已经过时的类、方法、字段等元素的注解,它可以让编译器在使用该元素时发出警告,提醒开发者不要再使用该元素,而是使用新的替代方案

    1. package org.example;
    2. class Animal {
    3. @Deprecated
    4. public void makeSound() {
    5. System.out.println("This method is deprecated.");
    6. }
    7. public void communicate() {
    8. System.out.println("Animals communicate in various ways.");
    9. }
    10. }
    11. public class Test {
    12. public static void main(String[] args) {
    13. Animal animal = new Animal();
    14. // 使用已过时的方法,会生成警告
    15. animal.makeSound();
    16. // 使用新方法
    17. animal.communicate();
    18. }
    19. }

    2.3@SuppressWarnings

    @SuppressWarnings注解是一种用于忽略指定的编译器警告的注解,它可以用于修饰类、方法、字段等元素,可以指定一个或多个警告类型。

    1. 压制各种不同类型的警告信息,使得编译器不显示警告
    2. 各种不同类型是叠加,如修饰类的警告类型,和修饰方法的警告类型,对于方法来说,是叠加的。
    3. 警告类型名称是编译器/IDE工具自己定的,Java规范没有强制要求哪些名称。编译器厂商需要自行协商,保证同名警告类型在各个编译器.上一样工作。

    1. package org.example;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. public class SuppressWarningsExample {
    5. @SuppressWarnings("unchecked")
    6. public static void main(String[] args) {
    7. // 创建一个泛型列表,不指定泛型类型
    8. List myList = new ArrayList();
    9. myList.add("Item 1");
    10. myList.add("Item 2");
    11. // 使用 @SuppressWarnings 抑制未经检查的警告
    12. List stringList = myList;
    13. // 不会再生成未经检查的警告
    14. System.out.println(stringList.get(0));
    15. System.out.println(stringList.get(1));
    16. }
    17. }

     2.4@SafeVarargs

    @SafeVarargs是一个用于抑制编译器警告的注解,它可以用于修饰使用可变参数(varargs)的方法或构造器,表示该方法或构造器不会对其可变参数执行潜在的不安全的操作,例如将其转换为不同的类型或返回给外部。@SafeVarargs注解可以提高代码的可读性和可维护性,也可以避免一些潜在的错误。

    @SafeVarargs注解在Java 7中引入,最初只能用于修饰final或static的方法或构造器,因为这些方法或构造器不能被重写,从而保证了安全性。但是在Java 9中,@SafeVarargs注解也可以用于修饰private的实例方法,因为这些方法也不能被重写。

    2.5@FunctionalInterface

    @FunctionalInterface是一个用于标记函数式接口的注解,它表示该接口只有一个抽象方法,可以用lambda表达式或方法引用来实现。函数式接口是Java 8引入的一种新特性,它可以让代码更简洁、清晰和优雅。函数式接口的一些例子有Runnable、ActionListener、Comparable等。@FunctionalInterface注解可以让编译器检查接口是否符合函数式接口的定义,如果不符合,编译器会报错。@FunctionalInterface注解也可以提高代码的可读性,明确表达接口的用途。 

    1. package org.example;
    2. @FunctionalInterface // 表示这是一个函数式接口
    3. interface Adder {
    4. int add(int a, int b); // 定义一个抽象方法add
    5. }
    6. public class Test {
    7. public static void main(String[] args) {
    8. Adder adder = (a, b) -> a + b; // 使用lambda表达式创建Adder接口的实例
    9. System.out.println(adder.add(10, 20)); // 调用add方法,输出30
    10. }
    11. }

    三、自定义注解

     3.1注解可以包括的类型

     3.2使用步骤

    自定义注解的基本流程是:第一步,定义注解,使用@interface语法,并指定元注解(Annotation的注解)来配置注解的作用域和策略;第二步,使用注解,把注解标记在需要用到的程序代码中;第三步,解析注解,在编译期或运行时检测到注解,并进行特殊操作

     3.3特殊

    • 注解可以使用元注解来指定其作用域和策略,例如@Target指定注解可以用在哪些元素上,@Retention指定注解在什么时候有效,@Documented指定注解是否包含在用户文档中,@Inherited指定注解是否可以被子类继承,@Repeatable指定注解是否可以在同一个元素上重复使用,例如:
    1. @Target(ElementType.METHOD) // 表示该注解只能用于方法上
    2. @Retention(RetentionPolicy.RUNTIME) // 表示该注解在运行时有效
    3. @Documented // 表示该注解会被javadoc工具记录
    4. @Inherited // 表示该注解可以被子类继承
    5. @Repeatable(MyAnnotations.class) // 表示该注解可以重复使用,需要指定一个容器注解来存放重复的注解
    6. public @interface MyAnnotation {
    7. // 注解内容
    8. }

     3.4相关代码

    注解代码

    1. package org.example;
    2. import java.lang.annotation.*;
    3. @Retention(RetentionPolicy.RUNTIME)
    4. //表示该注解会保留在class文件中
    5. @Target(ElementType.METHOD)
    6. //表示该注解只能用于方法
    7. public @interface MultipleTest {
    8. int a() default 0;
    9. int b() default 0;
    10. }

    测试代码

    1. package org.example;
    2. public class Foo {
    3. @MultipleTest(a=1,b=1)
    4. public static void m1(int a, int b) {
    5. if (a + b < 0) {
    6. throw new RuntimeException("Crash");
    7. }
    8. }
    9. @MultipleTest
    10. public static void m2(int a, int b) {
    11. //全部采用默认值
    12. if (a + b < 0) {
    13. throw new RuntimeException("Broken");
    14. }
    15. }
    16. @MultipleTest(b=-2,a=1)
    17. public static void m3(int a, int b) {
    18. //全部采用默认值
    19. if (a + b < 0) {
    20. throw new RuntimeException("Broken");
    21. }
    22. }
    23. }

    解析代码

    1. package org.example;
    2. import java.lang.reflect.Method;
    3. public class tes {
    4. public static void main(String[] args) throws Exception {
    5. int passed = 0, failed = 0; // 给passed变量赋值为0
    6. String className = "org.example.Foo";
    7. for (Method m : Class.forName(className).getMethods()) {
    8. if (m.isAnnotationPresent(MultipleTest.class)) {
    9. System.out.println(m.getName());
    10. MultipleTest st = m.getAnnotation(MultipleTest.class);
    11. try {
    12. m.invoke(null,st.a(),st.b());
    13. passed++;
    14. } catch (Throwable ex) {
    15. System.out.printf("Test %s failed: %s %n", m, ex.getCause()); // 使用%符号代替$符号
    16. failed++;
    17. }
    18. }
    19. }
    20. System.out.printf("passed:%d , failed:%d",passed,failed);
    21. }
    22. }

    四、 Java预定义的元注解

    元注解是一种用于给其他注解进行说明的注解,它可以用来指定注解的作用域、保留策略、文档化、继承性和重复性等特性

    4.1@retention 

    @Retention是一个用于指定注解的保留策略的元注解,它表示注解在什么时候有效,例如源码、字节码或运行时。它需要一个RetentionPolicy类型的参数,只能是一个

    @Retention的参数有以下三种取值:

    1. RetentionPolicy.SOURCE:表示注解只在源码中有效,编译器会丢弃它,不会写入字节码文件。
    2. RetentionPolicy.CLASS:表示注解在源码和字节码中都有效,编译器会把它写入字节码文件,但是虚拟机不会读取它,这是默认值
    3. RetentionPolicy.RUNTIME:表示注解在源码、字节码和运行时都有效,编译器会把它写入字节码文件,并且虚拟机会读取它,可以通过反射机制获取它
    1. package org.example;
    2. import java.lang.annotation.*;
    3. @Retention(RetentionPolicy.RUNTIME) // 指定注解在运行时保留
    4. public @interface AnimalInfo {
    5. String species() default "Unknown"; // 动物的种类属性,默认值为"Unknown"
    6. int age() default 0; // 动物的年龄属性,默认值为0
    7. }
    1. package org.example;
    2. @AnimalInfo(species = "Generic Animal", age = 1) // 使用注解为这个动物类提供信息
    3. public class Animal {
    4. private String name;
    5. public Animal(String name) {
    6. this.name = name;
    7. }
    8. public String getName() {
    9. return name;
    10. }
    11. }
    1. import java.lang.annotation.Annotation;
    2. import java.lang.reflect.Method;
    3. public class AnimalInfoExample {
    4. public static void main(String[] args) {
    5. // 获取Animal类的Class对象
    6. Class animalClass = Animal.class;
    7. // 获取Animal类上的所有注解
    8. Annotation[] annotations = animalClass.getAnnotations();
    9. for (Annotation annotation : annotations) {
    10. if (annotation instanceof AnimalInfo) {
    11. AnimalInfo animalInfo = (AnimalInfo) annotation;
    12. String species = animalInfo.species();
    13. int age = animalInfo.age();
    14. System.out.println("Species: " + species);
    15. System.out.println("Age: " + age);
    16. }
    17. }
    18. }
    19. }

    4.2 @Target

    @Target是一个用于指定注解可以用在哪些元素上的元注解,它表示注解的作用范围,例如类、方法、字段、参数等。它需要一个ElementType类型的参数,可以是一个或多个

    @Target的参数有以下几种取值:

    • ElementType.TYPE:表示注解可以用于类、接口、枚举或注解类型。
    • ElementType.FIELD:表示注解可以用于字段或枚举常量。
    • ElementType.METHOD:表示注解可以用于方法。
    • ElementType.PARAMETER:表示注解可以用于方法参数。
    • ElementType.CONSTRUCTOR:表示注解可以用于构造器。
    • ElementType.LOCAL_VARIABLE:表示注解可以用于局部变量。
    • ElementType.ANNOTATION_TYPE:表示注解可以用于注解类型。
    • ElementType.PACKAGE:表示注解可以用于包声明。
    • ElementType.TYPE_PARAMETER:表示注解可以用于类型参数,这是Java 8新增的特性。
    • ElementType.TYPE_USE:表示注解可以用于任何类型的使用,这也是Java 8新增的特性
    1. import java.lang.annotation.*;
    2. @Target({ElementType.TYPE, ElementType.METHOD}) // 指定注解可以应用于类和方法
    3. @Retention(RetentionPolicy.RUNTIME) // 指定注解在运行时保留
    4. public @interface AnimalInfo {
    5. String species() default "Unknown"; // 动物的种类属性,默认值为"Unknown"
    6. int age() default 0; // 动物的年龄属性,默认值为0
    7. }

    4.3@Inherited

    @Inherited是一个用于指明父类注解会被子类继承得到的元注解,它表示该注解可以被子类继承。它没有参数。

    @Inherited的作用是让自定义的注解具有继承性,即如果一个类使用了某个注解,那么它的子类也会自动拥有该注解,而不需要再次标注。这样可以简化代码,避免重复的注解。@Inherited只对类注解有效,对于方法和属性注解无效

    1. import java.lang.annotation.*;
    2. @Inherited // 指示该注解可以被继承
    3. @Retention(RetentionPolicy.RUNTIME)
    4. @Target(ElementType.TYPE)
    5. public @interface CustomAnnotation {
    6. String value() default "Default Value";
    7. }
    1. @CustomAnnotation(value = "Annotated Parent Class")
    2. public class Animal {
    3. private String name;
    4. public Animal(String name) {
    5. this.name = name;
    6. }
    7. public String getName() {
    8. return name;
    9. }
    10. }
    1. public class Cat extends Animal {
    2. public Cat(String name) {
    3. super(name);
    4. }
    5. }
    1. import java.lang.annotation.Annotation;
    2. public class InheritedAnnotationExample {
    3. public static void main(String[] args) {
    4. // 获取 Cat 类的 Class 对象
    5. Class catClass = Cat.class;
    6. // 获取 Cat 类上的所有注解,包括继承的注解
    7. Annotation[] annotations = catClass.getAnnotations();
    8. for (Annotation annotation : annotations) {
    9. if (annotation instanceof CustomAnnotation) {
    10. CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
    11. String value = customAnnotation.value();
    12. System.out.println("Annotation Value: " + value);
    13. }
    14. }
    15. }
    16. }

    4.4@Documented

    @Documented是一个用于将注解包含在javadoc中的元注解,它表示该注解会被javadoc工具记录。它没有参数。

    @Documented的作用是让自定义的注解在生成文档时能够显示出来,而不是被忽略。这样可以提高代码的可读性和可维护性,也可以让其他人更容易理解注解的含义和用法。@Documented只对类注解有效,对于方法和属性注解无效

    1. import java.lang.annotation.*;
    2. @Documented // 指示编译工具生成文档时包括此注解信息
    3. @Retention(RetentionPolicy.RUNTIME)
    4. @Target(ElementType.TYPE)
    5. public @interface AnimalInfo {
    6. String species() default "Unknown"; // 动物的种类属性,默认值为"Unknown"
    7. int age() default 0; // 动物的年龄属性,默认值为0
    8. }

    4.5@Repeatable

    @Repeatable是一个用于表示注解可以在同一个元素上重复使用的元注解,它需要指定一个容器注解来存放重复的注解。@Repeatable注解可以让代码更简洁和清晰,也可以避免一些潜在的错误。@Repeatable注解在Java 8中引入,最初只能用于修饰final或static的方法或构造器,但是在Java 9中,@Repeatable注解也可以用于修饰private的实例方法

    1. package org.example;
    2. import java.lang.annotation.*;
    3. // 自定义注解 @RepeatableAnnotation
    4. @Retention(RetentionPolicy.RUNTIME)
    5. @Target(ElementType.METHOD)
    6. @Repeatable(RepeatableAnnotations.class) // 指示该注解可以多次应用于同一目标元素,RepeatableAnnotations.class 是容器注解
    7. public @interface RepeatableAnnotation {
    8. int a() default 0;
    9. int b() default 0;
    10. int c() default 0;
    11. }
    1. package org.example;
    2. import java.lang.annotation.ElementType;
    3. import java.lang.annotation.Retention;
    4. import java.lang.annotation.RetentionPolicy;
    5. import java.lang.annotation.Target;
    6. // 容器注解 @RepeatableAnnotations
    7. @Retention(RetentionPolicy.RUNTIME)
    8. @Target(ElementType.METHOD)
    9. public @interface RepeatableAnnotations {
    10. RepeatableAnnotation[] value(); // 一个数组属性,用于存储多个 @RepeatableAnnotation 注解
    11. }
    1. package org.example;
    2. import java.lang.annotation.*;
    3. // 示例类 Student
    4. public class Student {
    5. // 使用 @RepeatableAnnotation 注解并检查条件
    6. @RepeatableAnnotation(a = 2, b = 3, c = 5) // 第一个 @RepeatableAnnotation 注解
    7. @RepeatableAnnotation(a = 4, b = 8, c = 11) // 第二个 @RepeatableAnnotation 注解
    8. public static void add(int a, int b, int c) {
    9. if (c != a + b) {
    10. throw new ArithmeticException("Wrong");
    11. }
    12. System.out.println("Sum: " + c);
    13. }
    14. public static void main(String[] args) throws Exception {
    15. String className = "org.example.Student";
    16. for (java.lang.reflect.Method m : Class.forName(className).getMethods()) {
    17. if (m.isAnnotationPresent(RepeatableAnnotations.class)) {
    18. RepeatableAnnotation[] annos = m.getAnnotationsByType(RepeatableAnnotation.class);
    19. for (RepeatableAnnotation anno : annos) {
    20. System.out.println("a: " + anno.a() + ", b: " + anno.b() + ", c: " + anno.c());
    21. try {
    22. m.invoke(null, anno.a(), anno.b(), anno.c());
    23. } catch (Throwable ex) {
    24. System.out.printf("Test %s failed: %s%n", m, ex.getCause().getMessage());
    25. }
    26. }
    27. }
    28. }
    29. }
    30. }

    五、注解的解析 

    解的解析是指通过反射机制获取程序元素上的注解信息,并根据注解的属性值进行相应的处理。

    Java注解的解析

    • RetentionPolicy.RUNTIME注解采用反射进行解析
    • RetentionPolicy.CLASS注解采用专用的字节码工具进行解析
    • RetentionPolicy.SOURCE注解采用注解处理器进行解析

    注解处理器继承AbstractProcessor,重写process方法
    javac  -processor  Processor1, Processor2, …   sourceJavaFile

     六、RUNTIME注解的实现本质

    6.1步骤

    为了获取和处理RUNTIME注解,我们需要以下几个步骤:

    1. 首先,我们需要使用Class类的forName方法或者对象的getClass方法来获取目标元素所属的Class对象。
    2. 然后,我们需要使用Class对象的getMethods、getFields、getConstructors等方法来获取目标元素所对应的Method、Field、Constructor等对象。
    3. 接着,我们需要使用Method、Field、Constructor等对象的isAnnotationPresent方法来判断目标元素上是否存在指定的RUNTIME注解。
    4. 最后,我们需要使用Method、Field、Constructor等对象的getAnnotation方法或者getAnnotationsByType方法来获取目标元素上的RUNTIME注解实例,并读取注解的属性值,然后进行相应的处理。

    6.2增加代理类导出设置

    RUNTIME注解代理类导出设置的具体方法有以下几种:

    • 在程序中使用System.setProperty方法来设置系统属性,例如:
    System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true"); // 设置保存代理类
    
    • 在命令行中使用-D参数来设置系统属性,例如:
    java -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true Main // 设置保存代理类

    如果RUNTIME注解被JVM加载进来,Java可以用反射获取到注解内容

    1. 程序自动为注解产生一个代理类,来拦截注解的方法访问
    2. 代理类有一个AnnotationInvocationHandler成员变量,其内部存放所有的注解的赋值

     七、 注解的应用

    7.1Servlet配置

    7.2Junit

     7.3Spring&&Spring Boot

    7.4Lombok 

  • 相关阅读:
    opencv dots_image_kernel
    微信小程序如何分包管理
    android APP使用指定网络上网的原理
    Mysql创建账号并且赋予权限
    图深度学习--图论基础
    社区治理进化史!拓世法宝化身“虚拟社工”,点亮智能社区的每一个角落
    微软宣布 Windows 11 开始大范围推送
    把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
    C语言每日一题(13) 单身狗1,2
    交叉编译poco-1.9.2
  • 原文地址:https://blog.csdn.net/qq_62377885/article/details/132841571