-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ArchCondition that checks for transitive dependencies #878
This adds an ArchCondition to the fluent API to check whether there is any matching transitive dependency. This is a much more efficient variant of `#transitivelyDependOnClassesThat` that can be used to detect transitive dependencies in a huge codebase or to classes in large 3rd-party libraries like the Android SDK. It focuses on detecting all *direct* dependencies of the selected classes that are themselves matched or have any transitive dependencies on matched classes. Thus, it doesn't discover all possible dependency paths but stops at the first match to be fast and resource-friendly. Sample usage: ``` noClasses().that.resideInAPackage(“de.foo.service..”) .should().transitivelyDependOnAnyClassesThat.resideInAPackage("android..") ``` Then this Architecture ![image](https://user-images.githubusercontent.com/17569373/172873445-17111662-30e2-4388-912e-840a105cd2bc.png) would output the following violations: ``` java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'no classes that reside in a package 'de.foo.service..' should transitively depend on any classes that reside in a package ['android..'] was violated (3 times): Class <de.foo.service.B> transitively depends on <android.I> by [F->E->android.I] in (B.java:0) Class <de.foo.service.B> transitively depends on <android.I> by [E->android.I] in (B.java:0) Class <de.foo.service.C> transitively depends on <android.H> by [D->android.H] in (C.java:0) ``` Resolves: #780 Resolves: #826
- Loading branch information
Showing
5 changed files
with
213 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...nit/src/main/java/com/tngtech/archunit/lang/conditions/TransitiveDependencyCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2014-2022 TNG Technology Consulting GmbH | ||
* | ||
* Licensed 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. | ||
*/ | ||
package com.tngtech.archunit.lang.conditions; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import com.tngtech.archunit.lang.ConditionEvent; | ||
import com.tngtech.archunit.lang.ConditionEvents; | ||
import com.tngtech.archunit.lang.SimpleConditionEvent; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.common.collect.Iterables.getLast; | ||
import static com.tngtech.archunit.lang.ConditionEvent.createMessage; | ||
import static java.util.stream.Collectors.joining; | ||
import static java.util.stream.Collectors.toSet; | ||
|
||
public class TransitiveDependencyCondition extends ArchCondition<JavaClass> { | ||
|
||
private final DescribedPredicate<? super JavaClass> conditionPredicate; | ||
private final TransitiveDependencyPath transitiveDependencyPath = new TransitiveDependencyPath(); | ||
private Collection<JavaClass> allClasses; | ||
|
||
public TransitiveDependencyCondition(DescribedPredicate<? super JavaClass> conditionPredicate) { | ||
super("transitively depend on classes that " + conditionPredicate.getDescription()); | ||
|
||
this.conditionPredicate = checkNotNull(conditionPredicate); | ||
} | ||
|
||
@Override | ||
public void init(Collection<JavaClass> allObjectsToTest) { | ||
this.allClasses = allObjectsToTest; | ||
} | ||
|
||
@Override | ||
public void check(JavaClass javaClass, ConditionEvents events) { | ||
boolean hasTransitiveDependency = false; | ||
for (JavaClass target : getDirectDependencyTargetsOutsideOfAnalyzedClasses(javaClass)) { | ||
List<JavaClass> dependencyPath = transitiveDependencyPath.findPathTo(target); | ||
if (!dependencyPath.isEmpty()) { | ||
events.add(newTransitiveDependencyPathFoundEvent(javaClass, dependencyPath)); | ||
hasTransitiveDependency = true; | ||
} | ||
} | ||
if (!hasTransitiveDependency) { | ||
events.add(newNoTransitiveDependencyPathFoundEvent(javaClass)); | ||
} | ||
} | ||
|
||
private static ConditionEvent newTransitiveDependencyPathFoundEvent(JavaClass javaClass, List<JavaClass> transitiveDependencyPath) { | ||
String message = String.format("%sdepends on <%s>", | ||
transitiveDependencyPath.size() > 1 ? "transitively " : "", | ||
getLast(transitiveDependencyPath).getFullName()); | ||
|
||
if (transitiveDependencyPath.size() > 1) { | ||
message += " by [" + transitiveDependencyPath.stream().map(JavaClass::getName).collect(joining("->")) + "]"; | ||
} | ||
|
||
return SimpleConditionEvent.satisfied(javaClass, createMessage(javaClass, message)); | ||
} | ||
|
||
private static ConditionEvent newNoTransitiveDependencyPathFoundEvent(JavaClass javaClass) { | ||
return SimpleConditionEvent.violated(javaClass, createMessage(javaClass, "does not transitively depend on any matching class")); | ||
} | ||
|
||
private Set<JavaClass> getDirectDependencyTargetsOutsideOfAnalyzedClasses(JavaClass item) { | ||
return item.getDirectDependenciesFromSelf().stream() | ||
.map(dependency -> dependency.getTargetClass().getBaseComponentType()) | ||
.filter(targetClass -> !allClasses.contains(targetClass)) | ||
.collect(toSet()); | ||
} | ||
|
||
private class TransitiveDependencyPath { | ||
/** | ||
* @return some outgoing transitive dependency path to the supplied class or empty if there is none | ||
*/ | ||
List<JavaClass> findPathTo(JavaClass clazz) { | ||
ImmutableList.Builder<JavaClass> transitivePath = ImmutableList.builder(); | ||
addDependenciesToPathFrom(clazz, transitivePath, new HashSet<>()); | ||
return transitivePath.build().reverse(); | ||
} | ||
|
||
private boolean addDependenciesToPathFrom( | ||
JavaClass clazz, | ||
ImmutableList.Builder<JavaClass> dependencyPath, | ||
Set<JavaClass> analyzedClasses | ||
) { | ||
if (conditionPredicate.test(clazz)) { | ||
dependencyPath.add(clazz); | ||
return true; | ||
} | ||
|
||
analyzedClasses.add(clazz); | ||
|
||
for (JavaClass directDependency : getDirectDependencyTargetsOutsideOfAnalyzedClasses(clazz)) { | ||
if (!analyzedClasses.contains(directDependency) | ||
&& addDependenciesToPathFrom(directDependency, dependencyPath, analyzedClasses)) { | ||
dependencyPath.add(clazz); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters