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..5a4c522370 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 @@ -34,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.local.AddConstructor; @@ -57,8 +58,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 +86,7 @@ private LocalCacheFactoryGenerator(Path directory) { this.factoryTypes = new ArrayList<>(); } - private void generate() throws FormatterException, IOException { + private void generate() throws IOException { generateLocalCaches(); writeJavaFile(); reformat(); @@ -106,7 +105,7 @@ private void writeJavaFile() throws IOException { } } - private void reformat() throws FormatterException, IOException { + private void reformat() throws IOException { if (Boolean.parseBoolean(System.getenv("JDK_EA"))) { return; // may be incompatible for EA builds } @@ -114,11 +113,19 @@ private void reformat() throws FormatterException, IOException { ImmutableList files = stream .filter(path -> path.toString().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); + var formatter = ToolProvider.findFirst("google-java-format"); + if (formatter.isEmpty()) { + return; + } + int result = formatter + .get() + .run( + System.err, + System.out, + Stream.concat(Stream.of("-i"), files.stream().map(Path::toString)) + .toArray(String[]::new)); + if (result != 0) { + throw new IOException(); } } } @@ -210,7 +217,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..621c75a8ff 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 @@ -33,6 +33,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 +53,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 +88,7 @@ private NodeFactoryGenerator(Path directory) { this.nodeTypes = new ArrayList<>(); } - private void generate() throws FormatterException, IOException { + private void generate() throws IOException { generatedNodes(); writeJavaFile(); reformat(); @@ -108,7 +107,7 @@ private void writeJavaFile() throws IOException { } } - private void reformat() throws FormatterException, IOException { + private void reformat() throws IOException { if (Boolean.parseBoolean(System.getenv("JDK_EA"))) { return; // may be incompatible for EA builds } @@ -116,11 +115,20 @@ private void reformat() throws FormatterException, IOException { ImmutableList files = stream .filter(path -> path.toString().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); + var formatter = ToolProvider.findFirst("google-java-format"); + if (formatter.isEmpty()) { + return; + } + int result = + formatter + .get() + .run( + System.err, + System.out, + Stream.concat(Stream.of("-i"), files.stream().map(Path::toString)) + .toArray(String[]::new)); + if (result != 0) { + throw new IOException(); } } } @@ -217,7 +225,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(); } }