Verify map lookups 66/99766/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 15 Feb 2022 21:36:56 +0000 (22:36 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 15 Feb 2022 21:38:33 +0000 (22:38 +0100)
SpotBugs is pointing out a number of unsafe lookups which could (in
theory) lead to a NPE. In reality they cannot, as they are pre-filled.
Add verifyNotNull() guards to keep SpotBugs happy.

Change-Id: I7d342709aba03fd51d0a27c1676cba8a89fc5374
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/BuildGlobalContext.java
parser/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/reactor/CustomCrossSourceStatementReactorBuilder.java

index 593779e6229f2c57fddc1dc3e60121af1d173250..bca4ae6d7f6d3ca7db25a78784ac843af47297ff 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
 import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.Verify;
@@ -50,7 +51,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableExce
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
@@ -94,7 +94,8 @@ final class BuildGlobalContext extends NamespaceStorageSupport implements Regist
 
         addToNamespace(ValidationBundlesNamespace.class, supportedValidation);
 
-        this.supportedVersions = ImmutableSet.copyOf(supports.get(ModelProcessingPhase.INIT).getSupportedVersions());
+        supportedVersions = ImmutableSet.copyOf(
+            verifyNotNull(supports.get(ModelProcessingPhase.INIT)).getSupportedVersions());
     }
 
     StatementSupportBundle getSupportsForPhase(final ModelProcessingPhase phase) {
@@ -140,7 +141,7 @@ final class BuildGlobalContext extends NamespaceStorageSupport implements Regist
             final Class<N> type) {
         NamespaceBehaviourWithListeners<?, ?, ?> potential = supportedNamespaces.get(type);
         if (potential == null) {
-            final NamespaceBehaviour<K, V, N> potentialRaw = supports.get(currentPhase).getNamespaceBehaviour(type);
+            final var potentialRaw = verifyNotNull(supports.get(currentPhase)).getNamespaceBehaviour(type);
             if (potentialRaw != null) {
                 potential = createNamespaceContext(potentialRaw);
                 supportedNamespaces.put(type, potential);
@@ -174,8 +175,7 @@ final class BuildGlobalContext extends NamespaceStorageSupport implements Regist
     StatementDefinitionContext<?, ?, ?> getStatementDefinition(final YangVersion version, final QName name) {
         StatementDefinitionContext<?, ?, ?> potential = definitions.get(version, name);
         if (potential == null) {
-            final StatementSupport<?, ?, ?> potentialRaw = supports.get(currentPhase).getStatementDefinition(version,
-                    name);
+            final var potentialRaw = verifyNotNull(supports.get(currentPhase)).getStatementDefinition(version, name);
             if (potentialRaw != null) {
                 potential = new StatementDefinitionContext<>(potentialRaw);
                 definitions.put(version, name, potential);
index 940cadf4ed8c842537a8b3bd120fb7cb90464688..637770b020aef197150aa788fe5a98f2da74d2a2 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.reactor;
 
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.annotations.Beta;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Table;
@@ -52,19 +54,19 @@ public class CustomCrossSourceStatementReactorBuilder implements Mutable {
 
     public @NonNull CustomCrossSourceStatementReactorBuilder addStatementSupport(final ModelProcessingPhase phase,
             final StatementSupport<?, ?, ?> stmtSupport) {
-        reactorSupportBundles.get(phase).addSupport(stmtSupport);
+        getBuilder(phase).addSupport(stmtSupport);
         return this;
     }
 
     public @NonNull CustomCrossSourceStatementReactorBuilder overrideStatementSupport(final ModelProcessingPhase phase,
             final StatementSupport<?, ?, ?> stmtSupport) {
-        reactorSupportBundles.get(phase).overrideSupport(stmtSupport);
+        getBuilder(phase).overrideSupport(stmtSupport);
         return this;
     }
 
     public @NonNull CustomCrossSourceStatementReactorBuilder addNamespaceSupport(final ModelProcessingPhase phase,
             final NamespaceBehaviour<?, ?, ?> namespaceSupport) {
-        reactorSupportBundles.get(phase).addSupport(namespaceSupport);
+        getBuilder(phase).addSupport(namespaceSupport);
         return this;
     }
 
@@ -116,17 +118,17 @@ public class CustomCrossSourceStatementReactorBuilder implements Mutable {
      * @return A CrossSourceStatementReactor
      */
     public @NonNull CrossSourceStatementReactor build() {
-        final StatementSupportBundle initBundle = reactorSupportBundles.get(ModelProcessingPhase.INIT).build();
-        final StatementSupportBundle preLinkageBundle = reactorSupportBundles
-                .get(ModelProcessingPhase.SOURCE_PRE_LINKAGE).setParent(initBundle).build();
-        final StatementSupportBundle linkageBundle = reactorSupportBundles.get(ModelProcessingPhase.SOURCE_LINKAGE)
-                .setParent(preLinkageBundle).build();
-        final StatementSupportBundle stmtDefBundle = reactorSupportBundles
-                .get(ModelProcessingPhase.STATEMENT_DEFINITION).setParent(linkageBundle).build();
-        final StatementSupportBundle fullDeclBundle = reactorSupportBundles.get(ModelProcessingPhase.FULL_DECLARATION)
-                .setParent(stmtDefBundle).build();
-        final StatementSupportBundle effectiveBundle = reactorSupportBundles.get(ModelProcessingPhase.EFFECTIVE_MODEL)
-                .setParent(fullDeclBundle).build();
+        final StatementSupportBundle initBundle = getBuilder(ModelProcessingPhase.INIT).build();
+        final StatementSupportBundle preLinkageBundle = getBuilder(ModelProcessingPhase.SOURCE_PRE_LINKAGE)
+            .setParent(initBundle).build();
+        final StatementSupportBundle linkageBundle = getBuilder(ModelProcessingPhase.SOURCE_LINKAGE)
+            .setParent(preLinkageBundle).build();
+        final StatementSupportBundle stmtDefBundle = getBuilder(ModelProcessingPhase.STATEMENT_DEFINITION)
+            .setParent(linkageBundle).build();
+        final StatementSupportBundle fullDeclBundle = getBuilder(ModelProcessingPhase.FULL_DECLARATION)
+            .setParent(stmtDefBundle).build();
+        final StatementSupportBundle effectiveBundle = getBuilder(ModelProcessingPhase.EFFECTIVE_MODEL)
+            .setParent(fullDeclBundle).build();
 
         final CrossSourceStatementReactor.Builder reactorBuilder = CrossSourceStatementReactor.builder()
                 .setBundle(ModelProcessingPhase.INIT, initBundle)
@@ -142,4 +144,8 @@ public class CustomCrossSourceStatementReactorBuilder implements Mutable {
 
         return reactorBuilder.build();
     }
+
+    private StatementSupportBundle.@NonNull Builder getBuilder(final ModelProcessingPhase phase) {
+        return verifyNotNull(reactorSupportBundles.get(phase));
+    }
 }