Java子类继承父类上的注解

人间二两风 / 2023-07-31 / 原文

子类可以继承到父类上的注解吗?

在编写自定义注解时,可以通过指定@Inherited注解,指明自定义注解是否可以被继承。

实现情况可细分为以下几种

未申明@Inherited 申明了@Inherited
子类的类上能否继承到父类的类上的注解? ×
子类实现了父类上的抽象方法,这个方法能否继承到注解? × ×
子类继承了父类上的方法,这个方法能否继承到注解?
子类重写了父类上的方法,这个方法能否继承到注解? × ×

代码示例

自定义注解

package test.annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
/**
 * 自定义注解
 */
@Inherited  //申明注解可以被继承
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)  //可以通过反射读取注解
public @interface MyAnnotation {
    String value();
}

父类

package test.annotation;
@MyAnnotation(value = "类名上的注解")
public abstract class ParentClass {

    @MyAnnotation(value = "父类的abstractMethod方法")
    public abstract void abstractMethod();

    @MyAnnotation(value = "父类的doExtends方法")
    public void doExtends() {
        System.out.println(" ParentClass doExtends ...");
    }

    @MyAnnotation(value = "父类的doHandle方法")
    public void doHandle(){
        System.out.println(" ParentClass doHandle ...");
    }
}

子类

package test.annotation;
public class SubClass extends ParentClass{

    //子类实现父类的抽象方法
    @Override
    public void abstractMethod() {
        System.out.println("子类实现父类的abstractMethod抽象方法");
    }

    //子类继承父类的doExtends方法,直接调用父类方法

    //子类重写父类的doHandle方法
    @Override
    public void doHandle(){
        System.out.println("子类覆盖父类的doHandle方法");
    }
}

测试类

package test.annotation;

import java.lang.reflect.Method;

public class MainTest {
    public static void main(String[] args) throws SecurityException,NoSuchMethodException {

        Class<SubClass> clazz = SubClass.class;

        if (clazz.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation cla = clazz.getAnnotation(MyAnnotation.class);
            System.out.println("子类继承到父类类上Annotation,其信息如下:"+cla.value());
        } else {
            System.out.println("子类没有继承到父类类上Annotation");
        }
        // 实现抽象方法测试
        Method method = clazz.getMethod("abstractMethod", new Class[] {});
        if (method.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation ma = method.getAnnotation(MyAnnotation.class);
            System.out.println("子类实现父类的abstractMethod抽象方法,继承到父类抽象方法中的Annotation,其信息如下:"+ma.value());
        } else {
            System.out.println("子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation");
        }
        //重写测试
        Method methodOverride = clazz.getMethod("doExtends", new Class[] {});
        if (methodOverride.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation ma = methodOverride.getAnnotation(MyAnnotation.class);
            System.out.println("子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:"+ma.value());
        } else {
            System.out.println("子类继承父类的doExtends方法,没有继承到父类doExtends方法中的Annotation");
        }
        //继承测试
        Method method3 = clazz.getMethod("doHandle", new Class[] {});
        if (method3.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation ma = method3.getAnnotation(MyAnnotation.class);
            System.out.println("子类覆盖父类的doHandle方法,继承到父类doHandle方法中的Annotation,其信息如下:"+ma.value());
        } else {
            System.out.println("子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation");
        }
    }
}

编写自定义注解时未申明@Inherited的运行结果:
子类没有继承到父类类上Annotation
子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation
子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:父类的doExtends方法
子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation

编写自定义注解时申明了@Inherited的运行结果:
子类继承到父类类上Annotation,其信息如下:类名上的注解
子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation
子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:父类的doExtends方法
子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation

重写方法如何实现继承注解?

@Inherited只能实现类上的注解继承;要想实现方法上注解的继承,可以通过反射在继承链上找到方法上的注解。
Spring框架中提供了AnnotatedElementUtils类,来方便我们处理注解的继承问题。

// spring提供的工具类 AnnotatedElementUtils
MyAnnotation annotation = AnnotatedElementUtils.getMergedAnnotation(SubClass.class.getMethod("doHandle"), MyAnnotation.class);

调用AnnotatedElementUtils的findMergedAnnotation()方法,可以帮助我们找到父类和接口、父类方法和接口方法上的注解,实现一键找到继承链的注解。