Simplify DEFAULT_PARSER_FACTORY instantition 99/104799/4
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 23:17:54 +0000 (00:17 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 8 Mar 2023 00:32:26 +0000 (01:32 +0100)
Use a plain assignment in a try/catch block. This favors the usual
case of no problem at instantiation.

Change-Id: I04b56aac2feae11078c5ca4bc42aa010cbf0cce0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/FilenameResolutionTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/GenerateSourcesTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessorTest.java

index 5e9067fddd07868762c4960fa81b93ae6f3fa66b..669825f6e2b44cf55b1996269d9fca6a9799ef2e 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -23,11 +22,11 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.NoSuchElementException;
 import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.function.Function;
@@ -57,9 +56,11 @@ class YangToSourcesProcessor {
     private static final YangParserFactory DEFAULT_PARSER_FACTORY;
 
     static {
-        final Iterator<YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
-        checkState(it.hasNext(), "Failed to find a YangParserFactory implementation");
-        DEFAULT_PARSER_FACTORY = it.next();
+        try {
+            DEFAULT_PARSER_FACTORY = ServiceLoader.load(YangParserFactory.class).iterator().next();
+        } catch (NoSuchElementException e) {
+            throw new IllegalStateException("Failed to find a YangParserFactory implementation", e);
+        }
     }
 
     private static final String META_INF_STR = "META-INF";
@@ -123,11 +124,9 @@ class YangToSourcesProcessor {
     }
 
     @VisibleForTesting
-    YangToSourcesProcessor(final File yangFilesRootDir, final Collection<File> excludedFiles,
-            final List<FileGeneratorArg> fileGenerators, final MavenProject project, final boolean inspectDependencies,
-            final YangProvider yangProvider) {
-        this(new DefaultBuildContext(), yangFilesRootDir, excludedFiles, ImmutableList.of(),
-            project, inspectDependencies, yangProvider);
+    YangToSourcesProcessor(final File yangFilesRootDir, final List<FileGeneratorArg> fileGenerators,
+            final MavenProject project, final YangProvider yangProvider) {
+        this(new DefaultBuildContext(), yangFilesRootDir, List.of(), List.of(), project, false, yangProvider);
     }
 
     YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir,
@@ -188,7 +187,7 @@ class YangToSourcesProcessor {
             }
             LOG.info("{} Found {} dependencies in {}", LOG_PREFIX, dependencies.size(), watch);
         } else {
-            dependencies = ImmutableList.of();
+            dependencies = List.of();
         }
 
         /*
index 5a3ae30dc05421772247975472ceeefd942db1d4..7a7a28b830ef5f8f6bb15a87f74c42b54b8d9c95 100644 (file)
@@ -30,8 +30,7 @@ class FilenameResolutionTest extends AbstractCodeGeneratorTest {
     void testResolveSubmoduleResource() throws Exception {
         assertMojoExecution(new YangToSourcesProcessor(
             new File(Resources.getResource(FilenameResolutionTest.class, "/filename").toURI()),
-            List.of(),
-            List.of(new FileGeneratorArg("mockGenerator")), project, false, yangProvider),
+            List.of(new FileGeneratorArg("mockGenerator")), project, yangProvider),
             mock -> {
                 doAnswer(invocation -> {
                     final EffectiveModelContext context = invocation.getArgument(0);
index a1c6f3eebcabfdef9cc2910af4833854fd7d48a9..9a9a5638cf187fa06f8547e7dfcd136329913ed4 100644 (file)
@@ -22,8 +22,7 @@ class GenerateSourcesTest extends AbstractCodeGeneratorTest {
     void test() throws Exception {
         assertMojoExecution(new YangToSourcesProcessor(
             new File(Resources.getResource(GenerateSourcesTest.class, "/yang").toURI()),
-            List.of(),
-            List.of(new FileGeneratorArg("mockGenerator")), project, false, yangProvider),
+            List.of(new FileGeneratorArg("mockGenerator")), project, yangProvider),
             mock -> {
                 doReturn(ImmutableTable.of()).when(mock).generateFiles(any(), any(), any());
             },
index f18efcdcc1c8c7314fc2b9066b62aa1c3a1641ab..1710dbd8b0184c3644eddd9be0713e813a4f9e50 100644 (file)
@@ -17,18 +17,22 @@ import java.util.List;
 import java.util.Set;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.sonatype.plexus.build.incremental.DefaultBuildContext;
+import org.sonatype.plexus.build.incremental.BuildContext;
 
 @ExtendWith(MockitoExtension.class)
 class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
+    @Mock
+    private BuildContext buildContext;
+
     private final File file = new File(getClass().getResource("/yang").getFile());
 
     @Test
     void basicTest() {
         assertMojoExecution(
-            new YangToSourcesProcessor(new DefaultBuildContext(), file, List.of(),
+            new YangToSourcesProcessor(buildContext, file, List.of(),
                 List.of(new FileGeneratorArg("mockGenerator")), project, true),
             mock -> {
                 doAnswer(invocation -> {
@@ -47,7 +51,7 @@ class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
         final var excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
 
         assertMojoExecution(
-            new YangToSourcesProcessor(new DefaultBuildContext(), file, List.of(excludedYang),
+            new YangToSourcesProcessor(buildContext, file, List.of(excludedYang),
                 List.of(new FileGeneratorArg("mockGenerator")), project, true),
             mock -> {
                 doAnswer(invocation -> {