Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

[MSHARED-632] Expose which dependency classes are used and where #21

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -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,20 +74,21 @@ public ProjectDependencyAnalysis analyze( MavenProject project )
{
Map<Artifact, Set<String>> artifactClassMap = buildArtifactClassMap( project );

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

Set<Artifact> declaredArtifacts = buildDeclaredArtifacts( project );

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

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() );

return new ProjectDependencyAnalysis( usedDeclaredArtifacts, usedUndeclaredArtifacts,
unusedDeclaredArtifacts );
Expand All @@ -101,32 +105,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;
}

protected Map<Artifact, Set<String>> buildArtifactClassMap( MavenProject project )
Expand Down Expand Up @@ -189,26 +183,26 @@ else if ( file != null && file.isDirectory() )
return artifactClassMap;
}

protected Set<String> buildDependencyClasses( MavenProject project )
protected 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 );
}

protected Set<Artifact> buildDeclaredArtifacts( MavenProject project )
Expand All @@ -224,22 +218,29 @@ protected Set<Artifact> buildDeclaredArtifacts( MavenProject project )
return declaredArtifacts;
}

protected Set<Artifact> buildUsedArtifacts( Map<Artifact, Set<String>> artifactClassMap,
Set<String> dependencyClasses )
protected Map<Artifact, Set<DependencyUsage>> buildArtifactToUsageMap( Map<Artifact, Set<String>> artifactClassMap,
Set<DependencyUsage> dependencyUsages )
{
Set<Artifact> usedArtifacts = new HashSet<Artifact>();
Map<String, Set<DependencyUsage>> dependencyClassToUsages = buildDependencyClassToUsageMap( dependencyUsages );

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

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

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

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

return usedArtifacts;
return artifactToUsages;
}

protected Artifact findArtifactForClassName( Map<Artifact, Set<String>> artifactClassMap, String className )
Expand All @@ -254,4 +255,35 @@ protected Artifact findArtifactForClassName( Map<Artifact, Set<String>> artifact

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 )
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