Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrotbohm/f47a02d0ca1ea0bb85fb4a033bb52122 to your computer and use it in GitHub Desktop.
Save odrotbohm/f47a02d0ca1ea0bb85fb4a033bb52122 to your computer and use it in GitHub Desktop.
Using annotation based null checks in Eclipse

Using annotation based null checks in Eclipse

Found issues

Calls to remote API not using nullability annotations cause warnings

Assume you call some external API (e.g. JDK API) that doesn’t use the nullability annotations:

// API declaring everything non-nullable via annotation on the package
void someMethod(String param) { … }

// Using code
Method method = … // known to be non-null
someMethod(method.getName());

In this case I get a warning for method.getName() as Eclipse assumes the method to potentially return null, which is as right as it is wrong, as without an annotation it can’t (shouldn’t) actually assume anything.

Question: Is there a way to configure Eclipse to abstain from generating warnings for this?

Answer: Looks like you need to configure "Unchecked conversion from non-annotated type to @NonNull type" to ignore.

Field value validation

Looks like for a class like this:

class SomeClass {

  private final DependencyA dependencyA;
  private DependencyB dependencyB;

  SomeClass(DependencyA dependencyA) {
    this.dependencyA = dependencyA;
  }

  …
}

I get a warning on the constructor that the non-nullable field dependencyB has not been initialized.

We currently embrace the pattern of trying to stick to immutability as much as possible and usually hold non-final fields for optional dependencies. I.e. everything non-final is usually optional and thus implicitly nullable.

Question: Is there a way for Eclipse to understand this as is or do we need to annotate all non-final fields with @Nullable. If the latter is the case, Spring’s @Nullable needs to be changed to be usable on fields, too.

Nullable low-level methods in Spring API

ClassUtils.getDefaultClassloader() is declared nullable. However it’s unclear what to actually do when this method returns null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment