From ab11ed6a272f5383394f7721428281dd9ddbc9e1 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 3 Oct 2026 20:25:01 +0800 Subject: [PATCH] Restore enum-compatible methods on JavaFormatterOptions.Style `Style` was changed from an enum to a record, which removed the implicit enum methods. Tools such as Spotless call `Style.valueOf(String)` to map user configuration to a style, and fail with `NoSuchMethodError` against the new version. --- .../java/JavaFormatterOptions.java | 41 ++++++++++++++ .../java/JavaFormatterOptionsTest.java | 55 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 core/src/test/java/com/google/googlejavaformat/java/JavaFormatterOptionsTest.java diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java index 65fcce4eb..d58aac4ea 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java @@ -66,6 +66,47 @@ public record Style( /** The AOSP-compliant configuration. */ public static final Style AOSP = builder().aosp().build(); + /** + * Returns the predefined styles, in the order they were declared when {@code Style} was an + * enum. + * + *

Retained for compatibility with callers compiled against the enum version of {@code + * Style}. + */ + public static Style[] values() { + return new Style[] {GOOGLE, AOSP}; + } + + /** + * Returns the predefined style with the given name ({@code "GOOGLE"} or {@code "AOSP"}). + * + *

Retained for compatibility with callers compiled against the enum version of {@code + * Style}. + * + * @throws IllegalArgumentException if there is no predefined style with the given name + * @throws NullPointerException if {@code name} is null + */ + public static Style valueOf(String name) { + return switch (requireNonNull(name, "Name is null")) { + case "GOOGLE" -> GOOGLE; + case "AOSP" -> AOSP; + default -> + throw new IllegalArgumentException( + "No enum constant " + Style.class.getCanonicalName() + "." + name); + }; + } + + /** + * Returns the name of the predefined style this style is based on ({@code "GOOGLE"} or {@code + * "AOSP"}), such that {@code valueOf(GOOGLE.name()) == GOOGLE}. + * + *

Retained for compatibility with callers compiled against the enum version of {@code + * Style}. + */ + public String name() { + return importOrder().name(); + } + /** * Returns the visual column width of a tab stop. * diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavaFormatterOptionsTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavaFormatterOptionsTest.java new file mode 100644 index 000000000..b818337b8 --- /dev/null +++ b/core/src/test/java/com/google/googlejavaformat/java/JavaFormatterOptionsTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2026 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.googlejavaformat.java; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; + +import com.google.googlejavaformat.java.JavaFormatterOptions.Style; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link JavaFormatterOptions}. */ +@RunWith(JUnit4.class) +public final class JavaFormatterOptionsTest { + + @Test + public void styleValues() { + assertThat(Style.values()).asList().containsExactly(Style.GOOGLE, Style.AOSP).inOrder(); + } + + @Test + public void styleValueOf() { + assertThat(Style.valueOf("GOOGLE")).isSameInstanceAs(Style.GOOGLE); + assertThat(Style.valueOf("AOSP")).isSameInstanceAs(Style.AOSP); + } + + @Test + public void styleValueOf_invalid() { + assertThrows(IllegalArgumentException.class, () -> Style.valueOf("google")); + assertThrows(IllegalArgumentException.class, () -> Style.valueOf("PALANTIR")); + assertThrows(NullPointerException.class, () -> Style.valueOf(null)); + } + + @Test + public void styleName() { + for (Style style : Style.values()) { + assertThat(Style.valueOf(style.name())).isSameInstanceAs(style); + } + assertThat(Style.AOSP.toBuilder().maxLineLength(120).useTabs(true).build().name()) + .isEqualTo("AOSP"); + } +}