From: Robert Varga Date: Fri, 5 Oct 2018 08:53:50 +0000 (+0200) Subject: Migrate yang-common to use JDT annotations X-Git-Tag: v2.1.2~16 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=7286488bb26fbd175297925978c4f75ecee1b70f;p=yangtools.git Migrate yang-common to use JDT annotations This removes run-time reliance on javax.annotation package. JIRA: YANGTOOLS-907 Change-Id: I9fc87546af3c3a0ee194be68e5cf9278b18b836c Signed-off-by: Robert Varga --- diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Empty.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Empty.java index ee2e691c37..af85eb0547 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Empty.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Empty.java @@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.common; import com.google.common.annotations.Beta; import java.io.Serializable; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.opendaylight.yangtools.concepts.Immutable; /** @@ -17,6 +18,7 @@ import org.opendaylight.yangtools.concepts.Immutable; * @author Robert Varga */ @Beta +@NonNullByDefault public final class Empty implements Immutable, Serializable { private static final long serialVersionUID = 1L; private static final Empty INSTANCE = new Empty(); diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java index 9a272c8f0e..5dc281ef90 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java @@ -23,7 +23,7 @@ import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.RegEx; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.concepts.Identifier; import org.opendaylight.yangtools.concepts.Immutable; @@ -53,6 +53,7 @@ import org.opendaylight.yangtools.concepts.WritableObject; * affect equality and identity of two QNames and carry only information * which may be useful for serializers / deserializers. */ +@NonNullByDefault public final class QName implements Immutable, Serializable, Comparable, Identifier, WritableObject { private static final Interner INTERNER = Interners.newWeakInterner(); private static final long serialVersionUID = 5398411242927766414L; @@ -71,8 +72,8 @@ public final class QName implements Immutable, Serializable, Comparable, private static final char[] ILLEGAL_CHARACTERS = { '?', '(', ')', '&', ':' }; - private final @NonNull QNameModule module; - private final @NonNull String localName; + private final QNameModule module; + private final String localName; private transient int hash = 0; private QName(final QNameModule module, final String localName) { @@ -129,10 +130,8 @@ public final class QName implements Immutable, Serializable, Comparable, /** * Creates new QName. * - * @param qnameModule - * Namespace and revision enclosed as a QNameModule - * @param localName - * Local name part of QName. MUST NOT BE null. + * @param qnameModule Namespace and revision enclosed as a QNameModule + * @param localName Local name part of QName. MUST NOT BE null. * @return Instance of QName */ public static QName create(final QNameModule qnameModule, final String localName) { @@ -142,12 +141,9 @@ public final class QName implements Immutable, Serializable, Comparable, /** * Creates new QName. * - * @param namespace - * Namespace of QName or null if namespace is undefined. - * @param revision - * Revision of namespace or null if revision is unspecified. - * @param localName - * Local name part of QName. MUST NOT BE null. + * @param namespace Namespace of QName or null if namespace is undefined. + * @param revision Revision of namespace or null if revision is unspecified. + * @param localName Local name part of QName. MUST NOT BE null. * @return Instance of QName */ public static QName create(final URI namespace, final @Nullable Revision revision, final String localName) { @@ -157,12 +153,9 @@ public final class QName implements Immutable, Serializable, Comparable, /** * Creates new QName. * - * @param namespace - * Namespace of QName or null if namespace is undefined. - * @param revision - * Revision of namespace. - * @param localName - * Local name part of QName. MUST NOT BE null. + * @param namespace Namespace of QName or null if namespace is undefined. + * @param revision Revision of namespace. + * @param localName Local name part of QName. MUST NOT BE null. * @return Instance of QName */ public static QName create(final URI namespace, final Optional revision, final String localName) { @@ -212,15 +205,11 @@ public final class QName implements Immutable, Serializable, Comparable, /** * Creates new QName. * - * @param namespace - * Namespace of QName, MUST NOT BE Null. - * @param localName - * Local name part of QName. MUST NOT BE null. + * @param namespace Namespace of QName, MUST NOT BE Null. + * @param localName Local name part of QName. MUST NOT BE null. * @return A new QName - * @throws NullPointerException - * If any of parameters is null. - * @throws IllegalArgumentException - * If namespace is not valid URI. + * @throws NullPointerException If any of parameters is null. + * @throws IllegalArgumentException If namespace is not valid URI. */ public static QName create(final String namespace, final String localName) { return create(parseNamespace(namespace), localName); @@ -229,15 +218,11 @@ public final class QName implements Immutable, Serializable, Comparable, /** * Creates new QName. * - * @param namespace - * Namespace of QName, MUST NOT BE null. - * @param localName - * Local name part of QName. MUST NOT BE null. + * @param namespace Namespace of QName, MUST NOT BE null. + * @param localName Local name part of QName. MUST NOT BE null. * @return A new QName - * @throws NullPointerException - * If any of parameters is null. - * @throws IllegalArgumentException - * If namespace is not valid URI. + * @throws NullPointerException If any of parameters is null. + * @throws IllegalArgumentException If namespace is not valid URI. */ public static QName create(final URI namespace, final String localName) { return new QName(namespace, localName); @@ -329,7 +314,7 @@ public final class QName implements Immutable, Serializable, Comparable, * */ @Override - public boolean equals(final Object obj) { + public boolean equals(final @Nullable Object obj) { if (this == obj) { return true; } @@ -370,13 +355,13 @@ public final class QName implements Immutable, Serializable, Comparable, * @param newModule New QNameModule to use * @return a QName with specified QNameModule and same local name as this one */ - public QName withModule(@NonNull final QNameModule newModule) { + public QName withModule(final QNameModule newModule) { return new QName(newModule, localName); } /** * Returns a QName with the same namespace and local name, but with no revision. If this QName does not have - * a Revision, this object is retured. + * a Revision, this object is returned. * * @return a QName with the same namespace and local name, but with no revision. */ diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java index 8b3db1cf82..92526280b8 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java @@ -12,6 +12,7 @@ import static java.util.Objects.requireNonNull; import com.google.common.base.MoreObjects; import com.google.common.collect.Interner; import com.google.common.collect.Interners; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; @@ -20,24 +21,23 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Objects; import java.util.Optional; -import javax.annotation.Nullable; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.concepts.Identifier; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.concepts.WritableObject; +@NonNullByDefault public final class QNameModule implements Comparable, Immutable, Serializable, Identifier, WritableObject { private static final Interner INTERNER = Interners.newWeakInterner(); private static final long serialVersionUID = 3L; - private final @NonNull URI namespace; - - //Nullable - private final Revision revision; + private final URI namespace; + private final @Nullable Revision revision; private transient int hash = 0; - private QNameModule(final @NonNull URI namespace, final Revision revision) { + private QNameModule(final URI namespace, final @Nullable Revision revision) { this.namespace = requireNonNull(namespace); this.revision = revision; } @@ -59,7 +59,7 @@ public final class QNameModule implements Comparable, Immutable, Se * @return A new, potentially shared, QNameModule instance */ public static QNameModule create(final URI namespace, final Optional revision) { - return new QNameModule(namespace, revision.orElse(null)); + return new QNameModule(namespace, revision.isPresent() ? revision.get() : null); } /** @@ -68,6 +68,7 @@ public final class QNameModule implements Comparable, Immutable, Se * @param namespace Module namespace * @return A new, potentially shared, QNameModule instance */ + @SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL") public static QNameModule create(final URI namespace) { return new QNameModule(namespace, null); } @@ -114,7 +115,8 @@ public final class QNameModule implements Comparable, Immutable, Se * YANG Module revison keyword */ public Optional getRevision() { - return Optional.ofNullable(revision); + final Revision rev = revision; + return rev == null ? Optional.empty() : Optional.of(rev); } @Override @@ -133,6 +135,7 @@ public final class QNameModule implements Comparable, Immutable, Se * * @return a QNameModule with the same namespace, but with no revision. */ + @SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL") public QNameModule withoutRevision() { return revision == null ? this : new QNameModule(namespace, null); } @@ -152,7 +155,7 @@ public final class QNameModule implements Comparable, Immutable, Se } @Override - public boolean equals(final Object obj) { + public boolean equals(final @Nullable Object obj) { if (this == obj) { return true; } diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Revision.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Revision.java index d0b4233400..32c6f50e5d 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Revision.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Revision.java @@ -18,9 +18,9 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Optional; import java.util.regex.Pattern; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.annotation.RegEx; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; /** * Dedicated object identifying a YANG module revision. @@ -53,9 +53,9 @@ public final class Revision implements Comparable, Serializable { private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - private final String str; + private final @NonNull String str; - private Revision(final String str) { + private Revision(final @NonNull String str) { /* * According to RFC7950 (https://tools.ietf.org/html/rfc7950#section-7.1.9): * @@ -78,7 +78,7 @@ public final class Revision implements Comparable, Serializable { * @throws DateTimeParseException if the string format does not conform specification. * @throws NullPointerException if the string is null */ - public static Revision of(@Nonnull final String str) { + public static @NonNull Revision of(final @NonNull String str) { return new Revision(str); } @@ -89,7 +89,7 @@ public final class Revision implements Comparable, Serializable { * @return An optional Revision instance. * @throws DateTimeParseException if the string format does not conform specification. */ - public static Optional ofNullable(@Nullable final String str) { + public static Optional ofNullable(final @Nullable String str) { return str == null ? Optional.empty() : Optional.of(new Revision(str)); } @@ -102,7 +102,7 @@ public final class Revision implements Comparable, Serializable { * @param second Second optional revision * @return Positive, zero, or negative integer. */ - public static int compare(final Optional first, final Optional second) { + public static int compare(final @NonNull Optional first, final @NonNull Optional second) { if (first.isPresent()) { return second.isPresent() ? first.get().compareTo(second.get()) : 1; } @@ -117,7 +117,7 @@ public final class Revision implements Comparable, Serializable { * @param second Second revision * @return Positive, zero, or negative integer. */ - public static int compare(@Nullable final Revision first, @Nullable final Revision second) { + public static int compare(final @Nullable Revision first, final @Nullable Revision second) { if (first != null) { return second != null ? first.compareTo(second) : 1; } @@ -176,7 +176,7 @@ public final class Revision implements Comparable, Serializable { } private Object readResolve() { - return Revision.of(str); + return Revision.of(requireNonNull(str)); } } } diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangConstants.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangConstants.java index eefd658750..c4e82b5a19 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangConstants.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangConstants.java @@ -8,10 +8,12 @@ package org.opendaylight.yangtools.yang.common; import java.net.URI; +import org.eclipse.jdt.annotation.NonNullByDefault; /** * Constant definitions present in RFC documents related to the YANG language. */ +@NonNullByDefault public final class YangConstants { /** * YANG File Extension, as defined in https://tools.ietf.org/html/rfc6020#section-14.1. @@ -99,6 +101,7 @@ public final class YangConstants { * * @param module Desired module * @return A QName representing action or RPC input. + * @throws NullPointerException if {@code module} is null */ public static QName operationInputQName(final QNameModule module) { return DUMMY_OPERATION_INPUT.withModule(module); @@ -110,6 +113,7 @@ public final class YangConstants { * * @param module Desired module * @return A QName representing action or RPC output. + * @throws NullPointerException if {@code module} is null */ public static QName operationOutputQName(final QNameModule module) { return DUMMY_OPERATION_OUTPUT.withModule(module); diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangNames.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangNames.java index 1e27ee3378..0830ef6dce 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangNames.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangNames.java @@ -10,6 +10,8 @@ package org.opendaylight.yangtools.yang.common; import com.google.common.annotations.Beta; import java.util.AbstractMap.SimpleEntry; import java.util.Map.Entry; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; /** * Utility class for handling various naming conventions mentioned in YANG and related specifications. @@ -17,6 +19,7 @@ import java.util.Map.Entry; * @author Robert Varga */ @Beta +@NonNullByDefault public final class YangNames { private YangNames() { throw new UnsupportedOperationException(); @@ -28,8 +31,9 @@ public final class YangNames { * * @param baseName file base name * @return A tuple containing the module name and parsed revision, if present. + * @throws NullPointerException if {@code baseName} is null */ - public static Entry parseFilename(final String baseName) { + public static Entry parseFilename(final String baseName) { final int zavinac = baseName.lastIndexOf('@'); if (zavinac < 0) { return new SimpleEntry<>(baseName, null); diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangVersion.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangVersion.java index f25e453d56..6ba5ceec16 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangVersion.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/YangVersion.java @@ -14,7 +14,7 @@ import com.google.common.collect.Maps; import java.util.Arrays; import java.util.Map; import java.util.Optional; -import javax.annotation.Nonnull; +import org.eclipse.jdt.annotation.NonNullByDefault; /** * Enumeration of supported YANG versions. @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; * @author Robert Varga */ @Beta +@NonNullByDefault public enum YangVersion { /** * Version 1, as defined in RFC6020. @@ -50,7 +51,7 @@ public enum YangVersion { * @return YANG version * @throws NullPointerException if the string is null */ - public static Optional parse(@Nonnull final String str) { + public static Optional parse(final String str) { return Optional.ofNullable(YANG_VERSION_MAP.get(requireNonNull(str))); } @@ -59,12 +60,12 @@ public enum YangVersion { * * @return Normative reference. */ - @Nonnull public String getReference() { + public String getReference() { return reference; } @Override - @Nonnull public String toString() { + public String toString() { return str; } }