From 4a319146ef5ef5bc4ffac58a10410e4dc2d7ed96 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 16 Aug 2022 13:01:14 +0200 Subject: [PATCH] Use @Serial tag With Java 17 we can make serialization-related components explicitly tagged so their shape is enforced. Sprinkle java.io.Serial around. Change-Id: Ibf0ce6e19f8433b3274e05ad3f56d86616146e78 Signed-off-by: Robert Varga --- .../codec/api/IncorrectNestingException.java | 2 ++ .../MissingClassInLoadingStrategyException.java | 2 ++ .../dom/codec/api/MissingSchemaException.java | 2 ++ .../api/MissingSchemaForClassException.java | 4 +++- .../AlphabeticallyTypeMemberComparator.java | 2 ++ .../api/generator/ByTypeMemberComparator.java | 3 +++ .../mdsal/binding/model/api/JavaTypeName.java | 8 +++++++- .../mdsal/binding/spec/util/NodeKey.java | 17 +++-------------- .../yang/binding/InstanceIdentifier.java | 8 ++++++++ .../yang/binding/InstanceIdentifierV3.java | 3 +++ .../yang/binding/KeyedInstanceIdentifier.java | 4 ++++ .../yang/binding/KeyedInstanceIdentifierV2.java | 3 +++ .../yang/binding/test/mock/NodeChildKey.java | 17 +++-------------- .../yang/binding/test/mock/NodeKey.java | 17 +++-------------- 14 files changed, 48 insertions(+), 44 deletions(-) diff --git a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/IncorrectNestingException.java b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/IncorrectNestingException.java index 66ff0f8bd1..995418824e 100644 --- a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/IncorrectNestingException.java +++ b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/IncorrectNestingException.java @@ -8,6 +8,7 @@ package org.opendaylight.mdsal.binding.dom.codec.api; import com.google.common.annotations.Beta; +import java.io.Serial; /** * Thrown where incorrect nesting of data structures was detected and was caused by user. This typically indicates @@ -16,6 +17,7 @@ import com.google.common.annotations.Beta; */ @Beta public class IncorrectNestingException extends IllegalArgumentException { + @Serial private static final long serialVersionUID = 1L; public IncorrectNestingException(final String message) { diff --git a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingClassInLoadingStrategyException.java b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingClassInLoadingStrategyException.java index 60b5c05528..17ffdfa8b8 100644 --- a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingClassInLoadingStrategyException.java +++ b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingClassInLoadingStrategyException.java @@ -8,6 +8,7 @@ package org.opendaylight.mdsal.binding.dom.codec.api; import com.google.common.annotations.Beta; +import java.io.Serial; /** * Thrown when user schema for supplied binding class is available in present schema context, but @@ -16,6 +17,7 @@ import com.google.common.annotations.Beta; */ @Beta public class MissingClassInLoadingStrategyException extends MissingSchemaException { + @Serial private static final long serialVersionUID = 1L; public MissingClassInLoadingStrategyException(final String msg, final Throwable cause) { diff --git a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaException.java b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaException.java index 911bd5d4b5..402379c441 100644 --- a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaException.java +++ b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaException.java @@ -8,12 +8,14 @@ package org.opendaylight.mdsal.binding.dom.codec.api; import com.google.common.annotations.Beta; +import java.io.Serial; /** * Thrown when codec was used with data which are not modeled and available in schema used by codec. */ @Beta public class MissingSchemaException extends IllegalArgumentException { + @Serial private static final long serialVersionUID = 1L; public MissingSchemaException(final String msg) { diff --git a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaForClassException.java b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaForClassException.java index 31d8967571..f8707fde09 100644 --- a/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaForClassException.java +++ b/binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/MissingSchemaForClassException.java @@ -11,6 +11,7 @@ import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.Serial; import org.eclipse.jdt.annotation.Nullable; /** @@ -21,6 +22,7 @@ import org.eclipse.jdt.annotation.Nullable; */ @Beta public final class MissingSchemaForClassException extends MissingSchemaException { + @Serial private static final long serialVersionUID = 1L; @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Documented in API contract") @@ -28,7 +30,7 @@ public final class MissingSchemaForClassException extends MissingSchemaException public MissingSchemaForClassException(final Class clz) { super(String.format("Schema is not available for %s", clz)); - this.bindingClass = requireNonNull(clz); + bindingClass = requireNonNull(clz); } public @Nullable Class getBindingClass() { diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AlphabeticallyTypeMemberComparator.java b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AlphabeticallyTypeMemberComparator.java index a3f8c86174..e0fd371187 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AlphabeticallyTypeMemberComparator.java +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AlphabeticallyTypeMemberComparator.java @@ -7,6 +7,7 @@ */ package org.opendaylight.mdsal.binding.java.api.generator; +import java.io.Serial; import java.io.Serializable; import java.util.Comparator; import org.opendaylight.mdsal.binding.model.api.TypeMember; @@ -18,6 +19,7 @@ import org.opendaylight.mdsal.binding.model.api.TypeMember; * @param TypeMember type */ public class AlphabeticallyTypeMemberComparator implements Comparator, Serializable { + @Serial private static final long serialVersionUID = 1L; @Override diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ByTypeMemberComparator.java b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ByTypeMemberComparator.java index c28741b860..213bbcbc94 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ByTypeMemberComparator.java +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ByTypeMemberComparator.java @@ -8,6 +8,7 @@ package org.opendaylight.mdsal.binding.java.api.generator; import com.google.common.annotations.Beta; +import java.io.Serial; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; @@ -36,6 +37,7 @@ import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; */ @Beta final class ByTypeMemberComparator implements Comparator, Serializable { + @Serial private static final long serialVersionUID = 1L; /** @@ -110,6 +112,7 @@ final class ByTypeMemberComparator implements Comparator localNameComponents() { - final List ret = new ArrayList<>(); + final var ret = new ArrayList(); ret.add(simpleName()); return ret; } @@ -169,6 +173,7 @@ public abstract sealed class JavaTypeName implements Identifier, Immutable { } private static final class Nested extends Reference { + @Serial private static final long serialVersionUID = 1L; private final Reference immediatelyEnclosingClass; @@ -222,6 +227,7 @@ public abstract sealed class JavaTypeName implements Identifier, Immutable { } private static final Logger LOG = LoggerFactory.getLogger(JavaTypeName.class); + @Serial private static final long serialVersionUID = 1L; private final String simpleName; diff --git a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/NodeKey.java b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/NodeKey.java index cfe9e773e6..e0cdc74bc3 100644 --- a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/NodeKey.java +++ b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/util/NodeKey.java @@ -7,9 +7,11 @@ */ package org.opendaylight.mdsal.binding.spec.util; +import java.io.Serial; import org.opendaylight.yangtools.yang.binding.Identifier; public class NodeKey implements Identifier { + @Serial private static final long serialVersionUID = 1L; private final int id; @@ -29,19 +31,6 @@ public class NodeKey implements Identifier { @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - NodeKey other = (NodeKey) obj; - if (id != other.id) { - return false; - } - return true; + return this == obj || obj != null && getClass() == obj.getClass() && id == ((NodeKey) obj).id; } } diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java index 216ec21fc4..48b199d678 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java @@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.ObjectStreamException; +import java.io.Serial; import java.io.Serializable; import java.util.Collections; import java.util.Iterator; @@ -63,6 +64,7 @@ import org.opendaylight.yangtools.util.HashCodeBuilder; */ public class InstanceIdentifier implements HierarchicalIdentifier> { + @Serial private static final long serialVersionUID = 3L; /* @@ -661,6 +663,7 @@ public class InstanceIdentifier } private abstract static class AbstractPathArgument implements PathArgument, Serializable { + @Serial private static final long serialVersionUID = 1L; private final @NonNull Class type; @@ -722,6 +725,7 @@ public class InstanceIdentifier * @param Item type */ public static class Item extends AbstractPathArgument { + @Serial private static final long serialVersionUID = 1L; Item(final Class type) { @@ -771,6 +775,7 @@ public class InstanceIdentifier */ public static class IdentifiableItem & DataObject, T extends Identifier> extends AbstractPathArgument { + @Serial private static final long serialVersionUID = 1L; private final @NonNull T key; @@ -831,6 +836,7 @@ public class InstanceIdentifier private static final class CaseItem & DataObject, T extends ChildOf> extends Item { + @Serial private static final long serialVersionUID = 1L; private final Class caseType; @@ -848,6 +854,7 @@ public class InstanceIdentifier private static final class CaseIdentifiableItem & DataObject, T extends ChildOf & Identifiable, K extends Identifier> extends IdentifiableItem { + @Serial private static final long serialVersionUID = 1L; private final Class caseType; @@ -953,6 +960,7 @@ public class InstanceIdentifier @NonNull InstanceIdentifier build(); } + @Serial private Object writeReplace() throws ObjectStreamException { return new InstanceIdentifierV3<>(this); } diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifierV3.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifierV3.java index 7f6ce11e55..80fcf14441 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifierV3.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifierV3.java @@ -14,12 +14,14 @@ import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.ObjectStreamException; +import java.io.Serial; import java.util.ArrayList; import java.util.List; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; class InstanceIdentifierV3 implements Externalizable { + @Serial private static final long serialVersionUID = 3L; private @Nullable Iterable pathArguments; @@ -80,6 +82,7 @@ class InstanceIdentifierV3 implements Externalizable { pathArguments = ImmutableList.copyOf(args); } + @Serial Object readResolve() throws ObjectStreamException { return new InstanceIdentifier<>(targetType, pathArguments, wildcarded, hash); } diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifier.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifier.java index 38b61c39ac..eef3d0ad67 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifier.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifier.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.yang.binding; import java.io.ObjectStreamException; +import java.io.Serial; import org.eclipse.jdt.annotation.NonNull; /** @@ -18,7 +19,9 @@ import org.eclipse.jdt.annotation.NonNull; */ public class KeyedInstanceIdentifier & DataObject, K extends Identifier> extends InstanceIdentifier { + @Serial private static final long serialVersionUID = 2L; + private final K key; KeyedInstanceIdentifier(final Class<@NonNull T> type, final Iterable pathArguments, @@ -55,6 +58,7 @@ public class KeyedInstanceIdentifier & DataObject, K e return key == null != (kii.key == null); } + @Serial private Object writeReplace() throws ObjectStreamException { return new KeyedInstanceIdentifierV2<>(this); } diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifierV2.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifierV2.java index a7f2546dd8..330a0f220b 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifierV2.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedInstanceIdentifierV2.java @@ -11,10 +11,12 @@ import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.ObjectStreamException; +import java.io.Serial; import org.eclipse.jdt.annotation.Nullable; final class KeyedInstanceIdentifierV2 & DataObject, K extends Identifier> extends InstanceIdentifierV3 { + @Serial private static final long serialVersionUID = 2L; private @Nullable K key; @@ -41,6 +43,7 @@ final class KeyedInstanceIdentifierV2 & DataObject, K key = (K) in.readObject(); } + @Serial @Override Object readResolve() throws ObjectStreamException { return new KeyedInstanceIdentifier<>(getTargetType(), getPathArguments(), isWildcarded(), getHash(), key); diff --git a/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeChildKey.java b/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeChildKey.java index c98b3eb221..cb44a6af1d 100644 --- a/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeChildKey.java +++ b/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeChildKey.java @@ -7,9 +7,11 @@ */ package org.opendaylight.yangtools.yang.binding.test.mock; +import java.io.Serial; import org.opendaylight.yangtools.yang.binding.Identifier; public class NodeChildKey implements Identifier { + @Serial private static final long serialVersionUID = 1L; private final int id; @@ -29,19 +31,6 @@ public class NodeChildKey implements Identifier { @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - NodeChildKey other = (NodeChildKey) obj; - if (id != other.id) { - return false; - } - return true; + return this == obj || obj != null && getClass() == obj.getClass() && id == ((NodeChildKey) obj).id; } } diff --git a/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeKey.java b/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeKey.java index dc61915d13..cbe3701f54 100644 --- a/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeKey.java +++ b/binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/test/mock/NodeKey.java @@ -7,9 +7,11 @@ */ package org.opendaylight.yangtools.yang.binding.test.mock; +import java.io.Serial; import org.opendaylight.yangtools.yang.binding.Identifier; public class NodeKey implements Identifier { + @Serial private static final long serialVersionUID = 1L; private final int id; @@ -29,19 +31,6 @@ public class NodeKey implements Identifier { @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - NodeKey other = (NodeKey) obj; - if (id != other.id) { - return false; - } - return true; + return this == obj || obj != null && getClass() == obj.getClass() && id == ((NodeKey) obj).id; } } -- 2.36.6