Clean up yang-maven-util Util 32/104632/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Feb 2023 23:21:39 +0000 (00:21 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Feb 2023 23:22:48 +0000 (00:22 +0100)
Use variable type inference and perform general clean up.

Change-Id: I415d4c91347c7b43eaf3afe6893408241a3504eb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java

index ddfbf452b543a6c873510c27d6480fc3724b8a15..64a999f39217be06a6e61f8ec8af46aa8304819d 100644 (file)
@@ -10,8 +10,6 @@ package org.opendaylight.yangtools.yang2sources.plugin;
 import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.LOG_PREFIX;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -19,11 +17,10 @@ import java.util.Set;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
-import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
-import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.repository.RepositorySystem;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
@@ -31,54 +28,48 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 final class Util {
+    private static final Logger LOG = LoggerFactory.getLogger(Util.class);
 
-    /**
-     * It isn't desirable to create instances of this class.
-     */
     private Util() {
+        // Hidden on purpose
     }
 
-    private static final Logger LOG = LoggerFactory.getLogger(Util.class);
+    static @NonNull SourceIdentifier moduleToIdentifier(final ModuleLike module) {
+        return new SourceIdentifier(Unqualified.of(module.getName()), module.getRevision().orElse(null));
+    }
 
     static List<File> getClassPath(final MavenProject project) {
-        final List<File> dependencies = new ArrayList<>();
-        for (Artifact element : project.getArtifacts()) {
-            File asFile = element.getFile();
-            if (isJar(asFile) || asFile.isDirectory()) {
-                dependencies.add(asFile);
-            }
-        }
-        return dependencies;
+        return project.getArtifacts().stream()
+            .map(Artifact::getFile)
+            .filter(file -> file.isFile() && file.getName().endsWith(".jar") || file.isDirectory())
+            .toList();
     }
 
     /**
      * Read current project dependencies and check if it don't grab incorrect
      * artifacts versions which could be in conflict with plugin dependencies.
      *
-     * @param project
-     *            current project
-     * @param repoSystem
-     *            repository system
-     * @param localRepo
-     *            local repository
-     * @param remoteRepos
-     *            remote repositories
+     * @param project current project
+     * @param repoSystem repository system
+     * @param localRepo local repository
+     * @param remoteRepos remote repositories
      */
     static void checkClasspath(final MavenProject project, final RepositorySystem repoSystem,
             final ArtifactRepository localRepo, final List<ArtifactRepository> remoteRepos) {
-        Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
+        final var plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
         if (plugin == null) {
             LOG.warn("{} {} not found, dependencies version check skipped", LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME);
-        } else {
-            Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
-            getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
+            return;
+        }
+
+        final var pluginDependencies = new HashMap<Artifact, Set<Artifact>>();
+        getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
 
-            Set<Artifact> projectDependencies = project.getDependencyArtifacts();
-            for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
-                checkArtifact(entry.getKey(), projectDependencies);
-                for (Artifact dependency : entry.getValue()) {
-                    checkArtifact(dependency, projectDependencies);
-                }
+        final var projectDependencies = project.getDependencyArtifacts();
+        for (var entry : pluginDependencies.entrySet()) {
+            checkArtifact(entry.getKey(), projectDependencies);
+            for (var dependency : entry.getValue()) {
+                checkArtifact(dependency, projectDependencies);
             }
         }
     }
@@ -86,34 +77,25 @@ final class Util {
     /**
      * Read transitive dependencies of given plugin and store them in map.
      *
-     * @param plugin
-     *            plugin to read
-     * @param map
-     *            map, where founded transitive dependencies will be stored
-     * @param repoSystem
-     *            repository system
-     * @param localRepository
-     *            local repository
-     * @param remoteRepos
-     *            list of remote repositories
+     * @param plugin plugin to read
+     * @param map map, where founded transitive dependencies will be stored
+     * @param repoSystem repository system
+     * @param localRepository local repository
+     * @param remoteRepos list of remote repositories
      */
     private static void getPluginTransitiveDependencies(final Plugin plugin,
-            final Map<Artifact, Collection<Artifact>> map, final RepositorySystem repoSystem,
+            final Map<Artifact, Set<Artifact>> map, final RepositorySystem repoSystem,
             final ArtifactRepository localRepository, final List<ArtifactRepository> remoteRepos) {
+        for (var dep : plugin.getDependencies()) {
+            final var artifact = repoSystem.createDependencyArtifact(dep);
 
-        List<Dependency> pluginDependencies = plugin.getDependencies();
-        for (Dependency dep : pluginDependencies) {
-            Artifact artifact = repoSystem.createDependencyArtifact(dep);
-
-            ArtifactResolutionRequest request = new ArtifactResolutionRequest();
+            final var request = new ArtifactResolutionRequest();
             request.setArtifact(artifact);
             request.setResolveTransitively(true);
             request.setLocalRepository(localRepository);
             request.setRemoteRepositories(remoteRepos);
 
-            ArtifactResolutionResult result = repoSystem.resolve(request);
-            Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
-            map.put(artifact, pluginDependencyDependencies);
+            map.put(artifact, repoSystem.resolve(request).getArtifacts());
         }
     }
 
@@ -127,23 +109,15 @@ final class Util {
      * @param dependencies
      *            collection of dependencies
      */
-    private static void checkArtifact(final Artifact artifact, final Collection<Artifact> dependencies) {
-        for (org.apache.maven.artifact.Artifact d : dependencies) {
-            if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())
-                && !artifact.getVersion().equals(d.getVersion())) {
+    private static void checkArtifact(final Artifact artifact, final Set<Artifact> dependencies) {
+        for (var dep : dependencies) {
+            if (artifact.getGroupId().equals(dep.getGroupId()) && artifact.getArtifactId().equals(dep.getArtifactId())
+                && !artifact.getVersion().equals(dep.getVersion())) {
                 LOG.warn("{} Dependency resolution conflict:", LOG_PREFIX);
                 LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]"
                     + ". It is recommended to fix this problem because it may cause compilation errors.",
-                    LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d);
+                    LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, dep);
             }
         }
     }
-
-    private static boolean isJar(final File element) {
-        return element.isFile() && element.getName().endsWith(".jar");
-    }
-
-    static SourceIdentifier moduleToIdentifier(final ModuleLike module) {
-        return new SourceIdentifier(Unqualified.of(module.getName()), module.getRevision().orElse(null));
-    }
 }