forked from TNG/ArchUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add arch condition to check for any transitive dependency
Issue: TNG#780 Signed-off-by: e.solutions <info@esolutions.de> on-behalf-of: @e-esolutions-GmbH <info@esolutions.de>
- Loading branch information
Showing
5 changed files
with
226 additions
and
21 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
.../src/main/java/com/tngtech/archunit/lang/conditions/AnyTransitiveDependencyCondition.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,120 @@ | ||
/* | ||
* 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.LinkedList; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.Dependency; | ||
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; | ||
|
||
public class AnyTransitiveDependencyCondition extends ArchCondition<JavaClass> { | ||
|
||
private final DescribedPredicate<? super JavaClass> conditionPredicate; | ||
private final Set<JavaClass> allClasses = new HashSet<>(); | ||
|
||
public AnyTransitiveDependencyCondition(DescribedPredicate<? super JavaClass> conditionPredicate) { | ||
super("transitively depend on any classes that " + conditionPredicate.getDescription()); | ||
|
||
this.conditionPredicate = checkNotNull(conditionPredicate); | ||
} | ||
|
||
@Override | ||
public void init(Collection<JavaClass> allObjectsToTest) { | ||
allClasses.addAll(allObjectsToTest); | ||
} | ||
|
||
@Override | ||
public void check(JavaClass item, ConditionEvents events) { | ||
getDirectDependencyTargets(item) | ||
.filter(not(allClasses::contains)) | ||
.map(this::getDependencyPathToMatchingClasses) | ||
.filter(Objects::nonNull) | ||
.map(dependencyPath -> createTransitivePathFoundEvent(item, dependencyPath)) | ||
.forEach(events::add); | ||
} | ||
|
||
/** | ||
* @return the dependency path to a matching class including the source class or null if there is none | ||
*/ | ||
private LinkedList<JavaClass> getDependencyPathToMatchingClasses(JavaClass clazz) { | ||
LinkedList<JavaClass> transitivePath = new LinkedList<>(); | ||
if (matchingTransitiveDependencies(clazz, new HashSet<>(), transitivePath)) { | ||
transitivePath.add(clazz); | ||
return transitivePath; | ||
} | ||
return null; | ||
} | ||
|
||
private boolean matchingTransitiveDependencies( | ||
JavaClass clazz, | ||
HashSet<JavaClass> analyzedClasses, | ||
LinkedList<JavaClass> transitivePath | ||
) { | ||
if (conditionPredicate.test(clazz)) { | ||
return true; | ||
} | ||
|
||
analyzedClasses.add(clazz); | ||
|
||
Optional<JavaClass> firstMatchingTransitiveDependency = getDirectDependencyTargets(clazz) | ||
.filter(not(allClasses::contains)) | ||
.filter(not(analyzedClasses::contains)) | ||
.filter(it -> matchingTransitiveDependencies(it, analyzedClasses, transitivePath)) | ||
.findFirst(); | ||
|
||
firstMatchingTransitiveDependency.ifPresent(transitivePath::add); | ||
return firstMatchingTransitiveDependency.isPresent(); | ||
} | ||
|
||
private static Stream<JavaClass> getDirectDependencyTargets(JavaClass item) { | ||
return item.getDirectDependenciesFromSelf().stream().map(Dependency::getTargetClass).map(JavaClass::getBaseComponentType).distinct(); | ||
} | ||
|
||
private static ConditionEvent createTransitivePathFoundEvent(JavaClass clazz, LinkedList<JavaClass> dependencyPath) { | ||
StringBuilder messageBuilder = | ||
new StringBuilder("Class <" + clazz.getFullName() + "> accesses <" + dependencyPath.getLast().getFullName() + ">"); | ||
if (dependencyPath.size() > 1) { | ||
messageBuilder | ||
.append(" which transitively accesses e.g. ") | ||
.append(dependencyPath.subList(0, dependencyPath.size()).stream() | ||
.map(it -> String.format("<%s>", it.getFullName())) | ||
.collect(Collectors.joining(" <- "))); | ||
|
||
} | ||
return SimpleConditionEvent.satisfied(clazz, messageBuilder.toString()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T> Predicate<T> not(Predicate<? super T> target) { | ||
Objects.requireNonNull(target); | ||
return (Predicate<T>) target.negate(); | ||
} | ||
} |
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
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