ArchUnit规则,用于检查类是否在同一个包或子包中被访问?

huangapple go评论81阅读模式
英文:

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.packagecom.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&lt;JavaClass&gt; accessClassesThatResideInAnUpperPackage() {
        return new AccessClassesThatResideInAnUpperPackageCondition();
    }

    private static class AccessClassesThatResideInAnUpperPackageCondition extends ArchCondition&lt;JavaClass&gt; {
        AccessClassesThatResideInAnUpperPackageCondition() {
            super(&quot;access classes that reside in an upper package&quot;);
        }

        @Override
        public void check(final JavaClass clazz, final ConditionEvents events) {
            for (JavaAccess&lt;?&gt; 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() + &quot;.&quot;;
            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

huangapple
  • 本文由 发表于 2020年5月4日 00:26:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定