From b0fa6a8ad00b48326aa1b9a746e01c079830c7ba Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 30 May 2023 23:56:42 +0200 Subject: [PATCH] Do not leak SuppressFBWarnings from UnresolvedQName Encapsulate SpotBugs-sensitive operations in a non-public class so that we do not leak that annotation to the outside world. Change-Id: I82b3b2795c68416fe7e4c4e1eae707358daefae3 Signed-off-by: Robert Varga --- common/yang-common/pom.xml | 4 +++ .../yang/common/DoNotLeakSpotbugs.java | 28 +++++++++++++++++++ .../yang/common/UnresolvedQName.java | 13 +++------ 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/DoNotLeakSpotbugs.java diff --git a/common/yang-common/pom.xml b/common/yang-common/pom.xml index 2e25d617d2..ecf32d6712 100644 --- a/common/yang-common/pom.xml +++ b/common/yang-common/pom.xml @@ -31,6 +31,10 @@ spotbugs-annotations true + + org.eclipse.jdt + org.eclipse.jdt.annotation + org.opendaylight.yangtools concepts diff --git a/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/DoNotLeakSpotbugs.java b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/DoNotLeakSpotbugs.java new file mode 100644 index 0000000000..39ff7db10a --- /dev/null +++ b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/DoNotLeakSpotbugs.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.common; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +/** + * Utility methods to hide operations that incur SpotBugs wrath used by public classes where we do not want to leak + * {@link SuppressFBWarnings}. + */ +final class DoNotLeakSpotbugs { + private DoNotLeakSpotbugs() { + // Hidden on purpose + } + + @SuppressFBWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ", justification = "Interning identity check") + static @Nullable String internedString(final @NonNull String str) { + final var interned = str.intern(); + return interned == str ? null : interned; + } +} diff --git a/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnresolvedQName.java b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnresolvedQName.java index 73452c0223..0782626a7d 100644 --- a/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnresolvedQName.java +++ b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnresolvedQName.java @@ -12,7 +12,6 @@ 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; @@ -82,12 +81,10 @@ public abstract sealed class UnresolvedQName extends AbstractQName { } @Override - @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check") public Qualified intern() { // Make sure to intern the string and check whether it refers to the same name as we are - final String name = getLocalName(); - final String internedName = name.intern(); - final Qualified template = internedName == name ? this : new Qualified(prefix.intern(), internedName); + final String internedName = DoNotLeakSpotbugs.internedString(getLocalName()); + final Qualified template = internedName == null ? this : new Qualified(prefix.intern(), internedName); return INTERNER.intern(template); } @@ -172,12 +169,10 @@ public abstract sealed class UnresolvedQName extends AbstractQName { } @Override - @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check") public Unqualified intern() { // Make sure to intern the string and check whether it refers to the same name as we are - final String name = getLocalName(); - final String internedName = name.intern(); - final Unqualified template = internedName == name ? this : new Unqualified(internedName); + final var internedName = DoNotLeakSpotbugs.internedString(getLocalName()); + final var template = internedName == null ? this : new Unqualified(internedName); return INTERNER.intern(template); } -- 2.36.6