英文:
ArchUnit rule to check if class is accessed within same package or sub-packages?
问题
我有用我的自定义注解@Inner
进行注释的类。
我想创建一个ArchUnit
规则,以便检测是否从同一包或子包内访问了带有这个特定注解的类。
例如:
包:com.example.my.package
包含被注解为@Inner
的类:MyInner
因此,该规则应检查是否仅从com.example.my.package
或com.example.my.package.baz
访问了MyInner
。
英文:
I have classes annotated by my custom annotation @Inner
.
I would like to create a rule for ArchUnit
to detect if classes annotated with this particular annotation are accessed within the same package or sub-packages.
For example:
package: com.example.my.package
Contains class: MyInner
that is annotated with @Inner
So the rule should check if MyInner
is accessed only from com.example.my.package
or com.example.my.package.baz
答案1
得分: 0
我找到了这样的规则:
public class DependencyRules {
@PublicAPI(usage = ACCESS)
public static ArchCondition<JavaClass> accessClassesThatResideInAnUpperPackage() {
return new AccessClassesThatResideInAnUpperPackageCondition();
}
private static class AccessClassesThatResideInAnUpperPackageCondition extends ArchCondition<JavaClass> {
AccessClassesThatResideInAnUpperPackageCondition() {
super("访问位于上层包中的类");
}
@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
英文:
I've found such rule:
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;
}
}
}
Source: https://github.com/TNG/ArchUnit/commit/5f7fadc0f67ba61e2c9fe94bfe494303780c37d7
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论