使用用户定义的注释生成编译器警告

发布于 2024-11-27 07:52:26 字数 51 浏览 0 评论 0原文

是否可以让编译器在遇到用户定义的注释时生成警告?类似于 @Deprecated 注释吗?

Is it possible for make the compiler generate a warning when it encounters a user defined annotaion? Something similar to the @Deprecated annotation?

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

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

发布评论

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

评论(3

虐人心 2024-12-04 07:52:26

根据您最初的问题和评论,我假设您正在尝试执行以下操作:

  • 将代码标记为不完整(带有编译器警告),以便其他开发人员尚未使用它。
  • 稍后识别 IDE 中不完整的代码。

我不相信你可以用编译器警告标记代码。 @Deprecated 标签已融入编译器中。指示方法不完整的更常见方法是抛出异常:

throw new UnsupportedOperationException("Not Implemented Yet");

直到运行时才实现效果,但其他开发人员应该对他们的代码进行单元测试。

至于识别不完整的代码,我仍然会参考我原来的评论。使用 TODO 注释标签,Eclipse 将为您构建一个任务列表。如果您的列表中充满了尚未清理的自动生成的代码,您可以使用 FIXMEXXX 或定义您自己的代码。然后您应该能够过滤您的列表。

Based on your original question and comments, I assume you're trying to do the following:

  • Mark code as incomplete (with a compiler warning) so other developers do not use it yet.
  • Identify the incomplete code in your IDE at a later point in time.

I don't believe you can mark the code with a compiler warning. The @Deprecated tag is baked into the compiler. A more common way of indicating a method is incomplete is by throwing an exception:

throw new UnsupportedOperationException("Not implemented yet");

The effect isn't realized until runtime, but the other developers should be unit testing their code.

As for identifying the incomplete code I would still refer back to my original comment. Use the TODO comment tag and Eclipse will build a task list for you. If your list is cluttered with auto-generated code that hasn't been cleaned up, you can use FIXME, XXX, or define your own. You should then be able to filter your list.

花之痕靓丽 2024-12-04 07:52:26

I've asked the Lombok people to look at providing this functionality https://github.com/peichhorn/lombok-pg/issues/114 and it is now implemented ;-) https://github.com/peichhorn/lombok-pg/wiki/%40Warning

紫南 2024-12-04 07:52:26

可以通过注释处理器 api

这是轻量级 Javac 警告注释库 https://github.com/pushtorefresh/ javac-warning-annotation

用法:

// some code...

@Warning("This method should be refactored")
public void someCodeWhichYouNeedAtTheMomentButYouWantToRefactorItLater() {
    // bad stuff going on here...
}

It's possible via annotation processors api

Here is Lightweight Javac Warning Annotation library https://github.com/pushtorefresh/javac-warning-annotation

Usage:

// some code...

@Warning("This method should be refactored")
public void someCodeWhichYouNeedAtTheMomentButYouWantToRefactorItLater() {
    // bad stuff going on here...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文