From 52c286908403fd6ed773ff155b1d2e8a26d0e0c5 Mon Sep 17 00:00:00 2001 From: cesarsotovalero Date: Tue, 9 Mar 2021 14:21:23 +0100 Subject: [PATCH] Refactor methods in DefaultProjectDependencyAnalyzer --- .../DefaultProjectDependencyAnalyzer.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java b/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java index 262850a6..86363d50 100644 --- a/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java +++ b/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java @@ -59,7 +59,7 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz /** * A map [dependency] -> [dependency classes]. */ - private final Map> artifactClassesMap = new HashMap<>(); + private Map> artifactClassesMap; /** * Analyze the dependencies in a project. @@ -71,8 +71,8 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz */ public ProjectDependencyAnalysis analyze(MavenProject project) throws ProjectDependencyAnalyzerException { try { - // map of [dependency] -> [classes] - Map> artifactClassMap = buildArtifactClassMap(project); + // a map of [dependency] -> [classes] + artifactClassesMap = buildArtifactClassMap(project); // direct dependencies of the project Set declaredArtifacts = project.getDependencyArtifacts(); @@ -82,16 +82,16 @@ public ProjectDependencyAnalysis analyze(MavenProject project) throws ProjectDep /* ******************** bytecode analysis ********************* */ - // set of classes in project - Set builtProjectDependencyClasses = buildProjectDependencyClasses(project); + // execute the analysis (note that the order of these operations matters!) + buildProjectDependencyClasses(project); Set projectClasses = new HashSet<>(DefaultCallGraph.getProjectVertices()); - Set builtDependenciesDependencyClasses = buildDependenciesDependencyClasses(project); + buildDependenciesDependencyClasses(project); /* ******************** usage analysis ********************* */ // search for the dependencies used by the project Set usedArtifacts = collectUsedArtifacts( - artifactClassMap, + artifactClassesMap, DefaultCallGraph.referencedClassMembers(projectClasses) ); @@ -144,22 +144,18 @@ public Map> buildArtifactClassMap(MavenProject project) th return artifactClassMap; } - private Set buildProjectDependencyClasses(MavenProject project) throws IOException { - Set 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 buildDependenciesDependencyClasses(MavenProject project) throws IOException { - Set 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 collectUsedArtifacts(Map> artifactClassMap,