当您为您的类实现 Serializable 时,IDE 会显示一条警告:可序列化类 YOURCLASS 未声明 long 类型的静态最终 serialVersionUID 字段。
我如何定义一个接口(interface)来强制 IDE 为我的字段显示此警告?
public class MyClass implements Serializable
{
// this class has warning
}
public class MyClass implements MyInterface
{
// I want to have warning for this class
}
public interface MyInterface{
public static final long myField;
}
最佳答案
您可以使用 aspectj生成编译器警告或错误。它有时用于强制架构方面,但也可能适用于您。
切入点定义本身作为练习留给读者:-)
@Aspect
public class ArchitecturalEnforcement {
@Pointcut("...")
public void implementsInterface() {}
@Pointcut("...")
public void definesConstant(){}
/** Advice that defines an error when a GUI method calls a method in the DAO layer */
@DeclareWarning("implementsInterface() && !definesConstant()")
private static final String WARNING= "...";
}
阅读更多关于 AOP 的信息(面向切面编程)
关于java - 如何定义强制 IDE 显示 "not declared"警告的接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9110190/