Search code examples
javagenericsarchunit

ArchUnit doesn't detect generic forbidden dependencies


I'm using ArchUnit to detect forbidden dependencies from the controller (C) to the entity (E).

Example :

layeredArchitecture()
    .consideringOnlyDependenciesInLayers()
    .layer("Controller").definedBy(controllerPredicate)
    .layer("Service").definedBy(servicePredicate)
    .layer("Entity").definedBy(entityPredicate)

    .whereLayer("Controller").mayOnlyAccessLayers("Service")
    .whereLayer("Entity").mayOnlyBeAccessedByLayers("Service")
    .check(...);

The dependency is detected if there is a direct dependency from C to E :

E entity = new E();

But it's not detected if there is a depenency from C to Optional<E> :

Optional<E> optionalEntity = service.returningAnOptionalEntity();

I'm using the 1.0.1 version. And I've seen here that generics should be supported since 0.0.20 but it doesn't seem to.

Any way to detect these generic dependencies ?


Solution

  • Unless you use a specific API of your entity (i.e. call a method or access a field) in your controller, ArchUnit cannot detect a dependency (see also ArchUnit#1012, which basically describes the same use case).

    This is unrelated to generics; you wouldn't get a violation even if your service returned a non-Optional entity E.