ArchUnit icon indicating copy to clipboard operation
ArchUnit copied to clipboard

How to check for unwanted dependencies in the signature / API of a class only?

Open Glurz opened this issue 4 years ago • 3 comments

I would like to implement an ArchUnit rule that checks for unwanted dependencies. That's easy to do but I'm only interested in violations that are part of the signature / API of the class. E.g. if the class uses an unwanted dependency in a private field or as method parameter of a private method, that's fine since it's not visible from outside.

I'm struggling with the fluent API. My obvious starting point is: noClasses().that() .resideInAnyPackage("..domain..", "..application..") .should() .dependOnClassesThat() .resideInAnyPackage( "badpackage1..", "badpackage2..");

How can I refine the above rule to only trigger for non-private language elements of my classes?

Glurz avatar Jun 30 '20 09:06 Glurz

You might have to declare individual rules for parameters or return types of public methods, and parent classes or interfaces of public classes (or interfaces), etc.

BTW: ArchUnit also uses an @ArchTest for the library itself that Guava should not leak into public API, which might be similar to what you're looking for. Maybe this can give some inspiration? Part of it looks like this:

codeUnits()
    .that().arePublic()
    .and().areDeclaredInClassesThat(are(publicAPI()))  // publicAPI is a custom predicate
    .should().haveRawParameterTypes(...)  // a custom predicate
    .andShould().haveRawReturnType(...)  // another custom predicate 

hankem avatar Jun 30 '20 20:06 hankem

There is probably a related issue here: #317 But yes, at the moment it is a little bit of manual work to plug something together to only check public API :disappointed:

codecholeric avatar Jul 01 '20 12:07 codecholeric

Thanks @hankem and @codecholeric. Yes I might be able to define my own predicates, but it's not easy to catch all language elements that contribute to the public API of a class. But it's well defined and could be built-in and provided/supported by Arch-Unit. And yes, #317 is definitely related to my question.

Glurz avatar Jul 01 '20 13:07 Glurz