Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArchUnit thinks "Switch with arrows" produces non-final fields #1313

Open
lasselindqvist opened this issue May 31, 2024 · 2 comments
Open

ArchUnit thinks "Switch with arrows" produces non-final fields #1313

lasselindqvist opened this issue May 31, 2024 · 2 comments

Comments

@lasselindqvist
Copy link

Example code:

having some method with a call inside it like:

			var status = service.waitForResult(taskId, Duration.ofMinutes(1));
			switch (status) {
			case COMPLETED -> result.setStatus(StatusEnum.COMPLETED);
			case FAILED -> result.setStatus(StatusEnum.FAILED);
			default -> result.setStatus(StatusEnum.PENDING);
			}

causes a violation like
Field <com.controller.Controller.$SWITCH_TABLE$...$StatusEnum> is not final in (Controller.java:0)

with a test

    @ArchTest
    public static final ArchRule rule = ArchRuleDefinition.classes()
        .that()
        .areAnnotatedWith(Controller.class)
        .should().haveOnlyFinalFields();

Maybe this is the fault of the compiler used (Eclipse), but I wonder if ArchUnit can ignore these switch tables when checking the rule?

@lasselindqvist
Copy link
Author

I guess this can be handled by skipping fields with JavaModifier.SYNTHETIC. Even if we don't want to do that in the default haveOnlyFinalFields method, maybe it would be good to show/document this workaround somehow.

I suppose one way is to check this using ArchRuleDefinition::fields, filtering with ‎MembersThatInternal::doNotHaveModifier(JavaModifier.SYNTHETIC) and then check for the final modifier.

@geoludbit
Copy link

Is this going to be fixed in some version or is there a workaround? We are having this issue with java 21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants