Skip to content

Commit

Permalink
Use the ToolProvider SPI to run google-java-format
Browse files Browse the repository at this point in the history
This makes the compile-time dependency on google-java-format optional.

Co-authored-by: Ben Manes <ben.manes@gmail.com>
  • Loading branch information
cushon and ben-manes committed Nov 7, 2024
1 parent 832ab2c commit 5fb9ead
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
6 changes: 4 additions & 2 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -244,7 +246,7 @@ tasks.named<CheckForbiddenApis>("forbiddenApisMain").configure {

tasks.named<CheckForbiddenApis>("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<CheckForbiddenApis>("forbiddenApisTest").configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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<Path> stream = Files.walk(directory)) {
ImmutableList<Path> files = stream
.filter(path -> path.toString().endsWith(".java"))
ImmutableList<String> 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);
});
}
}

Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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<Path> stream = Files.walk(directory)) {
ImmutableList<Path> files = stream
.filter(path -> path.toString().endsWith(".java"))
ImmutableList<String> 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);
});
}
}

Expand Down Expand Up @@ -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();
}
}

0 comments on commit 5fb9ead

Please sign in to comment.