Do not leak SpotBugs from yang-binding 28/109428/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Dec 2023 23:52:34 +0000 (00:52 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Dec 2023 23:57:25 +0000 (00:57 +0100)
We do not want to pollute user-visible classes with @SuppressFBWarnings,
as that is a purely-internal thing.

Isolate the single method requiring suppression into its own
package-private class.

Change-Id: I200ef79c512063f531f1dd18ed0148439c850563
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/CodeHelpers.java
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/DoNotLeakSpotbugs.java [new file with mode: 0644]

index a62a586c690ba419c3713d6fdb0684788e5def77..1a1870efc44bbe69f0a2bcbd08b98c76bdefba2f 100644 (file)
@@ -15,9 +15,7 @@ import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.HexFormat;
 import java.util.List;
 import java.util.Map;
@@ -32,8 +30,6 @@ import org.eclipse.jdt.annotation.Nullable;
  * Helper methods for generated binding code. This class concentrates useful primitives generated code may call
  * to perform specific shared functions. This allows for generated classes to be leaner. Methods in this class follows
  * general API stability requirements of the Binding Specification.
- *
- * @author Robert Varga
  */
 public final class CodeHelpers {
     private CodeHelpers() {
@@ -460,7 +456,7 @@ public final class CodeHelpers {
     @SuppressWarnings("unchecked")
     public static <T> @Nullable List<T> checkListFieldCast(final @NonNull Class<?> requiredClass,
             final @NonNull String fieldName, final @Nullable List<?> list) {
-        checkCollectionField(requiredClass, fieldName, list);
+        DoNotLeakSpotbugs.checkCollectionField(requiredClass, fieldName, list);
         return (List<T>) list;
     }
 
@@ -477,21 +473,7 @@ public final class CodeHelpers {
     @SuppressWarnings("unchecked")
     public static <T> @Nullable Set<T> checkSetFieldCast(final @NonNull Class<?> requiredClass,
             final @NonNull String fieldName, final @Nullable Set<?> set) {
-        checkCollectionField(requiredClass, fieldName, set);
+        DoNotLeakSpotbugs.checkCollectionField(requiredClass, fieldName, set);
         return (Set<T>) set;
     }
-
-    @SuppressFBWarnings(value = "DCN_NULLPOINTER_EXCEPTION",
-        justification = "Internal NPE->IAE conversion")
-    private static void checkCollectionField(final @NonNull Class<?> requiredClass,
-            final @NonNull String fieldName, final @Nullable Collection<?> collection) {
-        if (collection != null) {
-            try {
-                collection.forEach(item -> requiredClass.cast(requireNonNull(item)));
-            } catch (ClassCastException | NullPointerException e) {
-                throw new IllegalArgumentException("Invalid input item for property \"" + requireNonNull(fieldName)
-                    + "\"", e);
-            }
-        }
-    }
 }
diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/DoNotLeakSpotbugs.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/DoNotLeakSpotbugs.java
new file mode 100644 (file)
index 0000000..f73e30f
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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.binding;
+
+import static java.util.Objects.requireNonNull;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.util.Collection;
+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 = "DCN_NULLPOINTER_EXCEPTION", justification = "Internal NPE->IAE conversion")
+    static void checkCollectionField(final @NonNull Class<?> requiredClass, final @NonNull String fieldName,
+            final @Nullable Collection<?> collection) {
+        if (collection != null) {
+            try {
+                collection.forEach(item -> requiredClass.cast(requireNonNull(item)));
+            } catch (ClassCastException | NullPointerException e) {
+                throw new IllegalArgumentException(
+                    "Invalid input item for property \"" + requireNonNull(fieldName) + "\"", e);
+            }
+        }
+    }
+}