yang.common.Empty is a Serializable, which has no state beside its class
hierarchy. It therefore lends itself to having a singleton value object
-- except SpotBugs flags this as a violation MSC07-J rule in SEI CERT
Oracle Coding Standard for Java.
Digging into this, the readResolve() guard could be defeated via
hand-crafted serialization -- and thus could in theory have more than
once instance. Doing so would allow to have two Empty instances which
have the same Class, but do not compare as equal.
This patch erases that possibility, fixing a possibility of the error.
Also update documentation a bit to remove the word 'singleton', so users
are not tempted to use '==' for comparison -- which is already a bad
idea anyway.
Change-Id: Ibccd2909790e7a46d002d80f7b36280ff27fb724
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
import org.opendaylight.yangtools.concepts.Immutable;
/**
- * Dedicated singleton type for YANG's {@code type empty} value.
+ * Dedicated type for YANG's {@code type empty} value.
*/
@NonNullByDefault
public final class Empty implements Immutable, Serializable {
@Override
public boolean equals(final @Nullable Object obj) {
- return this == obj;
+ // Note: this is nominally a singleton, but due to it being Serializable multiple instances might be created
+ // via hand-crafted serialization streams. We therefore do not rely on '==' but on 'instanceof' check.
+ return obj instanceof Empty;
}
@Override