Is there a way to define a custom warning in Eclipse? How?
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.
You can use PMD to define a custom rule using variant of XPath over the Java code. PMD also has Eclipse plugin.