Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -24,10 +24,13 @@
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -71,24 +74,25 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
{
Map<Artifact, Set<String>> artifactClassMap = buildArtifactClassMap( project );

Set<String> dependencyClasses = buildDependencyClasses( project );
Set<DependencyUsage> dependencyUsages = buildDependencyUsages( project );

Set<String> testOnlyDependencyClasses = buildTestDependencyClasses( project );

Set<Artifact> declaredArtifacts = buildDeclaredArtifacts( project );

Set<Artifact> usedArtifacts = buildUsedArtifacts( artifactClassMap, dependencyClasses );
Map<Artifact, Set<DependencyUsage>> usedArtifacts = buildArtifactToUsageMap( artifactClassMap,
dependencyUsages );

Set<Artifact> testOnlyArtifacts = buildUsedArtifacts( artifactClassMap, testOnlyDependencyClasses );

Set<Artifact> usedDeclaredArtifacts = new LinkedHashSet<Artifact>( declaredArtifacts );
usedDeclaredArtifacts.retainAll( usedArtifacts );
Map<Artifact, Set<DependencyUsage>> usedDeclaredArtifacts = buildMutableCopy( usedArtifacts );
usedDeclaredArtifacts.keySet().retainAll( declaredArtifacts );

Set<Artifact> usedUndeclaredArtifacts = new LinkedHashSet<Artifact>( usedArtifacts );
usedUndeclaredArtifacts = removeAll( usedUndeclaredArtifacts, declaredArtifacts );
Map<Artifact, Set<DependencyUsage>> usedUndeclaredArtifacts = buildMutableCopy( usedArtifacts );
removeAll( usedUndeclaredArtifacts.keySet(), declaredArtifacts );

Set<Artifact> unusedDeclaredArtifacts = new LinkedHashSet<Artifact>( declaredArtifacts );
unusedDeclaredArtifacts = removeAll( unusedDeclaredArtifacts, usedArtifacts );
removeAll( unusedDeclaredArtifacts, usedArtifacts.keySet() );

Set<Artifact> testArtifactsWithNonTestScope = getTestArtifactsWithNonTestScope( testOnlyArtifacts );

Expand All @@ -107,32 +111,22 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
*
* @param start initial set
* @param remove set to exclude
* @return set with remove excluded
*/
private Set<Artifact> removeAll( Set<Artifact> start, Set<Artifact> remove )
private void removeAll( Set<Artifact> start, Set<Artifact> remove )
{
Set<Artifact> results = new LinkedHashSet<Artifact>( start.size() );

for ( Artifact artifact : start )
for ( Iterator<Artifact> iterator = start.iterator(); iterator.hasNext(); )
{
boolean found = false;
Artifact artifact = iterator.next();

for ( Artifact artifact2 : remove )
{
if ( artifact.getDependencyConflictId().equals( artifact2.getDependencyConflictId() ) )
{
found = true;
iterator.remove();
break;
}
}

if ( !found )
{
results.add( artifact );
}
}

return results;
}

private Set<Artifact> getTestArtifactsWithNonTestScope( Set<Artifact> testOnlyArtifacts )
Expand Down Expand Up @@ -217,10 +211,16 @@ private Set<String> buildTestDependencyClasses( MavenProject project ) throws IO
Set<String> testOnlyDependencyClasses = new HashSet<>();

String outputDirectory = project.getBuild().getOutputDirectory();
nonTestDependencyClasses.addAll( buildDependencyClasses( outputDirectory ) );
for ( DependencyUsage nonTestUsage : buildDependencyUsages( outputDirectory ) )
{
nonTestDependencyClasses.add( nonTestUsage.getDependencyClass() );
}

String testOutputDirectory = project.getBuild().getTestOutputDirectory();
testDependencyClasses.addAll( buildDependencyClasses( testOutputDirectory ) );
for ( DependencyUsage testUsage : buildDependencyUsages( testOutputDirectory ) )
{
testDependencyClasses.add( testUsage.getDependencyClass() );
}

for ( String testString : testDependencyClasses )
{
Expand All @@ -233,26 +233,26 @@ private Set<String> buildTestDependencyClasses( MavenProject project ) throws IO
return testOnlyDependencyClasses;
}

private Set<String> buildDependencyClasses( MavenProject project )
private Set<DependencyUsage> buildDependencyUsages( MavenProject project )
throws IOException
{
Set<String> dependencyClasses = new HashSet<String>();
Set<DependencyUsage> dependencyUsages = new HashSet<DependencyUsage>();

String outputDirectory = project.getBuild().getOutputDirectory();
dependencyClasses.addAll( buildDependencyClasses( outputDirectory ) );
dependencyUsages.addAll( buildDependencyUsages( outputDirectory ) );

String testOutputDirectory = project.getBuild().getTestOutputDirectory();
dependencyClasses.addAll( buildDependencyClasses( testOutputDirectory ) );
dependencyUsages.addAll( buildDependencyUsages( testOutputDirectory ) );

return dependencyClasses;
return dependencyUsages;
}

private Set<String> buildDependencyClasses( String path )
private Set<DependencyUsage> buildDependencyUsages( String path )
throws IOException
{
URL url = new File( path ).toURI().toURL();

return dependencyAnalyzer.analyze( url );
return dependencyAnalyzer.analyzeWithUsages( url );
}

private Set<Artifact> buildDeclaredArtifacts( MavenProject project )
Expand Down Expand Up @@ -286,6 +286,31 @@ private Set<Artifact> buildUsedArtifacts( Map<Artifact, Set<String>> artifactCla
return usedArtifacts;
}

private Map<Artifact, Set<DependencyUsage>> buildArtifactToUsageMap( Map<Artifact, Set<String>> artifactClassMap,
Set<DependencyUsage> dependencyUsages )
{
Map<String, Set<DependencyUsage>> dependencyClassToUsages = buildDependencyClassToUsageMap( dependencyUsages );

Map<Artifact, Set<DependencyUsage>> artifactToUsages = new HashMap<Artifact, Set<DependencyUsage>>();

for ( Entry<String, Set<DependencyUsage>> entry : dependencyClassToUsages.entrySet() )
{
Artifact artifact = findArtifactForClassName( artifactClassMap, entry.getKey() );

if ( artifact != null )
{
if ( !artifactToUsages.containsKey( artifact ) )
{
artifactToUsages.put( artifact, new HashSet<DependencyUsage>() );
}

artifactToUsages.get( artifact ).addAll( entry.getValue() );
}
}

return artifactToUsages;
}

private Artifact findArtifactForClassName( Map<Artifact, Set<String>> artifactClassMap, String className )
{
for ( Map.Entry<Artifact, Set<String>> entry : artifactClassMap.entrySet() )
Expand All @@ -298,4 +323,35 @@ private Artifact findArtifactForClassName( Map<Artifact, Set<String>> artifactCl

return null;
}

private Map<String, Set<DependencyUsage>> buildDependencyClassToUsageMap( Set<DependencyUsage> dependencyUsages )
{
Map<String, Set<DependencyUsage>> dependencyClassToUsages = new HashMap<String, Set<DependencyUsage>>();

for ( DependencyUsage dependencyUsage : dependencyUsages )
{
String dependencyClass = dependencyUsage.getDependencyClass();

if ( !dependencyClassToUsages.containsKey( dependencyClass ) )
{
dependencyClassToUsages.put( dependencyClass, new HashSet<DependencyUsage>() );
}

dependencyClassToUsages.get( dependencyClass ).add( dependencyUsage );
}

return dependencyClassToUsages;
}

private Map<Artifact, Set<DependencyUsage>> buildMutableCopy( Map<Artifact, Set<DependencyUsage>> map )
{
Map<Artifact, Set<DependencyUsage>> copy = new LinkedHashMap<Artifact, Set<DependencyUsage>>();

for ( Entry<Artifact, Set<DependencyUsage>> entry : map.entrySet() )
{
copy.put( entry.getKey(), new LinkedHashSet<DependencyUsage>( entry.getValue() ) );
}

return copy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public interface DependencyAnalyzer

Set<String> analyze( URL url )
throws IOException;

Set<DependencyUsage> analyzeWithUsages( URL url )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an incompatible change. Would it make sense to simply make analyze include usages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to simply make analyze include usages?

How were you thinking that would work, change the return type of analyze method? Isn't that also incompatible? And just to confirm, you're thinking about the case where someone wrote a custom implementation of DependencyAnalyzer and registered it for use with the dependency plugin?

One option that comes to mind is making analyzeWithUsages a default interface method with an implementation that throws something like UnsupportedOperationException. That way the dependency plugin can try to gracefully handle this case and fall back to the regular analyze method, for example:

  try 
  {
    return dependencyAnalyzer.analyzeWithUsages( url );
  }
  catch ( UnsupportedOperationException e )
  {
    log.warn( "Dependency analyzer {} should be updated to implement analyzeWithUsages", dependencyAnalyzer  );
    return dependencyAnalyzer.analyze( url );
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have default interface methods until java 8. :-(

Copy link
Contributor Author

@jhaber jhaber Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case another option is to keep things as-is and have the dependency plugin use the same pseudo-code as above, except it would catch something like NoSuchMethodError instead of UnsupportedOperationException (with a comment explaining the situation). Certainly not ideal, but it seems like we ultimately need some way for the dependency plugin to detect that the analyzer doesn't support the new functionality and gracefully degrade

throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.apache.maven.shared.dependency.analyzer;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* Usage of a dependency class by a project class.
*
* @author <a href="mailto:[email protected]">Jonathan Haber</a>
*/
public class DependencyUsage
{
// fields -----------------------------------------------------------------

private final String dependencyClass;

private final String usedBy;

// constructors -----------------------------------------------------------

public DependencyUsage( String dependencyClass, String usedBy )
{
this.dependencyClass = dependencyClass;
this.usedBy = usedBy;
}

// public methods ---------------------------------------------------------


public String getDependencyClass()
{
return dependencyClass;
}

public String getUsedBy()
{
return usedBy;
}

// Object methods ---------------------------------------------------------

/*
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
int hashCode = dependencyClass.hashCode();
hashCode = ( hashCode * 37 ) + usedBy.hashCode();

return hashCode;
}

/*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals( Object object )
{
if ( object instanceof DependencyUsage )
{
DependencyUsage usage = (DependencyUsage) object;

return getDependencyClass().equals( usage.getDependencyClass() )
&& getUsedBy().equals( usage.getUsedBy() );
}

return false;
}

/*
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuilder buffer = new StringBuilder();

buffer.append( "dependencyClass=" ).append( getDependencyClass() );
buffer.append( "," );
buffer.append( "usedBy=" ).append( getUsedBy() );

buffer.insert( 0, "[" );
buffer.insert( 0, getClass().getName() );

buffer.append( "]" );

return buffer.toString();
}
}
Loading