How to create custom warning in Eclipse for a standard Java API

房东的猫 提交于 2019-12-05 07:32:25

问题


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.

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

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.


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/25751054/how-to-create-custom-warning-in-eclipse-for-a-standard-java-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!