Do not leak SuppressFBWarnings from UnresolvedQName 99/106299/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 30 May 2023 21:56:42 +0000 (23:56 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 30 May 2023 22:43:36 +0000 (00:43 +0200)
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 <robert.varga@pantheon.tech>
common/yang-common/pom.xml
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/DoNotLeakSpotbugs.java [new file with mode: 0644]
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnresolvedQName.java

index 2e25d617d2bc27b63beaf59f0746ad403327b844..ecf32d6712772811e6dcaf491dbebfc27b646ada 100644 (file)
             <artifactId>spotbugs-annotations</artifactId>
             <optional>true</optional>
         </dependency>
+        <dependency>
+            <groupId>org.eclipse.jdt</groupId>
+            <artifactId>org.eclipse.jdt.annotation</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>concepts</artifactId>
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 (file)
index 0000000..39ff7db
--- /dev/null
@@ -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;
+    }
+}
index 73452c02233bda04d9c4846b71c40fd98db5b6b4..0782626a7d5b5ecd725f678b18a2ca71cb5f2be7 100644 (file)
@@ -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);
         }