如何在Eclipse中为标准Java API创建自定义警告

问题描述:

我希望Eclipse在我的Java项目中警告使用旧的Date / Calendar API,只允许Java 8全新的 Date-Time API 作为一个beeing

I want Eclipse to warn the use the of old Date/Calendar API in my Java project, allowing only the Java 8 brand new Date-Time API as the one beeing used in the source.

如果配置Eclipse显示警告,如果例如 java.util.Date 被使用?

How can I configure Eclipse to display a warning if, for example, java.util.Date is used?

我接受对这种方法的其他评论,以及其他替代方案。我知道我也应该在CI构建中应用这种类型的验证,以及Java策略可能是另一种选择。

I happly accept additional comments on this approach, as well for other alternatives. I am aware that I should also apply this type of validation in the CI build, as well that Java Policy might be another alternative.

p>您可以为项目添加一个访问规则,导致Eclipse在使用某个类时显示警告。右键单击您的项目 - >属性 - > Java构建路径 - >库。展开 JRE系统库(因为您的目标类是 java.util.Date ),然后选择访问规则,然后点击编辑

You can add an access rule for your project that causes Eclipse to show a warning when a certain class is used. Right-click on your project -> Properties -> Java Build Path -> Libraries. Expand the JRE System Library (since your target class is java.util.Date) and select Access rules, then click on Edit.

在显示的对话框中,单击添加并填写分辨率(例如,沮丧会产生警告)模式匹配。在你的情况下,模式是 java / util / Date

In the shown dialog, click Add and fill in the resolution (e.g. Discouraged would generate a warning) and the pattern to match. In your case, the pattern is java/util/Date.

另一个选择是编写自定义注释并使用Java注释处理API( JSR269 )来处理它们以生成警告。但是,这将需要您对使用该类的代码中的所有地方进行注释。

Another alternative is to write custom annotations and use the Java annotation processing API (JSR269) to process them to generate a warning. But that would require you to annotate all the places in the code where you use that class.