问题
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