Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1036] Analyze project classes only once #52

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
{
Map<Artifact, Set<String>> artifactClassMap = buildArtifactClassMap( project );

Set<String> dependencyClasses = buildDependencyClasses( project );
Set<String> mainDependencyClasses = buildMainDependencyClasses( project );
Set<String> testDependencyClasses = buildTestDependencyClasses( project );

Set<String> testOnlyDependencyClasses = buildTestDependencyClasses( project );
Set<String> dependencyClasses = new HashSet<>();
dependencyClasses.addAll( mainDependencyClasses );
dependencyClasses.addAll( testDependencyClasses );

Set<Artifact> declaredArtifacts = buildDeclaredArtifacts( project );
Set<String> testOnlyDependencyClasses = buildTestOnlyDependencyClasses( mainDependencyClasses,
testDependencyClasses );

Map<Artifact, Set<String>> usedArtifacts = buildUsedArtifacts( artifactClassMap, dependencyClasses );
Set<Artifact> mainUsedArtifacts = buildUsedArtifacts( artifactClassMap, mainDependencyClasses ).keySet();

Set<Artifact> testArtifacts = buildUsedArtifacts( artifactClassMap, testOnlyDependencyClasses ).keySet();
Set<Artifact> testOnlyArtifacts = removeAll( testArtifacts, mainUsedArtifacts );

Set<Artifact> declaredArtifacts = buildDeclaredArtifacts( project );
Set<Artifact> usedDeclaredArtifacts = new LinkedHashSet<>( declaredArtifacts );
usedDeclaredArtifacts.retainAll( usedArtifacts.keySet() );

Expand All @@ -97,7 +101,7 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
Set<Artifact> testArtifactsWithNonTestScope = getTestArtifactsWithNonTestScope( testOnlyArtifacts );

return new ProjectDependencyAnalysis( usedDeclaredArtifacts, usedUndeclaredArtifactsWithClasses,
unusedDeclaredArtifacts, testArtifactsWithNonTestScope );
unusedDeclaredArtifacts, testArtifactsWithNonTestScope );
}
catch ( IOException exception )
{
Expand All @@ -113,7 +117,7 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
* @param remove set to exclude
* @return set with remove excluded
*/
private Set<Artifact> removeAll( Set<Artifact> start, Set<Artifact> remove )
private static Set<Artifact> removeAll( Set<Artifact> start, Set<Artifact> remove )
{
Set<Artifact> results = new LinkedHashSet<>( start.size() );

Expand All @@ -139,7 +143,7 @@ private Set<Artifact> removeAll( Set<Artifact> start, Set<Artifact> remove )
return results;
}

private Set<Artifact> getTestArtifactsWithNonTestScope( Set<Artifact> testOnlyArtifacts )
private static Set<Artifact> getTestArtifactsWithNonTestScope( Set<Artifact> testOnlyArtifacts )
{
Set<Artifact> nonTestScopeArtifacts = new LinkedHashSet<>();

Expand Down Expand Up @@ -201,48 +205,26 @@ else if ( file != null && file.isDirectory() )
return artifactClassMap;
}

private Set<String> buildTestDependencyClasses( MavenProject project ) throws IOException
private static 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 );
}
}

Set<String> testOnlyDependencyClasses = new HashSet<>( testDependencyClasses );
testOnlyDependencyClasses.removeAll( mainDependencyClasses );
return testOnlyDependencyClasses;
}

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 )
Expand All @@ -253,7 +235,7 @@ private Set<String> buildDependencyClasses( String path )
return dependencyAnalyzer.analyze( url );
}

private Set<Artifact> buildDeclaredArtifacts( MavenProject project )
private static Set<Artifact> buildDeclaredArtifacts( MavenProject project )
{
Set<Artifact> declaredArtifacts = project.getDependencyArtifacts();

Expand All @@ -265,8 +247,8 @@ private Set<Artifact> buildDeclaredArtifacts( MavenProject project )
return declaredArtifacts;
}

private Map<Artifact, Set<String>> buildUsedArtifacts( Map<Artifact, Set<String>> artifactClassMap,
Set<String> dependencyClasses )
private static Map<Artifact, Set<String>> buildUsedArtifacts( Map<Artifact, Set<String>> artifactClassMap,
Set<String> dependencyClasses )
{
Map<Artifact, Set<String>> usedArtifacts = new HashMap<>();

Expand All @@ -289,7 +271,7 @@ private Map<Artifact, Set<String>> buildUsedArtifacts( Map<Artifact, Set<String>
return usedArtifacts;
}

private Artifact findArtifactForClassName( Map<Artifact, Set<String>> artifactClassMap, String className )
private static Artifact findArtifactForClassName( Map<Artifact, Set<String>> artifactClassMap, String className )
{
for ( Map.Entry<Artifact, Set<String>> entry : artifactClassMap.entrySet() )
{
Expand Down