Closed ThStock closed 5 years ago
You can per se not do any checks of imports with ArchUnit, since ArchUnit operates on bytecode and import statements live only in source code (and vanish on compile). You can however forbid that any class depends on an evil implementation like
noClasses().should().dependOnClassesThat()
.haveFullyQualifiedName(org.bouncycastle.util.Strings.class.getName())
Or you could write a little more sophisticated custom code to make this more generic:
classes().should(onlyHaveDependenciesTo(com.google.common.base.Strings.class))
...
ArchCondition<JavaClass> onlyHaveDependenciesTo(final Class<?> allowedType) {
return new ArchCondition<JavaClass>("only have dependencies to " + allowedType.getName()) {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
javaClass.getDirectDependenciesFromSelf().stream()
.filter(d -> d.getTargetClass().getSimpleName().equals(allowedType.getSimpleName()))
.filter(d -> !d.getTargetClass().isEquivalentTo(allowedType))
.forEach(d -> events.add(SimpleConditionEvent.violated(d, d.getDescription())));
}
};
}
The last example would white list your allowed dependency and automatically grow if for some reason you get a further implementation of Strings
on your classpath.
Does this help you?
Yes both works very well.
Cool, I'll close this issue then :smiley: Feel free to open another one, if you should run into further problems!!
Is it possible to add a rule that check my classes for unwanted imports e.g. if I only want:
but in my classpath are: