如何故意导致自定义 java 编译器警告消息? [英] How to intentionally cause a custom java compiler warning message?

查看:38
本文介绍了如何故意导致自定义 java 编译器警告消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将提交一个丑陋的临时黑客,以便在我们等待修复外部资源时解决阻塞问题.除了用可怕的大注释和一堆 FIXME 标记它之外,我希望编译器抛出一个明显的警告消息作为提醒,这样我们就不会忘记将其删除.例如,类似于:

I'm about to commit an ugly temporary hack in order to work around a blocking issue while we wait for an external resource to be fixed. Aside from marking it with a big scary comment and a bunch of FIXMEs, I'd love to have the compiler throw an obvious warning message as a reminder so we don't forget to take this out. For example, something like:

[javac] com.foo.Hacky.java:192: warning: FIXME temporary hack to work around library bug, remove me when library is fixed!

有没有一种方法可以通过我选择的消息引起有意的编译器警告?如果做不到这一点,添加到代码中以抛出现有警告的最简单的方法是什么,可能在违规行的字符串中包含一条消息,以便将其打印在警告消息中?

Is there a way I can cause an intentional compiler warning with a message of my choosing? Failing that, what's the easiest thing to add to the code to throw an existing warning, with perhaps a message in a string on the offending line so it gets printed in the warning message?

已弃用的标签似乎对我没有任何作用:

Deprecated tags don't seem to be doing anything for me:

/**
 * @deprecated "Temporary hack to work around remote server quirks"
 */
@Deprecated
private void doSomeHackyStuff() { ... }

在 eclipse 或 sun javac 1.6(从 ant 脚本运行)中没有编译器或运行时错误,而且它肯定在执行函数.

No compiler or runtime errors in eclipse or from sun javac 1.6 (running from ant script), and it's definitely executing the function.

推荐答案

我见过的一种技术是将其与单元测试联系起来(你单元测试,对吗?).基本上,您创建一个单元测试,一旦外部资源修复完成,该单元测试失败.然后,您对该单元测试发表评论,告诉其他人一旦问题得到解决,如何撤消您的粗暴攻击.

One technique that I've seen used is to tie this into unit testing (you do unit test, right?). Basically you create a unit test that fails once the external resource fix is achieved. Then you comment that unit test to tell others how to undo your gnarly hack once the issue is resolved.

这种方法的真正巧妙之处在于,撤销黑客行为的触发因素是解决核心问题本身.

What's really slick about this approach is that the trigger for undoing your hack is a fix of the core issue itself.

这篇关于如何故意导致自定义 java 编译器警告消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆