Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Archunit check method calls

Tags:

archunit

I have a class that has three methods that shouldn't be called from certain classes.

How can I check this with Archunit?

So for example

public class Foo {

   public void method1() {
   }

   public void method2() {
   }

   public void method3() {
   }
}

method1 and method2 should only be called by classes Bar1 and Bar2.

like image 957
Simon Martinelli Avatar asked Oct 14 '25 03:10

Simon Martinelli


1 Answers

With

import com.tngtech.archunit.lang.ArchRule;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.lang.conditions.ArchConditions.callMethod;

you can use

ArchRule rule = noClasses()
    .that().doNotHaveFullyQualifiedName(Bar1.class.getName())
    .and().doNotHaveFullyQualifiedName(Bar2.class.getName())
    // alternative 1:
    .should().callMethod(Foo.class, "method1")
    .orShould().callMethod(Foo.class, "method2");
    // alternative 2:
    // .should(callMethod(Foo.class, "method1").or(callMethod(Foo.class, "method2")));
like image 161
Manfred Avatar answered Oct 19 '25 12:10

Manfred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!