'Archunit: How to control usage one class in all other packages

I have class from the different repository which has public methods and has been used in the class with import.

Example:

com.tables.Field class which has public Methods from different repository and has been used in the project.

I want above Field class to be used only by class in the following package com.test.FieldImpl and should not be used by other packages in the project.

How to write Predicate for this condition

private final ArchRule table_should_access_only_impl = classes().that().
            haveFullyQualifiedName("com.tables.Field").should().accessTargetWhere(?);


Solution 1:[1]

I think that you're looking for something like this:

ArchRule table_should_access_only_impl = noClasses()
    .that().doNotHaveFullyQualifiedName("com.test.FieldImpl")
    .should().accessClassesThat().haveFullyQualifiedName("com.tables.Field");

Instead of accessClassesThat, you can also use dependOnClassesThat, which covers more dependencies than just field access or method calls.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manfred