Skip to content

Commit 2c5d5fd

Browse files
java-team-github-botcushon
authored andcommitted
Automatic code cleanup.
COPYBARA_INTEGRATE_REVIEW=google#656 from sormuras:issues/561-toolprovider 82130f3 PiperOrigin-RevId: 396936217
1 parent 8490fce commit 2c5d5fd

File tree

6 files changed

+123
-6
lines changed

6 files changed

+123
-6
lines changed

core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
<artifactId>auto-value-annotations</artifactId>
5757
<optional>true</optional>
5858
</dependency>
59+
<dependency>
60+
<groupId>com.google.auto.service</groupId>
61+
<artifactId>auto-service-annotations</artifactId>
62+
<optional>true</optional>
63+
</dependency>
5964

6065
<!-- Test dependencies -->
6166
<dependency>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 com.google.auto.service.AutoService;
18+
import java.io.PrintWriter;
19+
import java.util.spi.ToolProvider;
20+
21+
/** Provide a way to be invoked without necessarily starting a new VM. */
22+
@AutoService(ToolProvider.class)
23+
public class GoogleJavaFormatToolProvider implements ToolProvider {
24+
@Override
25+
public String name() {
26+
return "google-java-format";
27+
}
28+
29+
@Override
30+
public int run(PrintWriter out, PrintWriter err, String... args) {
31+
try {
32+
return Main.main(out, err, args);
33+
} catch (RuntimeException e) {
34+
err.print(e.getMessage());
35+
return -1; // pass non-zero value back indicating an error has happened
36+
}
37+
}
38+
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,27 @@ public Main(PrintWriter outWriter, PrintWriter errWriter, InputStream inStream)
6363
* @param args the command-line arguments
6464
*/
6565
public static void main(String[] args) {
66-
int result;
6766
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, UTF_8));
6867
PrintWriter err = new PrintWriter(new OutputStreamWriter(System.err, UTF_8));
68+
int result = main(out, err, args);
69+
System.exit(result);
70+
}
71+
72+
/**
73+
* Package-private main entry point used this CLI program and the java.util.spi.ToolProvider
74+
* implementation in the same package as this Main class.
75+
*/
76+
static int main(PrintWriter out, PrintWriter err, String... args) {
6977
try {
7078
Main formatter = new Main(out, err, System.in);
71-
result = formatter.format(args);
79+
return formatter.format(args);
7280
} catch (UsageException e) {
7381
err.print(e.getMessage());
74-
result = 0;
82+
return 0;
7583
} finally {
7684
err.flush();
7785
out.flush();
7886
}
79-
System.exit(result);
8087
}
8188

8289
/**

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static com.google.googlejavaformat.java.javadoc.Token.Type.LIST_ITEM_OPEN_TAG;
2828
import static com.google.googlejavaformat.java.javadoc.Token.Type.PARAGRAPH_OPEN_TAG;
2929

30-
import com.google.common.base.Strings;
3130
import com.google.common.collect.ImmutableSet;
3231
import com.google.googlejavaformat.java.javadoc.Token.Type;
3332

@@ -395,7 +394,7 @@ private int innerIndent() {
395394

396395
// If this is a hotspot, keep a String of many spaces around, and call append(string, start, end).
397396
private void appendSpaces(int count) {
398-
output.append(Strings.repeat(" ", count));
397+
output.append(" ".repeat(count));
399398
}
400399

401400
/**
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
20+
import java.io.PrintWriter;
21+
import java.io.StringWriter;
22+
import java.util.ServiceLoader;
23+
import java.util.spi.ToolProvider;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
/** Tests for {@link GoogleJavaFormatToolProvider}. */
29+
@RunWith(JUnit4.class)
30+
public class GoogleJavaFormatToolProviderTest {
31+
32+
@Test
33+
public void testUsageOutputAfterLoadingViaToolName() {
34+
String name = "google-java-format";
35+
36+
assertThat(
37+
ServiceLoader.load(ToolProvider.class).stream()
38+
.map(ServiceLoader.Provider::get)
39+
.map(ToolProvider::name))
40+
.contains(name);
41+
42+
ToolProvider format = ToolProvider.findFirst(name).get();
43+
44+
StringWriter out = new StringWriter();
45+
StringWriter err = new StringWriter();
46+
47+
int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help");
48+
49+
assertThat(result).isEqualTo(0);
50+
51+
String usage = err.toString();
52+
53+
// Check that doc links are included.
54+
assertThat(usage).containsMatch("http.*/google-java-format");
55+
assertThat(usage).contains("Usage: google-java-format");
56+
}
57+
}

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<checker.version>3.6.1</checker.version>
9696
<errorprone.version>2.7.1</errorprone.version>
9797
<auto-value.version>1.8.2</auto-value.version>
98+
<auto-service.version>1.0</auto-service.version>
9899
<maven-javadoc-plugin.version>3.1.0</maven-javadoc-plugin.version>
99100
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
100101
</properties>
@@ -124,6 +125,11 @@
124125
<artifactId>auto-value-annotations</artifactId>
125126
<version>${auto-value.version}</version>
126127
</dependency>
128+
<dependency>
129+
<groupId>com.google.auto.service</groupId>
130+
<artifactId>auto-service-annotations</artifactId>
131+
<version>${auto-service.version}</version>
132+
</dependency>
127133

128134
<!-- Test dependencies -->
129135
<dependency>
@@ -220,6 +226,11 @@
220226
<artifactId>auto-value</artifactId>
221227
<version>${auto-value.version}</version>
222228
</path>
229+
<path>
230+
<groupId>com.google.auto.service</groupId>
231+
<artifactId>auto-service</artifactId>
232+
<version>${auto-service.version}</version>
233+
</path>
223234
</annotationProcessorPaths>
224235
</configuration>
225236
</plugin>

0 commit comments

Comments
 (0)