val rule: ArchRule =
noClasses().that().resideInAPackage("$Base.app..")
.should().dependOnClassesThat().resideOutsideOfPackages(allowablePackages)
rule.check(importedClasses)
大家好,我想问有没有一种方法可以使用与
resideOutsOfPackages
类似的方法,但对于特定的类而不是包?
我想允许将
ServiceA
例外添加到我的“allowablePackages”中,但我不希望包含 ServiceA
所在的整个包。
明显的解决方案是创建一个新包并将
ServiceA
移动到那里,但我想知道是否有比这更好的解决方案?
您可以组合预定义的
DescribedPredicate
(和语法糖方法以获得更好的描述)
import com.tngtech.archunit.base.DescribedPredicate.doNot
import com.tngtech.archunit.core.domain.JavaClass.Predicates.resideOutsideOfPackages
import com.tngtech.archunit.core.domain.properties.HasName.Predicates.name
import com.tngtech.archunit.lang.conditions.ArchPredicates.have
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses
通过以下方式允许依赖于
ServiceA
,即使它不在allowablePackages
内:
var rule: ArchRule = noClasses()
.that().resideInAPackage("$Base.app..")
.should().dependOnClassesThat(
resideOutsideOfPackages(allowablePackages)
.and(doNot(have(name(ServiceA::class.java.name))))
)