1
是否有办法强制编译器检查是否在父方法中使用了与child方法相同的注释。如果我使用@NotNull
和String
一切都按我的预期工作。@Override不强制在重写的方法中使用枚举注释
public static final int FIRST_ENUM = 0;
public static final int SECOND_ENUM = 1;
@IntDef(value = {
FIRST_ENUM,
SECOND_ENUM
})
@Retention(RetentionPolicy.SOURCE)
public @interface MyEnum {
}
class Parent {
public void method(@MyEnum int a) {
}
}
class Child extends Parent {
@Override
public void method(int a) { // This is valid otherwise there is no @MyEnum
}
}
此代码是有效的,虽然我不覆盖所有注释。
感谢您的建议,但我需要在Android Studio中进行注释检查,并且Android Studio会自动进行一些检查。例如'int sth = 10;''@MyEnum int num = sth;/*没有错误(坏)* /''num = sth;/*错误显示(ok)* /'也许所有检查尚未执行... – mmrmartin
您可以向Android Studio团队提交错误报告并等待他们解决。在此之前,您可以使用第三方工具,如[Enum Checker](https://checkerframework.org/manual/#fenum-checker)。 – mernst