Search in sources :

Example 1 with AccessTarget

use of com.tngtech.archunit.core.domain.AccessTarget in project git-machete-intellij-plugin by VirtusLab.

the class UiThreadUnsafeMethodInvocationsTestSuite method only_ui_thread_unsafe_methods_should_call_other_ui_thread_unsafe_methods.

@Test
public void only_ui_thread_unsafe_methods_should_call_other_ui_thread_unsafe_methods() {
    methods().that().areNotAnnotatedWith(UIThreadUnsafe.class).should(new ArchCondition<JavaMethod>("never call any ${UIThreadUnsafeName} methods") {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            // which would in turn heavily reduce readability, hence potentially leading to bugs in the long run.
            if (method.getName().startsWith("access$") || method.getName().startsWith("lambda$")) {
                return;
            }
            method.getCallsFromSelf().forEach(access -> {
                AccessTarget accessTarget = access.getTarget();
                if (accessTarget.isAnnotatedWith(UIThreadUnsafe.class)) {
                    String message = "a non-${UIThreadUnsafeName} method ${method.getFullName()} " + "calls a ${UIThreadUnsafeName} method ${accessTarget.getFullName()}";
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            });
        }
    }).check(importedClasses);
}
Also used : ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Aggregations

AccessTarget (com.tngtech.archunit.core.domain.AccessTarget)1 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)1 ArchCondition (com.tngtech.archunit.lang.ArchCondition)1 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)1 Test (org.junit.Test)1