Clean up AbstractCompositeRuntimeType 55/109355/3
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 16 Dec 2023 13:59:55 +0000 (14:59 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 16 Dec 2023 15:39:50 +0000 (16:39 +0100)
SpotBugs is no longer complaining about the Comparator, hence we can
completely remove the dependency of spotbugs-annotations.

While we are here, also clean up the Arrays.binarySearch() assumption we
make, so we tolerate if the key is reported as the first argument.

Change-Id: I97b9683b64e49617ca529826e37392face70739a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator/pom.xml
binding/mdsal-binding-generator/src/main/java/module-info.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/rt/AbstractCompositeRuntimeType.java

index dfdb448b6e6d8a44ed9e7aef1e224ab2feddb9a3..a44f167917b66731b12737154374e53c22a0fb2b 100644 (file)
     <packaging>bundle</packaging>
 
     <dependencies>
-        <dependency>
-            <groupId>com.github.spotbugs</groupId>
-            <artifactId>spotbugs-annotations</artifactId>
-            <optional>true</optional>
-        </dependency>
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
index 88dd6f8bd17a80734fffc301b74cdc3724dad37c..83b182b1506c760e224496c50dac8c4efbef3d46 100644 (file)
@@ -34,7 +34,6 @@ module org.opendaylight.mdsal.binding.generator {
 
     // Annotations
     requires static transitive org.eclipse.jdt.annotation;
-    requires static com.github.spotbugs.annotations;
     requires static javax.inject;
     requires static org.kohsuke.metainf_services;
     requires static org.osgi.service.component.annotations;
index a806f99a484f4721fd1b2e2c9408c1facb60694b..d144120299330e2f4f01cf91949ac064cb01f57b 100644 (file)
@@ -7,13 +7,12 @@
  */
 package org.opendaylight.mdsal.binding.generator.impl.rt;
 
-import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
+import static java.util.Objects.requireNonNullElse;
 
 import com.google.common.base.Functions;
 import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableMap;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -34,8 +33,6 @@ abstract class AbstractCompositeRuntimeType<S extends EffectiveStatement<?, ?>>
     private final ImmutableMap<JavaTypeName, GeneratedRuntimeType> byClass;
     private final Object bySchemaTree;
 
-    @SuppressFBWarnings(value = "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE",
-        justification = "https://github.com/spotbugs/spotbugs/issues/1985")
     AbstractCompositeRuntimeType(final GeneratedType bindingType, final S statement, final List<RuntimeType> children) {
         super(bindingType, statement);
 
@@ -53,7 +50,9 @@ abstract class AbstractCompositeRuntimeType<S extends EffectiveStatement<?, ?>>
             default -> {
                 Arrays.sort(tmp, (o1, o2) -> {
                     final int cmp = extractQName(o1).compareTo(extractQName(o2));
-                    verify(cmp != 0, "Type %s conflicts with %s on schema tree", o1, o2);
+                    if (cmp == 0) {
+                        throw new VerifyException("Type " + o1 + " conflicts with " + o2 + " on schema tree");
+                    }
                     return cmp;
                 });
                 yield tmp;
@@ -68,15 +67,13 @@ abstract class AbstractCompositeRuntimeType<S extends EffectiveStatement<?, ?>>
         }
 
         final var tmp = (RuntimeType[]) bySchemaTree;
-        @SuppressFBWarnings(value = "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE",
-            justification = "https://github.com/spotbugs/spotbugs/issues/1985")
-        final int offset = Arrays.binarySearch(tmp, null, (o1, o2) -> {
-            // We make assumptions about how Arrays.binarySearch() is implemented: o2 is expected to be the provided
-            // key -- which is null. This helps CHA by not introducing a fake RuntimeType class and the
-            // corresponding instanceof checks.
-            verify(o2 == null, "Unexpected key %s", o2);
-            return extractQName(o1).compareTo(qname);
-        });
+        // Here we are assuming that Arrays.binarySearch() accepts a null object, so as to help CHA by not introducing
+        // a fake RuntimeType class and the corresponding instanceof checks to side-step the statement lookup (which
+        // would need more faking).
+        // We make a slight assumption that o2 is the what we specify as a key, but can recover if it is the other way
+        // around.
+        final int offset = Arrays.binarySearch(tmp, null,
+            (o1, o2) -> extractQName(requireNonNullElse(o1, o2)).compareTo(qname));
         return offset < 0 ? null : tmp[offset];
     }
 
@@ -94,7 +91,9 @@ abstract class AbstractCompositeRuntimeType<S extends EffectiveStatement<?, ?>>
 
         final var tmp = (RuntimeType[]) bySchemaTree;
         for (var item : tmp) {
-            verify(expectedType.isInstance(item), "Unexpected schema tree child %s", item);
+            if (!expectedType.isInstance(item)) {
+                throw new VerifyException("Unexpected schema tree child " + item);
+            }
         }
         return (List<T>) Collections.unmodifiableList(Arrays.asList(tmp));
     }