@@ -70,19 +70,23 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
7070 {
7171 Map <Artifact , Set <String >> artifactClassMap = buildArtifactClassMap ( project );
7272
73- Set <String > dependencyClasses = buildDependencyClasses ( project );
7473 Set <String > mainDependencyClasses = buildMainDependencyClasses ( project );
74+ Set <String > testDependencyClasses = buildTestDependencyClasses ( project );
7575
76- Set <String > testOnlyDependencyClasses = buildTestDependencyClasses ( project );
76+ Set <String > dependencyClasses = new HashSet <>();
77+ dependencyClasses .addAll ( mainDependencyClasses );
78+ dependencyClasses .addAll ( testDependencyClasses );
7779
78- Set <Artifact > declaredArtifacts = buildDeclaredArtifacts ( project );
80+ Set <String > testOnlyDependencyClasses = buildTestOnlyDependencyClasses ( mainDependencyClasses ,
81+ testDependencyClasses );
7982
8083 Map <Artifact , Set <String >> usedArtifacts = buildUsedArtifacts ( artifactClassMap , dependencyClasses );
8184 Set <Artifact > mainUsedArtifacts = buildUsedArtifacts ( artifactClassMap , mainDependencyClasses ).keySet ();
8285
8386 Set <Artifact > testArtifacts = buildUsedArtifacts ( artifactClassMap , testOnlyDependencyClasses ).keySet ();
8487 Set <Artifact > testOnlyArtifacts = removeAll ( testArtifacts , mainUsedArtifacts );
8588
89+ Set <Artifact > declaredArtifacts = buildDeclaredArtifacts ( project );
8690 Set <Artifact > usedDeclaredArtifacts = new LinkedHashSet <>( declaredArtifacts );
8791 usedDeclaredArtifacts .retainAll ( usedArtifacts .keySet () );
8892
@@ -97,7 +101,7 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
97101 Set <Artifact > testArtifactsWithNonTestScope = getTestArtifactsWithNonTestScope ( testOnlyArtifacts );
98102
99103 return new ProjectDependencyAnalysis ( usedDeclaredArtifacts , usedUndeclaredArtifactsWithClasses ,
100- unusedDeclaredArtifacts , testArtifactsWithNonTestScope );
104+ unusedDeclaredArtifacts , testArtifactsWithNonTestScope );
101105 }
102106 catch ( IOException exception )
103107 {
@@ -113,7 +117,7 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
113117 * @param remove set to exclude
114118 * @return set with remove excluded
115119 */
116- private Set <Artifact > removeAll ( Set <Artifact > start , Set <Artifact > remove )
120+ private static Set <Artifact > removeAll ( Set <Artifact > start , Set <Artifact > remove )
117121 {
118122 Set <Artifact > results = new LinkedHashSet <>( start .size () );
119123
@@ -139,7 +143,7 @@ private Set<Artifact> removeAll( Set<Artifact> start, Set<Artifact> remove )
139143 return results ;
140144 }
141145
142- private Set <Artifact > getTestArtifactsWithNonTestScope ( Set <Artifact > testOnlyArtifacts )
146+ private static Set <Artifact > getTestArtifactsWithNonTestScope ( Set <Artifact > testOnlyArtifacts )
143147 {
144148 Set <Artifact > nonTestScopeArtifacts = new LinkedHashSet <>();
145149
@@ -201,48 +205,26 @@ else if ( file != null && file.isDirectory() )
201205 return artifactClassMap ;
202206 }
203207
204- private Set <String > buildTestDependencyClasses ( MavenProject project ) throws IOException
208+ private static Set <String > buildTestOnlyDependencyClasses ( Set <String > mainDependencyClasses ,
209+ Set <String > testDependencyClasses )
205210 {
206- Set <String > testOnlyDependencyClasses = new HashSet <>();
207-
208- String outputDirectory = project .getBuild ().getOutputDirectory ();
209- Set <String > nonTestDependencyClasses = new HashSet <>( buildDependencyClasses ( outputDirectory ) );
210-
211- String testOutputDirectory = project .getBuild ().getTestOutputDirectory ();
212- Set <String > testDependencyClasses = new HashSet <>( buildDependencyClasses ( testOutputDirectory ) );
213-
214- for ( String testString : testDependencyClasses )
215- {
216- if ( !nonTestDependencyClasses .contains ( testString ) )
217- {
218- testOnlyDependencyClasses .add ( testString );
219- }
220- }
221-
211+ Set <String > testOnlyDependencyClasses = new HashSet <>( testDependencyClasses );
212+ testOnlyDependencyClasses .removeAll ( mainDependencyClasses );
222213 return testOnlyDependencyClasses ;
223214 }
224215
225- private Set <String > buildDependencyClasses ( MavenProject project )
216+ private Set <String > buildMainDependencyClasses ( MavenProject project )
226217 throws IOException
227218 {
228-
229219 String outputDirectory = project .getBuild ().getOutputDirectory ();
230- Set <String > dependencyClasses = new HashSet <>( buildDependencyClasses ( outputDirectory ) );
231-
232- String testOutputDirectory = project .getBuild ().getTestOutputDirectory ();
233- dependencyClasses .addAll ( buildDependencyClasses ( testOutputDirectory ) );
234-
235- return dependencyClasses ;
220+ return buildDependencyClasses ( outputDirectory );
236221 }
237222
238- private Set <String > buildMainDependencyClasses ( MavenProject project )
223+ private Set <String > buildTestDependencyClasses ( MavenProject project )
239224 throws IOException
240225 {
241-
242- String outputDirectory = project .getBuild ().getOutputDirectory ();
243- Set <String > dependencyClasses = new HashSet <>( buildDependencyClasses ( outputDirectory ) );
244-
245- return dependencyClasses ;
226+ String testOutputDirectory = project .getBuild ().getTestOutputDirectory ();
227+ return buildDependencyClasses ( testOutputDirectory );
246228 }
247229
248230 private Set <String > buildDependencyClasses ( String path )
@@ -253,7 +235,7 @@ private Set<String> buildDependencyClasses( String path )
253235 return dependencyAnalyzer .analyze ( url );
254236 }
255237
256- private Set <Artifact > buildDeclaredArtifacts ( MavenProject project )
238+ private static Set <Artifact > buildDeclaredArtifacts ( MavenProject project )
257239 {
258240 Set <Artifact > declaredArtifacts = project .getDependencyArtifacts ();
259241
@@ -265,8 +247,8 @@ private Set<Artifact> buildDeclaredArtifacts( MavenProject project )
265247 return declaredArtifacts ;
266248 }
267249
268- private Map <Artifact , Set <String >> buildUsedArtifacts ( Map <Artifact , Set <String >> artifactClassMap ,
269- Set <String > dependencyClasses )
250+ private static Map <Artifact , Set <String >> buildUsedArtifacts ( Map <Artifact , Set <String >> artifactClassMap ,
251+ Set <String > dependencyClasses )
270252 {
271253 Map <Artifact , Set <String >> usedArtifacts = new HashMap <>();
272254
@@ -289,7 +271,7 @@ private Map<Artifact, Set<String>> buildUsedArtifacts( Map<Artifact, Set<String>
289271 return usedArtifacts ;
290272 }
291273
292- private Artifact findArtifactForClassName ( Map <Artifact , Set <String >> artifactClassMap , String className )
274+ private static Artifact findArtifactForClassName ( Map <Artifact , Set <String >> artifactClassMap , String className )
293275 {
294276 for ( Map .Entry <Artifact , Set <String >> entry : artifactClassMap .entrySet () )
295277 {
0 commit comments