Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ProjectDependencyAnalysis #60

Merged
merged 3 commits into from
Mar 27, 2021
Merged
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 @@ -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