Eclipse:自定义警告(Rhino 字符串参数)
有没有办法在 Eclipse 中定义自定义警告?如何?
我的动机
我有大量的类通过 Mozilla Rhino 作为 JavaScript 对象公开。我遇到过几个这样的错误:
public void jsFunction_foo(String bar) {
if (bar != null) {
...
} else {
...
}
}
错误:jsFunction_
中的字符串参数永远不会为空。如果 JavaScript 调用 foo(null)
,则到达的参数是 "null"
,而不是 null
。巨大的差异。
解决方案是将参数声明为可脚本化,然后将其转换(如果存在):
public void jsFunction_foo(Scriptable bar) {
if (bar != null) {
String barStr = convertToString(bar);
...
} else {
...
}
}
无论如何。我们在 JavaScript 中公开了 50 多个类。我不想手动完成所有这些工作,而是希望使该过程自动化。类似于:
if (function.name.indexOf("jsFunction_") == 0 &&
function.paramTypes.contains(String.class) &&
stringParamComparedToNull(function))
addWarning(function.lineNum, "It doesn't work like that!");
理想情况下,这个自定义警告将在我的团队(甚至 Mozilla Rhino 项目本身)之间共享,这样其他人就不会再犯同样的错误。
对于那些与“null”相比的人来说,一种让 Eclipse 崩溃的方法也不错。咕噜。
Is there a way to define a custom warning in Eclipse? How?
My motivation
I have a Large Number of classes that are exposed as JavaScript objects via Mozilla Rhino. I've run across a couple bugs like this:
public void jsFunction_foo(String bar) {
if (bar != null) {
...
} else {
...
}
}
The Bug: A String parameter in a jsFunction_
IS NEVER NULL. If the JavaScript calls foo(null)
, the parameter that arrives is "null"
, not null
. HUGE difference.
The solution is to declare the parameter as Scriptable
, and then convert it if present:
public void jsFunction_foo(Scriptable bar) {
if (bar != null) {
String barStr = convertToString(bar);
...
} else {
...
}
}
So anyway. We have 50+ classes we expose in JavaScript. Rather than schlepping through them all by hand, I'd like to automate the process. Something like:
if (function.name.indexOf("jsFunction_") == 0 &&
function.paramTypes.contains(String.class) &&
stringParamComparedToNull(function))
addWarning(function.lineNum, "It doesn't work like that!");
Ideally this custom warning would be shared across my team (or even with the Mozilla Rhino project itself) so other folks won't keep making the same mistakes.
A way to crash Eclipse for people that compare to "null" would be good too. Grr.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PMD 定义 在 Java 代码上使用 XPath 变体的自定义规则。 PMD 还有 Eclipse 插件。
You can use PMD to define a custom rule using variant of XPath over the Java code. PMD also has Eclipse plugin.