Reputation: 27003
Assume this code:
String[] data = new String[2];
data[0] = "OK";
data[1] = "returning data";
data[2] = "data out of bounds";
For sure will throw an IndexOutOfBounds
exception.
My question is:
Eclipse
parser does not complain this issue with a warning
or an error
?Analizing the case I find:
There are other similar warnings/errors being detected like:
uninitialized variable
when you try to use a variable declared as List<String> = null
.unreachable code
when writting sentences after a valid return
.unused code
in the dead branches of if
statements.Upvotes: 6
Views: 133
Reputation: 33000
Shouldn't Eclipse parser detect find this kind of IndexOutOfBounds exceptions?
Obviously the answer is yes since it is easy to detect.
But you can also do better and avoid the potential error by not using a magic constant:
String[] data = new String[] { "OK", "returning data", "data not out of bounds" };
Upvotes: 0
Reputation: 719386
There is some reason because Eclipse parser does not complain this issue with a warning or an error?
Eclipse doesn't give you an error message because this is valid Java code, according to the Java language specification. If Eclipse treated that as an compilation error, it would not be a proper Java compiler.
The warning case is less clear cut. Yes, Eclipse could (legitimately) provide a warning, much like it warns about some cases involving NPEs. However, there is a trade-off between producing helpful warnings, and the time that the compiler spends checking for the conditions that give rise to them. Too much checking will make the compiler slower for all programs, and programmers will complain about that. (Especially programmers who work on applications with thousands of classes.)
As @Tagir notes, if you want to be warned about this kind of thing, consider using a bug checker such as FindBugs or PMD.
There are other similar warnings/errors being detected like ...
The two of three cases that you listed are all compilation errors because the Java Language Specification states that they are invalid Java code. I'm not sure about the "dead code" case because it is unclear what case you are referring to. If you are referring to
if (<compile-time-constant-expr>) { ... } else { ... }
then that code shouldn't give either a compilation error or a warning.
Upvotes: 1
Reputation: 54679
The other cases that you mentioned are not directly related:
An uninitialized variable is a plain compile-time error, according to JLS Chapter 16, "Definite Assignment".
The unreachable code is a compile-time error as well, according to JLS Section 14.21, "Unreachable Statements"
The last remaining one is the dead code. This is not an error, and the reasons are also explained in the Unreachable Statements section:
One might expect the if statement to be handled in the following manner:
...
This approach would be consistent with the treatment of other control structures. However, in order to allow the if statement to be used conveniently for "conditional compilation" purposes, the actual rules differ.
As an example, the following statement results in a compile-time error:
while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar > case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.
So the "dead code" is "unusual" and certainly something that is worth pointing out, but not necessarily an error.
Regarding your actual question (ignoring the fact that the Parser and the Compiler are two different things - it's clear what the question was about): This is a very specific case. It only applies when the declaration of the array and the access to the array are in the same scope. Additionally, the indices must be compile-time constants.
So obviously, it would not be possible for
void init(String data[]) {
data[0] = "OK";
data[1] = "returning data";
data[2] = "data out of bounds";
}
or
int i = 0;
data[i++] = "OK";
data[i++] = "returning data";
data[i++] = "data out of bounds";
There probably is no convincing, profound reason of why this is not detected by Eclipse, except that the ratio between the effort of detecting such bugs and the benefit is not sufficiently high - so maybe something like this will be added in a future release. There already are some rather sophisticated warnings in Eclipse (e.g. automatic detection of "Null pointer access"). But for a more detailed, static analysis, there are dedicated tools, like FindBugs or PMD, which take greater effort (and more time, by the way) than the tests that are done casually and on the fly by eclipse.
A side note: You can consider avoiding this kind of bug with
String data[] = {
"OK",
"returning data",
"data out of bounds"
};
Upvotes: 2
Reputation: 100309
You can install a FindBugs plugin for Eclipse (available on Eclipse marketplace). It's capable to detect such problem:
Upvotes: 4