ArchUnit 规则检查 class 是否在同一个包或子包中被访问?
ArchUnit rule to check if class is accessed within same package or sub-packages?
我有 classes 由我的自定义注释 @Inner
注释。
我想为 ArchUnit
创建一个规则,以检测是否在同一个包或子包中访问使用此特定注释注释的 classes。
例如:
包:com.example.my.package
包含 class:MyInner
注释为 @Inner
因此规则应该检查 MyInner
是否仅从 com.example.my.package
或 com.example.my.package.baz
访问
我发现了这样的规则:
public class DependencyRules {
@PublicAPI(usage = ACCESS)
public static ArchCondition<JavaClass> accessClassesThatResideInAnUpperPackage() {
return new AccessClassesThatResideInAnUpperPackageCondition();
}
private static class AccessClassesThatResideInAnUpperPackageCondition extends ArchCondition<JavaClass> {
AccessClassesThatResideInAnUpperPackageCondition() {
super("access classes that reside in an upper package");
}
@Override
public void check(final JavaClass clazz, final ConditionEvents events) {
for (JavaAccess<?> access : clazz.getAccessesFromSelf()) {
boolean callToSuperPackage = isCallToSuperPackage(access.getOriginOwner(), access.getTargetOwner());
events.add(new SimpleConditionEvent(access, callToSuperPackage, access.getDescription()));
}
}
private boolean isCallToSuperPackage(JavaClass origin, JavaClass target) {
String originPackageName = getOutermostEnclosingClass(origin).getPackageName();
String targetSubPackagePrefix = getOutermostEnclosingClass(target).getPackageName() + ".";
return originPackageName.startsWith(targetSubPackagePrefix);
}
private JavaClass getOutermostEnclosingClass(JavaClass javaClass) {
while (javaClass.getEnclosingClass().isPresent()) {
javaClass = javaClass.getEnclosingClass().get();
}
return javaClass;
}
}
}
来源:https://github.com/TNG/ArchUnit/commit/5f7fadc0f67ba61e2c9fe94bfe494303780c37d7
我有 classes 由我的自定义注释 @Inner
注释。
我想为 ArchUnit
创建一个规则,以检测是否在同一个包或子包中访问使用此特定注释注释的 classes。
例如:
包:com.example.my.package
包含 class:MyInner
注释为 @Inner
因此规则应该检查 MyInner
是否仅从 com.example.my.package
或 com.example.my.package.baz
我发现了这样的规则:
public class DependencyRules {
@PublicAPI(usage = ACCESS)
public static ArchCondition<JavaClass> accessClassesThatResideInAnUpperPackage() {
return new AccessClassesThatResideInAnUpperPackageCondition();
}
private static class AccessClassesThatResideInAnUpperPackageCondition extends ArchCondition<JavaClass> {
AccessClassesThatResideInAnUpperPackageCondition() {
super("access classes that reside in an upper package");
}
@Override
public void check(final JavaClass clazz, final ConditionEvents events) {
for (JavaAccess<?> access : clazz.getAccessesFromSelf()) {
boolean callToSuperPackage = isCallToSuperPackage(access.getOriginOwner(), access.getTargetOwner());
events.add(new SimpleConditionEvent(access, callToSuperPackage, access.getDescription()));
}
}
private boolean isCallToSuperPackage(JavaClass origin, JavaClass target) {
String originPackageName = getOutermostEnclosingClass(origin).getPackageName();
String targetSubPackagePrefix = getOutermostEnclosingClass(target).getPackageName() + ".";
return originPackageName.startsWith(targetSubPackagePrefix);
}
private JavaClass getOutermostEnclosingClass(JavaClass javaClass) {
while (javaClass.getEnclosingClass().isPresent()) {
javaClass = javaClass.getEnclosingClass().get();
}
return javaClass;
}
}
}
来源:https://github.com/TNG/ArchUnit/commit/5f7fadc0f67ba61e2c9fe94bfe494303780c37d7