Skip to content

Dependency check of Enum Types in Switch-Expression/Statement #570

Closed
@bfncs

Description

If you define a dependency check for an Enum Type that is used in a switch-Expression (or old style Statement) this leads to a false positive failed test. I'll try to explain this better with an example:

If you define this Enum in a package lib:

public enum ExampleEnum {
  FOO,
  BAR
}

…and you use it like this in a package app:

public static String failingExample1(final ExampleEnum exampleEnum) {
	return switch (exampleEnum) {
		case FOO -> "foo";
		case BAR -> "bar";
	};
}

…and you have a test containing a check to assert classes in package app only depend on class that reside in package lib:

@ArchTest
public static final ArchRule DEPENDENCY_TEST_EXAMPLE =
    classes()
        .that()
        .resideInAPackage("..app..")
        .should()
        .onlyDependOnClassesThat(resideInAPackage("..lib.."));

…the Test fails with the following unexpected Architecture Violation:

Method <us.byteb.archunit.app.Example.failingExample1(us.byteb.archunit.lib.ExampleEnum)> gets field <us.byteb.archunit.app.Example$1.$SwitchMap$us$byteb$archunit$lib$ExampleEnum> in (Example.java:8)

This is also true if you refactor the same logic to use and old-style switch-Statement but goes away if you use if-Statements instead. I would expect all three semantically equal options to produce the same result: a successful test.

You can find a compiling implementation of the tests in this repository (Problem happens with Java 8, 11, 14 & 16). Please let me know, if I can be of any help in resolving it.

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions