Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another false positive SING_SINGLETON_IMPLEMENTS_SERIALIZABLE when class is not a singleton (e.g. protobuf) #2985

Open
cpfeiffer opened this issue May 15, 2024 · 1 comment

Comments

@cpfeiffer
Copy link
Contributor

https://github.com/spotbugs/spotbugs/blob/master/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/MultipleInstantiationsOfSingletons.java#L218 lists the criteria for recognizing a class as a Singleton. While already quite sophisticated, it wrongly recognizes some non-singleton classes as singleton. E.g. protobuf-generated classes that contain a builder to create an instance like this (GeneratedMessageV3 is Serializable).

Here's a manually simplified version of such a class generated by the protoc compiler:

public final class Person extends
    com.google.protobuf.GeneratedMessageV3 implements PersonOrBuilder {
  private static final long serialVersionUID = 0L;

  private static final Person DEFAULT_INSTANCE;
  static {
    DEFAULT_INSTANCE = new Person();
  }

  public static Person getDefaultInstance() {
    return DEFAULT_INSTANCE;
  }

  private Person(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
    super(builder);
  }
  private Person() {
    name_ = com.google.protobuf.LazyStringArrayList.emptyList();
  }

  @java.lang.Override
  @SuppressWarnings({"unused"})
  protected java.lang.Object newInstance( <--- this method should already be an indicator that it is not a singleton
      UnusedPrivateParameter unused) {
    return new Person();
  }

  public static Builder newBuilder() {
    return DEFAULT_INSTANCE.toBuilder();
  }

  public static final class Builder extends
      com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements PersonOrBuilder {
    public Person build() {
      return new Person(this);
    }
  }
}
@cpfeiffer
Copy link
Contributor Author

(Related to #2964)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant