Skip to content

Commit

Permalink
Refactor methods in DefaultProjectDependencyAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsotovalero committed Mar 9, 2021
1 parent 05e281c commit 52c2869
Showing 1 changed file with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
/**
* A map [dependency] -> [dependency classes].
*/
private final Map<Artifact, Set<String>> artifactClassesMap = new HashMap<>();
private Map<Artifact, Set<String>> artifactClassesMap;

/**
* Analyze the dependencies in a project.
Expand All @@ -71,8 +71,8 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
*/
public ProjectDependencyAnalysis analyze(MavenProject project) throws ProjectDependencyAnalyzerException {
try {
// map of [dependency] -> [classes]
Map<Artifact, Set<String>> artifactClassMap = buildArtifactClassMap(project);
// a map of [dependency] -> [classes]
artifactClassesMap = buildArtifactClassMap(project);

// direct dependencies of the project
Set<Artifact> declaredArtifacts = project.getDependencyArtifacts();
Expand All @@ -82,16 +82,16 @@ public ProjectDependencyAnalysis analyze(MavenProject project) throws ProjectDep

/* ******************** bytecode analysis ********************* */

// set of classes in project
Set<String> builtProjectDependencyClasses = buildProjectDependencyClasses(project);
// execute the analysis (note that the order of these operations matters!)
buildProjectDependencyClasses(project);
Set<String> projectClasses = new HashSet<>(DefaultCallGraph.getProjectVertices());
Set<String> builtDependenciesDependencyClasses = buildDependenciesDependencyClasses(project);
buildDependenciesDependencyClasses(project);

/* ******************** usage analysis ********************* */

// search for the dependencies used by the project
Set<Artifact> usedArtifacts = collectUsedArtifacts(
artifactClassMap,
artifactClassesMap,
DefaultCallGraph.referencedClassMembers(projectClasses)
);

Expand Down Expand Up @@ -144,22 +144,18 @@ public Map<Artifact, Set<String>> buildArtifactClassMap(MavenProject project) th
return artifactClassMap;
}

private Set<String> buildProjectDependencyClasses(MavenProject project) throws IOException {
Set<String> dependencyClasses = new HashSet<>();
private void buildProjectDependencyClasses(MavenProject project) throws IOException {
/* paths to project compiled classes */
String outputDirectory = project.getBuild().getOutputDirectory();
String testOutputDirectory = project.getBuild().getTestOutputDirectory();
/* construct the dependency classes */
dependencyClasses.addAll(collectDependencyClasses(outputDirectory));
dependencyClasses.addAll(collectDependencyClasses(testOutputDirectory));
return dependencyClasses;
collectDependencyClasses(outputDirectory);
collectDependencyClasses(testOutputDirectory);
}

private Set<String> buildDependenciesDependencyClasses(MavenProject project) throws IOException {
Set<String> dependencyClasses = new HashSet<>();
String dependenciesDirectory = project.getBuild().getDirectory() + "/" + "dependency";
dependencyClasses.addAll(collectDependencyClasses(dependenciesDirectory));
return dependencyClasses;
private void buildDependenciesDependencyClasses(MavenProject project) throws IOException {
String dependenciesDirectory = project.getBuild().getDirectory() + File.separator + "dependency";
collectDependencyClasses(dependenciesDirectory);
}

private Set<Artifact> collectUsedArtifacts(Map<Artifact, Set<String>> artifactClassMap,
Expand Down

0 comments on commit 52c2869

Please sign in to comment.