You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/02/21 23:03:49 UTC

[GitHub] [maven-dependency-analyzer] elharo commented on a change in pull request #52: [MSHARED-1036] Analyze project classes only once

elharo commented on a change in pull request #52:
URL: https://github.com/apache/maven-dependency-analyzer/pull/52#discussion_r811469731



##########
File path: src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
##########
@@ -276,12 +254,7 @@ else if ( file != null && file.isDirectory() )
 
             if ( artifact != null )
             {
-                Set<String> classesFromArtifact = usedArtifacts.get( artifact );
-                if ( classesFromArtifact == null )
-                {
-                    classesFromArtifact = new HashSet<String>();
-                    usedArtifacts.put( artifact, classesFromArtifact );
-                }
+                Set<String> classesFromArtifact = usedArtifacts.computeIfAbsent( artifact, k -> new HashSet<>() );

Review comment:
       This one's more borderline, though I still find the old version clearer. 
   
   That said, is anything actually changing here? If this isn't relevant to fixing the issue at hand, then we should try to avoid unnecessary churn that obscures what's actually changing in this PR.  

##########
File path: src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
##########
@@ -33,9 +33,11 @@
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Optional;

Review comment:
       Are we requiring Java 8 these days? 

##########
File path: src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
##########
@@ -201,68 +209,38 @@ else if ( file != null && file.isDirectory() )
         return artifactClassMap;
     }
 
-    private Set<String> buildTestDependencyClasses( MavenProject project ) throws IOException
+    private Set<String> buildTestOnlyDependencyClasses( Set<String> mainDependencyClasses,
+                                                        Set<String> testDependencyClasses )
     {
-        Set<String> testOnlyDependencyClasses = new HashSet<>();
-
-        String outputDirectory = project.getBuild().getOutputDirectory();
-        Set<String> nonTestDependencyClasses = new HashSet<>( buildDependencyClasses( outputDirectory ) );
-
-        String testOutputDirectory = project.getBuild().getTestOutputDirectory();
-        Set<String> testDependencyClasses = new HashSet<>( buildDependencyClasses( testOutputDirectory ) );
-
-        for ( String testString : testDependencyClasses )
-        {
-            if ( !nonTestDependencyClasses.contains( testString ) )
-            {
-                testOnlyDependencyClasses.add( testString );
-            }
-        }
-
-        return testOnlyDependencyClasses;
+        return testDependencyClasses.stream()
+            .filter( klass -> !mainDependencyClasses.contains( klass ) )
+            .collect( Collectors.toSet() );
     }
 
-    private Set<String> buildDependencyClasses( MavenProject project )
+    private Set<String> buildMainDependencyClasses( MavenProject project )
         throws IOException
     {
-
         String outputDirectory = project.getBuild().getOutputDirectory();
-        Set<String> dependencyClasses = new HashSet<>( buildDependencyClasses( outputDirectory ) );
-
-        String testOutputDirectory = project.getBuild().getTestOutputDirectory();
-        dependencyClasses.addAll( buildDependencyClasses( testOutputDirectory ) );
-
-        return dependencyClasses;
+        return buildDependencyClasses( outputDirectory );
     }
 
-    private Set<String> buildMainDependencyClasses( MavenProject project )
+    private Set<String> buildTestDependencyClasses( MavenProject project )
         throws IOException
     {
-
-        String outputDirectory = project.getBuild().getOutputDirectory();
-        Set<String> dependencyClasses = new HashSet<>( buildDependencyClasses( outputDirectory ) );
-
-        return dependencyClasses;
+        String testOutputDirectory = project.getBuild().getTestOutputDirectory();
+        return buildDependencyClasses( testOutputDirectory );
     }
 
     private Set<String> buildDependencyClasses( String path )
         throws IOException
     {
         URL url = new File( path ).toURI().toURL();
-
         return dependencyAnalyzer.analyze( url );
     }
 
     private Set<Artifact> buildDeclaredArtifacts( MavenProject project )
     {
-        Set<Artifact> declaredArtifacts = project.getDependencyArtifacts();
-
-        if ( declaredArtifacts == null )
-        {
-            declaredArtifacts = Collections.emptySet();
-        }
-
-        return declaredArtifacts;
+        return Optional.ofNullable( project.getDependencyArtifacts() ).orElseGet( Collections::emptySet );

Review comment:
       This is less clear and harder to read than the code it replaces. Please revert. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org