migrator / guava-libraries-2

Guava: Google Core Libraries for Java 1.6+
0 stars 0 forks source link

Converter - FindBugs problem #10

Open migrator opened 9 years ago

migrator 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_HASHCODE

@ 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">

4.0.0
<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>

@@

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.

relevance: 2

migrator commented 9 years ago

summary: Not Defined

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.

status Not Defined creator: kak@google.com created at: Sep 28, 2014