Skip to content

Clarify when returned values might be null #358

@smoyer64

Description

@smoyer64

When the ParameterResolver's supports() and resolve() methods were changed to use ParameterContext, the statement that the Parameter was never null was replaced by text that stated ParamterResolver is never null. ParameterResolver itself makes no assertion that getParameter() always returns a non-null value. Thus, I'm now checking for null parameters as follows:

    boolean supported = false;
    Parameter parameter = parameterContext.getParameter();
    if (parameter != null && Performance.class.equals(parameter.getType())) {
      supported = true;
    }
    return supported;

For the ParameterContext, it wouldn't make sense to ever have a non-null parameter (otherwise it would be a SomethingElseContext), so this code is probably completely unnecessary. As a defensive coder, I put it in anyway. Here are my recommendations in order of priority/complexity:

  • For every returned value, explicitly state in the Javadoc whether or not the return type may be null.
  • Globally use Optional when a return type might be null.
  • State in the User Guide that no public APIs ever return null.
  • Add the JSR-305 annotation library with a provided scope and add @CheckForNull, @Nonnull and @Nullable to all public APIs.

I know the JUnit team's policy on avoiding dependencies - adding JSR-305 as provided allows those of us who chose to perform static analysis on our Engine, Extension and test code to choose to add the dependency to our classpath.

Related Resources

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions