Skip to content

Preserve Set.of immutability in UseEnumSetOf - #1205

Open
martinfrancois wants to merge 3 commits into
openrewrite:mainfrom
martinfrancois:agent/repro-use-enum-set-immutability
Open

Preserve Set.of immutability in UseEnumSetOf#1205
martinfrancois wants to merge 3 commits into
openrewrite:mainfrom
martinfrancois:agent/repro-use-enum-set-immutability

Conversation

@martinfrancois

@martinfrancois martinfrancois commented Aug 15, 2026

Copy link
Copy Markdown

Suggested review order: 7 of 52 (Score: 8)
Review first: openrewrite/rewrite#8447

What's changed?

Wraps generated EnumSet values with Collections.unmodifiableSet(...) so UseEnumSetOf preserves the mutation contract of Set.of(...). When source names prevent both safe spellings of Collections, the recipe leaves the original expression unchanged.

What's your motivation?

Recipe: org.openrewrite.java.migrate.util.UseEnumSetOf.

I found this by running org.openrewrite.java.migrate.util.UseEnumSetOf from org.openrewrite.recipe:rewrite-migrate-java:3.40.0 on WorkflowConfigEditor.java in Symphony-Trello at a8013f27. I reproduced the same result with the latest released recipe artifact, org.openrewrite.recipe:rewrite-migrate-java:3.42.0.

Before

private static final Set<PosixFilePermission> POSIX_WRITE_PERMISSIONS =
        Set.of(PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE);

Actual after the recipe

private static final Set<PosixFilePermission> POSIX_WRITE_PERMISSIONS =
        EnumSet.of(PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE);

Expected after the recipe

private static final Set<PosixFilePermission> POSIX_WRITE_PERMISSIONS =
        Collections.unmodifiableSet(EnumSet.of(
                PosixFilePermission.OWNER_WRITE,
                PosixFilePermission.GROUP_WRITE,
                PosixFilePermission.OTHERS_WRITE));

Set.of returns an unmodifiable set, while EnumSet.of returns a mutable set. The recipe changes an observable contract of the static constant even though the declared type remains Set.

Confirmed real-world execution

Anything in particular you'd like reviewers to focus on?

Please review the decision to retain the intended EnumSet representation while preserving the source set's mutation contract and the conservative no-change result when neither Collections spelling is safe.

Have you considered any alternatives or workarounds?

Leaving every Set.of(...) expression unchanged preserves behavior but removes the standalone recipe's intended conversion. The unmodifiable wrapper retains both properties.

Any additional context

Pre-existing tests changed: UseEnumSetOfTest.java.changeDeclaration (updated), UseEnumSetOfTest.java.changeAssignment (updated), UseEnumSetOfTest.java.dontHaveArgs (updated).

This change was prepared with AI assistance. I reviewed the target execution evidence, implementation, tests, generated metadata, and contribution text.

Checklist

@martinfrancois
martinfrancois force-pushed the agent/repro-use-enum-set-immutability branch from 7739d31 to 5b984a3 Compare August 16, 2026 02:53
@martinfrancois
martinfrancois marked this pull request as ready for review August 17, 2026 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant