java - 你能在父类中获得子类注释吗?
问题描述
这是有线的,但你能做到吗?
我想使用无参数构造函数(所以开发依赖于我的框架不需要扩展构造函数),但我想在字段中使用 final。所以 :
注释.java
public @interface Annotation {
String value();
}
父类.java
public abstract class Parent {
public final String name;
// this will cause you must extends constructor in any sub class
public Parent(Annotation annotation){
this.name = annotation.value();
}
}
接口定义
public abstract class Define extends Parent {
// You can't do that:
// Cannot reference 'this' before supertype constructor has been called
public Define (){
this(this.getClass().getAnnotation(Annotation.class).value());
}
// You can do that, but not working
// Define is point at Define as is rather I hope the sub class MyModule
public Define (){
this(Define.class.getAnnotation(Annotation.class).value());
}
public Define (Annotation annotation){
super(annotation); // You must extend this
}
public abstract void foo();
}
我希望开发人员像这样使用我的框架:
public class MyModule extends Define {
public void foo(){
// foo bar
}
}
但由于Cannot reference 'this' before supertype constructor has been called
,你必须写:
@Annotation
public class MyModule extends Define {
// my framework using scan an IoC auto invoke
public MyModule(Annotation annotation){
super(annotation.value())
}
public void foo(){
// foo bar
}
}
悖论是name
写在注解中,而且this
必须在newInstance之后。所以这个问题更像:
如何getClass()
分班?
所以唯一的解决方案是放弃 final 字段并使用 init() 之类的东西?
解决方案
不需要每个子类读取注释并将其传递给超类,您可以将读取注释移动到基类:
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value() default "defaultValue";
}
abstract class Base {
final String value;
Base() {
MyAnnotation myAnnotation= getClass().getAnnotation(MyAnnotation.class);
if (myAnnotation == null) {
throw new IllegalStateException("Class " + getClass().getName() + " does not have require annotation MyAnnotation");
}
this.value = myAnnotation.value();
}
}
@MyAnnotation
class DefaultValue extends Base {
}
@MyAnnotation("someValue")
class ExplicitValue extends Base {
}
class NoAnnotation extends Base {
}
鉴于这些类,这两行
System.out.println(new DefaultValue().value);
System.out.println(new ExplicitValue().value);
将分别打印defaultValue
和someValue
。但是,这一行会抛出一个IllegalStateException
:
new NoAnnotation();
不幸的是,Java 类型系统不允许在编译时对每个具体类强制执行注释要求,所以这个运行时异常是你能得到的最好的(在其他静态分析工具或 ArchUnit 等架构测试软件之外)。
推荐阅读
- android - 编译时错误:面临在 android studio 中使用 c++ 调用一种方法的问题
- html - Bulma:如何为项目列表对齐列表项
- ios - 通过 NodeJS 发送推送,即使通知也看不到操作按钮
- python - 在python中将numpy矩阵绘制为violinplot
- python - elif循环中的python dict值
- jquery - 模态显示并立即消失
- typescript - Typescript - 在保留签名的同时包装函数
- python - 无需初始化的面向对象编程
- linux - 如何使用linux命令从字符串中获取参数值
- xamarin - 使用样式下划线在 Xamarin 中为文本添加下划线