您可以使用自定义条件来测试每个访问的来源:
classes()
.that().resideInAPackage("..model")
.should(new ArchCondition<JavaClass>("only be accessed from the corresponding service package") {
@Override
public void check(JavaClass javaClass, ConditionEvents conditionEvents) {
javaClass.getAccessesToSelf().forEach(access -> {
String originPackageName = access.getOrigin().getOwner().getPackageName();
String targetPackageName = access.getTarget().getOwner().getPackageName();
String expectedOriginPackageName = targetPackageName.replace(".model", ".service");
boolean satisfied = originPackageName.equals(expectedOriginPackageName);
conditionEvents.add(new SimpleConditionEvent(access, satisfied, access.getDescription()));
});
}
});
(可能有更复杂的方法来构建 expectedOriginPackageName
......)