Skip to content

Commit

Permalink
Refactor ProjectDependencyAnalysis (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: César Soto Valero <[email protected]>
  • Loading branch information
ABHAY0O7 and cesarsotovalero authored Mar 27, 2021
1 parent c79c42d commit 654388e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class DefaultClassAnalyzer implements ClassAnalyzer {
* @return A set of visited classes.
* @throws IOException If there is an error.
*/
@Override
public Set<String> analyze(URL url) throws IOException {
CollectorClassFileVisitor visitor = new CollectorClassFileVisitor();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
Expand All @@ -31,46 +30,60 @@
*/
public class ProjectDependencyAnalysis {

/**
* Store all the used declared artifacts (ie. used direct dependencies).
*/
private final Set<Artifact> usedDeclaredArtifacts;

/**
* Store all the used undeclared artifacts (ie. used transitive dependencies).
*/
private final Set<Artifact> usedUndeclaredArtifacts;

/**
* Store all the unused declared artifacts (ie. unused transitive dependencies).
*/
private final Set<Artifact> unusedDeclaredArtifacts;

/**
* Ctor.
*/
public ProjectDependencyAnalysis(Set<Artifact> usedDeclaredArtifacts,
public ProjectDependencyAnalysis(
Set<Artifact> usedDeclaredArtifacts,
Set<Artifact> usedUndeclaredArtifacts,
Set<Artifact> unusedDeclaredArtifacts) {
this.usedDeclaredArtifacts = safeCopy(usedDeclaredArtifacts);
this.usedUndeclaredArtifacts = safeCopy(usedUndeclaredArtifacts);
this.unusedDeclaredArtifacts = safeCopy(unusedDeclaredArtifacts);
}

/**
* To prevent unnecessary and unexpected modification in the set.
*
* @param The required set.
* @return An unmodifiable set corresponding to the provided set.
*/
private Set<Artifact> safeCopy(Set<Artifact> set) {
return (set == null) ? Collections.emptySet()
: Collections.unmodifiableSet(new LinkedHashSet<Artifact>(set));
}

/**
* Filter not-compile scoped artifacts from unused declared.
* Filter out artifacts with scope other than compile from the set of unused declared artifacts.
*
* @return updated project dependency analysis
* @since 1.3
*/
public ProjectDependencyAnalysis ignoreNonCompile() {
Set<Artifact> filteredUnusedDeclared = new HashSet<>(unusedDeclaredArtifacts);
for (Iterator<Artifact> iter = filteredUnusedDeclared.iterator(); iter.hasNext(); ) {
Artifact artifact = iter.next();
if (!artifact.getScope().equals(Artifact.SCOPE_COMPILE)) {
iter.remove();
}
}
filteredUnusedDeclared.removeIf(artifact -> !artifact.getScope().equals(Artifact.SCOPE_COMPILE));
return new ProjectDependencyAnalysis(usedDeclaredArtifacts, usedUndeclaredArtifacts, filteredUnusedDeclared);
}

/**
* Overrides the hash code value method of the object.
*/
@Override
public int hashCode() {
int hashCode = getUsedDeclaredArtifacts().hashCode();
hashCode = (hashCode * 37) + getUsedUndeclaredArtifacts().hashCode();
Expand All @@ -79,7 +92,7 @@ public int hashCode() {
}

/**
* Used and declared artifacts.
* Used declared artifacts.
*
* @return {@link Artifact}
*/
Expand Down Expand Up @@ -110,6 +123,7 @@ public Set<Artifact> getUnusedDeclaredArtifacts() {
/**
* Overrides the standard equals method of Object.
*/
@Override
public boolean equals(Object object) {
if (object instanceof ProjectDependencyAnalysis) {
ProjectDependencyAnalysis analysis = (ProjectDependencyAnalysis) object;
Expand All @@ -124,6 +138,7 @@ && getUsedUndeclaredArtifacts().equals(analysis.getUsedUndeclaredArtifacts())
/**
* Overrides de toString standard method of class Object @see java.lang.Object#toString().
*/
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();

Expand Down

0 comments on commit 654388e

Please sign in to comment.