mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Converter - FindBugs problem #1858

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
FindBugs reports "inherits equals and uses Object.hashCode()" for 
Converter-subclasses.

http://findbugs.sourceforge.net/bugDescriptions.html#HE_INHERITS_EQUALS_USE_HASH
CODE

@ test/Test.java @

package test;

import com.google.common.base.Converter;

import java.io.Serializable;

public final class Test {

  private static final class TestConverter
      extends Converter<String, String> implements Serializable {

    /*package*/ static final TestConverter INSTANCE = new TestConverter();

    private static final long serialVersionUID = 1;

    @Override
    public String toString() {
      return "TestConverter.testConverter()";
    }

    @Override
    protected String doForward(String string) {
      return string;
    }

    @Override
    protected String doBackward(String string) {
      return string;
    }

    private Object readResolve() {
      return INSTANCE;
    }
  }

  public static Converter<String, String> testConverter() {
    return TestConverter.INSTANCE;
  }

  private Test() {
  }
}

@ pom.xml @

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

@@

mvn clean install

...
[INFO] --- findbugs-maven-plugin:3.0.0:check (default) @ test ---
[INFO] BugInstance size is 1
[INFO] Error size is 0
[INFO] Total bugs: 1
[INFO] test.Test$TestConverter inherits equals and uses Object.hashCode() 
["test.Test$TestConverter"] At Test.java:[lines 9-32]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.516 s
[INFO] Finished at: 2014-09-29T00:00:54+01:00
[INFO] Final Memory: 23M/118M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal 
org.codehaus.mojo:findbugs-maven-plugin:3.0.0:check (default) on project test: 
failed with 1 bugs and 0 errors -> [Help 1]
...

@@

The "problem" is the override in Converter:

  @Override
  public boolean equals(@Nullable Object object) {
    return super.equals(object);
  }

But there is no corresponding override for hashCode:

  @Override
  public int hashCode() {
    return super.hashCode();
  }

This forces everyone to either ignore the FindBugs warning or include the 
snippet above in their subclass.

Please add 

  @Override
  public int hashCode() {
    return super.hashCode();
  }

to Converter, thanks.

Original issue reported on code.google.com by sebastia...@gmail.com on 28 Sep 2014 at 10:07

GoogleCodeExporter commented 9 years ago
To be clear, this is actually a false positive from FindBugs (since there isn't 
actually a bug here).

Looks like we only override equals so we can add additional documentation. I 
wonder if we should just move that documentation into the class docs, or drop 
it all together.

Original comment by kak@google.com on 29 Sep 2014 at 5:33

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07