Skip to content

Instantly share code, notes, and snippets.

@unix-junkie
Created July 12, 2017 13:04
Show Gist options
  • Save unix-junkie/9f3919b4634a7c14586f3b594c15e7f3 to your computer and use it in GitHub Desktop.
Save unix-junkie/9f3919b4634a7c14586f3b594c15e7f3 to your computer and use it in GitHub Desktop.
Why I think null analysis in Eclipse is better
package com.example;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class NullAnalysisTest {
void test0(final Object @Nullable[]@Nullable[] values) {
for (final Object[] line : values) { // XXX: values can be null (Eclipse: caught, IDEA: caught)
for (final Object item : line) { // XXX: line can be null (Eclipse: caught, IDEA: caught)
}
}
}
void test1(final Object @NotNull[]@Nullable[] values) {
for (final Object[] line : values) { // IDEA: false positive here
for (final Object item : line) { // XXX: line can be null (Eclipse: caught, IDEA: ignored)
}
}
}
void doSmth2(final Object @Nullable[]@NotNull[] values) {
for (final Object[] line : values) { // XXX: values can be null (Eclipse: caught, IDEA: ignored)
for (final Object item : line) { // IDEA: false positive here
}
}
}
void doSmth3(final @Nullable Object @NotNull[]@NotNull[] values) {
for (final Object[] line : values) {
for (final Object item : line) {
System.out.println(item.hashCode()); // XXX: potential NPE (Eclipse: ignored, IDEA: ignored)
}
}
}
void doSmth4() {
final Object[]@NotNull[] values = new Object[]@NotNull[] {
/*
* XXX: Null spec violation: nested array is null while
* declared as @NotNull[].
*
* Will produce an NPE when iterating over the nested
* array (Eclipse: caught, IDEA: ignored).
*/
null,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment