From 5fb9eadf85164d04cc5720e19c6c8b1df70bf20c Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 6 Nov 2024 15:32:12 -0800 Subject: [PATCH] Use the ToolProvider SPI to run google-java-format This makes the compile-time dependency on google-java-format optional. Co-authored-by: Ben Manes --- caffeine/build.gradle.kts | 6 +++-- .../cache/LocalCacheFactoryGenerator.java | 27 ++++++++++--------- .../caffeine/cache/NodeFactoryGenerator.java | 27 ++++++++++--------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/caffeine/build.gradle.kts b/caffeine/build.gradle.kts index a916638f1e..1441c07949 100644 --- a/caffeine/build.gradle.kts +++ b/caffeine/build.gradle.kts @@ -25,6 +25,7 @@ sourceSets { val compileJavaPoetJava by tasks.existing val jammAgent: Configuration by configurations.creating val collections4Sources: Configuration by configurations.creating +val javaPoetRuntimeOnly: Configuration = configurations["javaPoetRuntimeOnly"] val javaPoetImplementation: Configuration = configurations["javaPoetImplementation"] dependencies { @@ -75,7 +76,8 @@ dependencies { javaPoetImplementation(libs.guava) javaPoetImplementation(libs.javapoet) javaPoetImplementation(libs.commons.lang3) - javaPoetImplementation(libs.google.java.format) + + javaPoetRuntimeOnly(libs.google.java.format) } val compileCodeGenJava by tasks.existing(JavaCompile::class) { @@ -244,7 +246,7 @@ tasks.named("forbiddenApisMain").configure { tasks.named("forbiddenApisJavaPoet").configure { bundledSignatures.addAll(listOf("jdk-deprecated", "jdk-internal", - "jdk-non-portable", "jdk-reflection", "jdk-system-out", "jdk-unsafe")) + "jdk-non-portable", "jdk-reflection", "jdk-unsafe")) } tasks.named("forbiddenApisTest").configure { diff --git a/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/LocalCacheFactoryGenerator.java b/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/LocalCacheFactoryGenerator.java index d61f60c99e..ec02741647 100644 --- a/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/LocalCacheFactoryGenerator.java +++ b/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/LocalCacheFactoryGenerator.java @@ -18,6 +18,7 @@ import static com.github.benmanes.caffeine.cache.Specifications.BOUNDED_LOCAL_CACHE; import static com.github.benmanes.caffeine.cache.Specifications.kTypeVar; import static com.github.benmanes.caffeine.cache.Specifications.vTypeVar; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.ImmutableList.toImmutableList; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Objects.requireNonNull; @@ -34,6 +35,7 @@ import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; +import java.util.spi.ToolProvider; import java.util.stream.Stream; import com.github.benmanes.caffeine.cache.local.AddConstructor; @@ -57,8 +59,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.common.io.Resources; -import com.google.googlejavaformat.java.Formatter; -import com.google.googlejavaformat.java.FormatterException; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.ParameterizedTypeName; @@ -87,7 +87,7 @@ private LocalCacheFactoryGenerator(Path directory) { this.factoryTypes = new ArrayList<>(); } - private void generate() throws FormatterException, IOException { + private void generate() throws IOException { generateLocalCaches(); writeJavaFile(); reformat(); @@ -106,20 +106,21 @@ private void writeJavaFile() throws IOException { } } - private void reformat() throws FormatterException, IOException { + @SuppressWarnings("SystemOut") + private void reformat() throws IOException { if (Boolean.parseBoolean(System.getenv("JDK_EA"))) { return; // may be incompatible for EA builds } try (Stream stream = Files.walk(directory)) { - ImmutableList files = stream - .filter(path -> path.toString().endsWith(".java")) + ImmutableList files = stream + .map(Path::toString) + .filter(path -> path.endsWith(".java")) .collect(toImmutableList()); - var formatter = new Formatter(); - for (Path file : files) { - String source = Files.readString(file); - String formatted = formatter.formatSourceAndFixImports(source); - Files.writeString(file, formatted); - } + ToolProvider.findFirst("google-java-format").ifPresent(formatter -> { + int result = formatter.run(System.err, System.out, + Stream.concat(Stream.of("-i"), files.stream()).toArray(String[]::new)); + checkState(result == 0, "Java formatting failed with %s exit code", result); + }); } } @@ -210,7 +211,7 @@ private static String encode(String className) { .replaceFirst("_REFRESH_WRITE", "R"); } - public static void main(String[] args) throws FormatterException, IOException { + public static void main(String[] args) throws IOException { new LocalCacheFactoryGenerator(Path.of(args[0])).generate(); } } diff --git a/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/NodeFactoryGenerator.java b/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/NodeFactoryGenerator.java index ae4889c348..65d5714197 100644 --- a/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/NodeFactoryGenerator.java +++ b/caffeine/src/javaPoet/java/com/github/benmanes/caffeine/cache/NodeFactoryGenerator.java @@ -18,6 +18,7 @@ import static com.github.benmanes.caffeine.cache.Specifications.PACKAGE_NAME; import static com.github.benmanes.caffeine.cache.Specifications.kTypeVar; import static com.github.benmanes.caffeine.cache.Specifications.vTypeVar; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.ImmutableList.toImmutableList; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Objects.requireNonNull; @@ -33,6 +34,7 @@ import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; +import java.util.spi.ToolProvider; import java.util.stream.Stream; import com.github.benmanes.caffeine.cache.node.AddConstructors; @@ -52,8 +54,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.common.io.Resources; -import com.google.googlejavaformat.java.Formatter; -import com.google.googlejavaformat.java.FormatterException; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.ParameterizedTypeName; @@ -89,7 +89,7 @@ private NodeFactoryGenerator(Path directory) { this.nodeTypes = new ArrayList<>(); } - private void generate() throws FormatterException, IOException { + private void generate() throws IOException { generatedNodes(); writeJavaFile(); reformat(); @@ -108,20 +108,21 @@ private void writeJavaFile() throws IOException { } } - private void reformat() throws FormatterException, IOException { + @SuppressWarnings("SystemOut") + private void reformat() throws IOException { if (Boolean.parseBoolean(System.getenv("JDK_EA"))) { return; // may be incompatible for EA builds } try (Stream stream = Files.walk(directory)) { - ImmutableList files = stream - .filter(path -> path.toString().endsWith(".java")) + ImmutableList files = stream + .map(Path::toString) + .filter(path -> path.endsWith(".java")) .collect(toImmutableList()); - var formatter = new Formatter(); - for (Path file : files) { - String source = Files.readString(file); - String formatted = formatter.formatSourceAndFixImports(source); - Files.writeString(file, formatted); - } + ToolProvider.findFirst("google-java-format").ifPresent(formatter -> { + int result = formatter.run(System.err, System.out, + Stream.concat(Stream.of("-i"), files.stream()).toArray(String[]::new)); + checkState(result == 0, "Java formatting failed with %s exit code", result); + }); } } @@ -217,7 +218,7 @@ private static String encode(String className) { .replaceFirst("_SIZE", "S"); } - public static void main(String[] args) throws FormatterException, IOException { + public static void main(String[] args) throws IOException { new NodeFactoryGenerator(Path.of(args[0])).generate(); } }