google / google/error-prone

ImmutableEnumChecker should respect Design Pattern (Singleton using Enum)

Open
#1,137 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

Please answer these questions before submitting your issue. Thanks!

### What version of Error Prone are you using?

2.3.1 (latest)

### Does this issue reproduce with the latest release?

yes

### What did you do?

public enum EnumSingleton {

INSTANCE;

private final com.fasterxml.jackson.databind.ObjectMapper mapper;

private EnumSingleton() {
this.mapper = new com.fasterxml.jackson.databind.ObjectMapper();
}

public com.fasterxml.jackson.databind.ObjectMapper getMapper() {
return this.mapper;
}

}

### What did you expect to see?

No issue, because the enum has only one entry and is following the "Design Pattern – Singleton using Enum" and com.fasterxml.jackson.databind.ObjectMapper is thread-safe.

https://dzone.com/articles/java-singletons-using-enum
http://mydevgeek.com/design-pattern-singleton-using-enum/
Joshua Bloch explained using an Enum in his Effective Java Reloaded talk at Google I/O 2008.

### What did you see instead?

[javac] ...EnumSingleton.java:7: warning: [ImmutableEnumChecker] enums should be immutable: 'EnumSingleton' has field 'mapper' of type 'com.fasterxml.jackson.databind.ObjectMapper', the declaration of type 'com.fasterxml.jackson.databind.ObjectMapper' is not annotated with @com.google.errorprone.annotations.Immutable or @javax.annotation.concurrent.Immutable
[javac] private final com.fasterxml.jackson.databind.ObjectMapper mapper;
[javac] ^
[javac] (see http://errorprone.info/bugpattern/ImmutableEnumChecker)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.