简体   繁体   English

有没有一种方法可以使特定类的子类中的特定Java注释成为必需的

[英]Is there a way to make a specific Java annotation mandatory in subclasses of a specific class

I am not a super expert in Java annotations, and I wanted to find out if there is a way to make sure that given a subclass of a specific class, the subclass must use a specific Java annotation on the class definition itself or the code won't compile? 我不是Java注释的超级专家,我想找出是否有办法确保在给定特定类的子类的情况下,该子类必须在类定义本身或所获得的代码上使用特定的Java注释不编译?

Thanks IS 谢谢IS

I don't think there is a way to enforce the developer to put annotation on subclass. 我认为没有办法强制开发人员在子类上添加注释。

However, you can add the required annotations on your parent class and its subclass will "automatically" have the annotation without you explicitly specifying. 但是,您可以在父类上添加所需的注释,并且其子类将“自动”具有注释,而无需您明确指定。

@RequiredAnnotations
class Parent{
    //...
}

// it will have @RequiredAnnotations without specifying
class Child extend Parent{
    //...
}

It works with interface , concrete class or abstract class inheritance. 它与接口具体类抽象类继承一起使用。

Like Pedro points out, the @RequiredAnnotation needs to have @Inherited to make it work. 就像Pedro指出的那样, @RequiredAnnotation必须具有@Inherited才能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM