diff --git a/core/pom.xml b/core/pom.xml
index e8e9bf249..5ebdb591b 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -56,6 +56,11 @@
auto-value-annotations
true
+
+ com.google.auto.service
+ auto-service-annotations
+ true
+
diff --git a/core/src/main/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProvider.java b/core/src/main/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProvider.java
new file mode 100644
index 000000000..7bcad4cc5
--- /dev/null
+++ b/core/src/main/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProvider.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2021 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 com.google.auto.service.AutoService;
+import java.io.PrintWriter;
+import java.util.spi.ToolProvider;
+
+/** Provide a way to be invoked without necessarily starting a new VM. */
+@AutoService(ToolProvider.class)
+public class GoogleJavaFormatToolProvider implements ToolProvider {
+ @Override
+ public String name() {
+ return "google-java-format";
+ }
+
+ @Override
+ public int run(PrintWriter out, PrintWriter err, String... args) {
+ try {
+ return Main.main(out, err, args);
+ } catch (RuntimeException e) {
+ err.print(e.getMessage());
+ return -1; // pass non-zero value back indicating an error has happened
+ }
+ }
+}
diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java
index 4056f2ae3..953ca5860 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/Main.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java
@@ -63,20 +63,27 @@ public Main(PrintWriter outWriter, PrintWriter errWriter, InputStream inStream)
* @param args the command-line arguments
*/
public static void main(String[] args) {
- int result;
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, UTF_8));
PrintWriter err = new PrintWriter(new OutputStreamWriter(System.err, UTF_8));
+ int result = main(out, err, args);
+ System.exit(result);
+ }
+
+ /**
+ * Package-private main entry point used this CLI program and the java.util.spi.ToolProvider
+ * implementation in the same package as this Main class.
+ */
+ static int main(PrintWriter out, PrintWriter err, String... args) {
try {
Main formatter = new Main(out, err, System.in);
- result = formatter.format(args);
+ return formatter.format(args);
} catch (UsageException e) {
err.print(e.getMessage());
- result = 0;
+ return 0;
} finally {
err.flush();
out.flush();
}
- System.exit(result);
}
/**
diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java
index 9c5838aeb..0361415a1 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java
@@ -27,7 +27,6 @@
import static com.google.googlejavaformat.java.javadoc.Token.Type.LIST_ITEM_OPEN_TAG;
import static com.google.googlejavaformat.java.javadoc.Token.Type.PARAGRAPH_OPEN_TAG;
-import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.googlejavaformat.java.javadoc.Token.Type;
@@ -395,7 +394,7 @@ private int innerIndent() {
// If this is a hotspot, keep a String of many spaces around, and call append(string, start, end).
private void appendSpaces(int count) {
- output.append(Strings.repeat(" ", count));
+ output.append(" ".repeat(count));
}
/**
diff --git a/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java
new file mode 100644
index 000000000..15e452297
--- /dev/null
+++ b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2021 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 com.google.common.truth.Truth8.assertThat;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ServiceLoader;
+import java.util.spi.ToolProvider;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link GoogleJavaFormatToolProvider}. */
+@RunWith(JUnit4.class)
+public class GoogleJavaFormatToolProviderTest {
+
+ @Test
+ public void testUsageOutputAfterLoadingViaToolName() {
+ String name = "google-java-format";
+
+ assertThat(
+ ServiceLoader.load(ToolProvider.class).stream()
+ .map(ServiceLoader.Provider::get)
+ .map(ToolProvider::name))
+ .contains(name);
+
+ ToolProvider format = ToolProvider.findFirst(name).get();
+
+ StringWriter out = new StringWriter();
+ StringWriter err = new StringWriter();
+
+ int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help");
+
+ assertThat(result).isEqualTo(0);
+
+ String usage = err.toString();
+
+ // Check that doc links are included.
+ assertThat(usage).containsMatch("http.*/google-java-format");
+ assertThat(usage).contains("Usage: google-java-format");
+ }
+}
diff --git a/pom.xml b/pom.xml
index c66ed2f23..75a18293d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -95,6 +95,7 @@
3.6.1
2.7.1
1.8.2
+ 1.0
3.1.0
3.2.1
@@ -124,6 +125,11 @@
auto-value-annotations
${auto-value.version}
+
+ com.google.auto.service
+ auto-service-annotations
+ ${auto-service.version}
+
@@ -220,6 +226,11 @@
auto-value
${auto-value.version}
+
+ com.google.auto.service
+ auto-service
+ ${auto-service.version}
+