Skip to content

Commit 205b85f

Browse files
java-team-github-botgoogle-java-format Team
authored andcommitted
Add javax.tools.Tool implementation for google-java-format.
PiperOrigin-RevId: 586765411
1 parent 53390d9 commit 205b85f

File tree

5 files changed

+138
-10
lines changed

5 files changed

+138
-10
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.googlejavaformat.java;
16+
17+
import static com.google.common.collect.Sets.toImmutableEnumSet;
18+
19+
import com.google.auto.service.AutoService;
20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
import java.io.PrintStream;
23+
import java.util.Arrays;
24+
import java.util.Set;
25+
import javax.lang.model.SourceVersion;
26+
import javax.tools.Tool;
27+
28+
/** Provide a way to be invoked without necessarily starting a new VM. */
29+
@AutoService(Tool.class)
30+
public class GoogleJavaFormatTool implements Tool {
31+
@Override
32+
public String name() {
33+
return "google-java-format";
34+
}
35+
36+
@Override
37+
public Set<SourceVersion> getSourceVersions() {
38+
return Arrays.stream(SourceVersion.values()).collect(toImmutableEnumSet());
39+
}
40+
41+
@Override
42+
public int run(InputStream in, OutputStream out, OutputStream err, String... args) {
43+
PrintStream outStream = new PrintStream(out);
44+
PrintStream errStream = new PrintStream(err);
45+
try {
46+
return Main.main(in, outStream, errStream, args);
47+
} catch (RuntimeException e) {
48+
errStream.print(e.getMessage());
49+
errStream.flush();
50+
return 1; // pass non-zero value back indicating an error has happened
51+
}
52+
}
53+
}

core/src/main/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public String name() {
2929
@Override
3030
public int run(PrintWriter out, PrintWriter err, String... args) {
3131
try {
32-
return Main.main(out, err, args);
32+
return Main.main(System.in, out, err, args);
3333
} catch (RuntimeException e) {
3434
err.print(e.getMessage());
35-
return -1; // pass non-zero value back indicating an error has happened
35+
err.flush();
36+
return 1; // pass non-zero value back indicating an error has happened
3637
}
3738
}
3839
}

core/src/main/java/com/google/googlejavaformat/java/Main.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.io.OutputStreamWriter;
29+
import java.io.PrintStream;
2930
import java.io.PrintWriter;
3031
import java.nio.file.Files;
3132
import java.nio.file.Path;
@@ -66,20 +67,28 @@ public Main(PrintWriter outWriter, PrintWriter errWriter, InputStream inStream)
6667
*
6768
* @param args the command-line arguments
6869
*/
69-
public static void main(String[] args) {
70-
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, UTF_8));
71-
PrintWriter err = new PrintWriter(new OutputStreamWriter(System.err, UTF_8));
72-
int result = main(out, err, args);
70+
public static void main(String... args) {
71+
int result = main(System.in, System.out, System.err, args);
7372
System.exit(result);
7473
}
7574

7675
/**
77-
* Package-private main entry point used this CLI program and the java.util.spi.ToolProvider
76+
* Package-private main entry point used by the {@link javax.tools.Tool Tool} implementation in
77+
* the same package as this Main class.
78+
*/
79+
static int main(InputStream in, PrintStream out, PrintStream err, String... args) {
80+
PrintWriter outWriter = new PrintWriter(new OutputStreamWriter(out, UTF_8));
81+
PrintWriter errWriter = new PrintWriter(new OutputStreamWriter(err, UTF_8));
82+
return main(in, outWriter, errWriter, args);
83+
}
84+
85+
/**
86+
* Package-private main entry point used by the {@link java.util.spi.ToolProvider ToolProvider}
7887
* implementation in the same package as this Main class.
7988
*/
80-
static int main(PrintWriter out, PrintWriter err, String... args) {
89+
static int main(InputStream in, PrintWriter out, PrintWriter err, String... args) {
8190
try {
82-
Main formatter = new Main(out, err, System.in);
91+
Main formatter = new Main(out, err, in);
8392
return formatter.format(args);
8493
} catch (UsageException e) {
8594
err.print(e.getMessage());

core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void testUsageOutputAfterLoadingViaToolName() {
4646

4747
int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help");
4848

49-
assertThat(result).isEqualTo(2);
49+
assertThat(result).isNotEqualTo(0);
5050

5151
String usage = err.toString();
5252

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.googlejavaformat.java;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.common.truth.Truth8.assertThat;
19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.InputStream;
24+
import java.util.ServiceLoader;
25+
import javax.tools.Tool;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
/** Tests for {@link GoogleJavaFormatToolProvider}. */
31+
@RunWith(JUnit4.class)
32+
public class GoogleJavaFormatToolTest {
33+
34+
@Test
35+
public void testUsageOutputAfterLoadingViaToolName() {
36+
String name = "google-java-format";
37+
38+
assertThat(
39+
ServiceLoader.load(Tool.class).stream()
40+
.map(ServiceLoader.Provider::get)
41+
.map(Tool::name))
42+
.contains(name);
43+
44+
Tool format =
45+
ServiceLoader.load(Tool.class).stream()
46+
.filter(provider -> name.equals(provider.get().name()))
47+
.findFirst()
48+
.get()
49+
.get();
50+
51+
InputStream in = new ByteArrayInputStream(new byte[0]);
52+
ByteArrayOutputStream out = new ByteArrayOutputStream();
53+
ByteArrayOutputStream err = new ByteArrayOutputStream();
54+
55+
int result = format.run(in, out, err, "--help");
56+
57+
assertThat(result).isNotEqualTo(0);
58+
59+
String usage = new String(err.toByteArray(), UTF_8);
60+
61+
// Check that doc links are included.
62+
assertThat(usage).containsMatch("http.*/google-java-format");
63+
assertThat(usage).contains("Usage: google-java-format");
64+
}
65+
}

0 commit comments

Comments
 (0)