FindBugs warning with Guava’s Predicate interface

When running FindBugs on some code I wrote, I received a  NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE warning:

Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE (click for details)
In class com.wall.steve.HelperMethods$1
In method com.wall.steve.HelperMethods$1.apply(MyItem)
Parameter item
At HelperMethods.java:[line 27]

This was the code I had written:

//com.wall.steve.HelperMethods class
public static final Predicate<MyItem> NON_REJECTED_ITEMS = new Predicate<MyItem>() {
    @Override
    public boolean apply(final MyItem item)  //This is Line 27
    {
        return !item.isRejected();
    }
};

The issue was with my implementation of the apply() method.  Guava has the parameter of the apply method marked @Nullable.

//Guava 18.0 - Predicate class 
boolean apply(@Nullable T input);

FindBugs flagged my implementation since it does not allow for the item parameter to be null.  To fix this issue, I used the @Nonnull annotation (from the javax.annotation package):

//com.wall.steve.HelperMethods class
public static final Predicate<MyItem> NON_REJECTED_ITEMS = new Predicate<MyItem>() {
    @Override
    public boolean apply(@Nonnull final MyItem item)  //Line 27
    {
        return !item.isRejected();
    }
};

This question on Stack Overflow was very similar and provides some more background in answering “why”.

Advertisements

About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Drools, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Guava, Java and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s