如何定义一个接口来强制 IDE 显示“未声明”?警告?

发布于 2025-01-01 11:16:01 字数 417 浏览 1 评论 0原文

当您为类实现 Serialized 时,IDE 会显示一条警告:可序列化类 YOURCLASS 未声明 long 类型的静态最终 serialVersionUID 字段。

如何定义一个接口来强制 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;
}

When you implement Serializable for your class, IDE shows a warning that The serializable class YOURCLASS does not declare a static final serialVersionUID field of type long.

How can I define an interface that forces the IDE to show this warning for my field?

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蛮可爱 2025-01-08 11:16:01

您可以使用 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 上的更多内容(面向方面​​编程)

You can use aspectj to generate compiler warnings or errors. It is sometimes used to enforce architectural aspects, but might be applicable for you, too.

The Pointcut definitions itself are left as an excercise for the reader :-)

@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= "...";
}

Read more on AOP (Aspect Oriented Programming)

街角卖回忆 2025-01-08 11:16:01

它是特殊情况,仅适用于实现 Serialized 接口并可在 eclipse 中配置的类。 Windows--->首选项--->编译--错误/警告-->潜在的编程问题---> 这是 eclipse 对此进行配置的地方。即使您强制 IDE 通过其他方式为您的类显示警告,但当您使用 javac 编译时,它不会强制。

It is special case applicable only in case of classes implementing Serializable interface and configurable in eclipse. Windows--->Preference--->Compile--Error/warnings-->Potential programming problems---> This where eclipse has configuration for this. Even though you force IDE to display warning by some other means for your class, when you compile with javac, it won't force.

抠脚大汉 2025-01-08 11:16:01

这与您使用的 IDE 没有任何关系,它与您声明的接口错误有关。您的接口必须包含方法签名,现在在您的示例中它仅包含常量的声明。为了使你的接口有效,它需要看起来更像这样:

public interface MyInterface{
   public long doSomethingAndReturnALong();
   public void doSomethingElseButRequireAnInt(int var);
}

如果你在接口中声明任何类型的变量,就像上面所做的那样,它被假定为静态和最终的,也称为不能的常量由实现该接口的类来更改。

希望有帮助!
大卫

This doesn't have anything to with what IDE you're using, it has to do with that you declared your interface wrong. Your interface must contain method signatures and right now in your example it only contains a declaration for a constant. For your interface to be valid it would need to look something more like this:

public interface MyInterface{
   public long doSomethingAndReturnALong();
   public void doSomethingElseButRequireAnInt(int var);
}

If you declare any kind of variable in an interface, such as you did above, it's assumed to be static and final, also known as a constant that can't be changed by classes that implement the interface.

Hope that helps!
David

一瞬间的火花 2025-01-08 11:16:01

一般来说,这对于 Java 中的接口来说是不可能的。

如果您希望该字段可公开访问,那么无论如何您都应该为其提供 getter 和 setter 方法。您可以在接口中声明这些方法,如果实现类没有声明它们,这当然会导致编译器错误。

如果您只想强制类拥有一个私有字段,您可以在某个时候通过反射访问该字段,那么您应该考虑是否有另一个不需要该字段的解决方案。也许也可以使用方法?

最后一个选项是使用静态分析工具,例如 FindBugs,它允许您定义 自定义错误模式。因此,您可以编写一条规则来查找没有此字段的接口的实现者,并在 FindBugs 配置中启用它。有一个很好的 FindBugs Eclipse 插件,它可以在代码中显示 FindBugs 警告(就像常规 Eclipse 警告一样)。

In general, this is not possible with interfaces in Java.

If you want the field to be publicly accessible, then you should provide getter and setter methods for it anyway. You can declare these methods in the interface, which will of course result in a compiler error if an implementing class does not declare them.

If you only want to force the classes to have a private field which you would at some point access by reflection, you should think if there is another solution that doesn't need the field. Perhaps its also possible to use methods?

The last option would be to use a static analysis tool like FindBugs, which allows you to define custom bug patterns. So you would write a rule which finds implementors of your interface without this field and enable it in your FindBugs configuration. There exists a good Eclipse plugin for FindBugs which displays the FindBugs warnings in your code (just like the regular Eclipse warnings).

妥活 2025-01-08 11:16:01

IDE 并不是发出警告的人。它是编译器。如果您需要自定义警告,则需要自定义编译器。但是你应该将编译器与你的界面一起分发。

为了创建自定义编译器,您可以使用:
http://today. java.net/article/2008/04/09/source-code-analysis-using-java-6-apis

基本上您需要定义自己的注释和注释处理器。您可能会为您的接口创建一个注释,当一个类实现它并且它没有定义变量时,您将发出警告。
我还没有这样做,所以我无法向您提供更多信息。

另一种可能性是使用定义自定义警告和注释的现有编译器插件:
https://checkerframework.org/
我不知道它是否会帮助您完成任务,或者您最终应该编写自己的编译器插件。

PS:也请查看这篇文章:
如何故意引发自定义 java 编译器警告消息?< /a>

The IDE is not the one raising the warning. It is the compiler. If you need a custom warning, you need a custom compiler. But then you should distribute the compiler with your interface.

In order to create a custom compiler you can use:
http://today.java.net/article/2008/04/09/source-code-analysis-using-java-6-apis

Basically you need to define your own annotation and annotation processor. Probably you will create an annotation for your interface that when a class implements it and it does not define a variable you will raise a warning.
I haven't done this yet, so I cannot give you more info about it.

Another possibility is to use an existing compiler plugin that defines custom warnings and annotations:
https://checkerframework.org/
I do not know if it will help you in your quest, or you should eventually write your own compiler plugin.

PS: checkout this post also:
How to intentionally cause a custom java compiler warning message?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文