Skip to content

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

Closed

Description

@lasselindqvist

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?

Activity

  1. lasselindqvist commented on Jun 8, 2024

    @lasselindqvist
    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.

  2. geoludbit commented on Sep 9, 2024

    @geoludbit

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

  3. lasselindqvist commented on Oct 11, 2024

    @lasselindqvist
    Author

    Here is a proper example of the workaround example for anyone wondering:

        @ArchTest
        public static final ArchRule rule = 
        	ArchRuleDefinition.fields()
            .that()
            .doNotHaveModifier(JavaModifier.SYNTHETIC)
            .and()
            .areDeclaredInClassesThat(CanBeAnnotated.Predicates.annotatedWith(Controller.class))
            .should(ArchConditions.beFinal());
    
  4. codecholeric commented on Nov 10, 2024

    @codecholeric
    Collaborator

    Sorry for the late reply and thanks for the workaround! It might make sense to ignore this by default in reasonable predicates. This is a good case where it likely never makes sense to consider synthetic fields.
    I've tried to exclude all synthetic members and classes by default at some point, but that actually caused a ton of problems back then. But for specific predefined predicates we could likely do this without any danger to overlook something 🤔

  5. sharanggupta commented on Oct 6, 2026

    @sharanggupta
    Contributor

    I'd like to take this one. Plan: make haveOnlyFinalFields() ignore synthetic fields (filtering JavaModifier.SYNTHETIC for that predefined condition, with the Javadoc updated to say so), leaving haveOnlyPrivateConstructors() untouched, plus a regression test built via the domain test builders since javac does not emit a non-final synthetic field. Happy to adjust scope if you prefer the filter in the shared condition.

  6. added 2 commits that reference this issue on Oct 7, 2026
    778995f
    3706aa5
  7. added a commit that references this issue on Oct 8, 2026
    ef17849
  8. hankem commented on Oct 8, 2026

    @hankem
    Member

    #1747 eventually added a new explicit method haveOnlyFinalNonSyntheticFields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions