• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
name()32   public String name() {
33     return "google-java-format";
34   }
35 
36   @Override
getSourceVersions()37   public Set<SourceVersion> getSourceVersions() {
38     return Arrays.stream(SourceVersion.values()).collect(toImmutableEnumSet());
39   }
40 
41   @Override
run(InputStream in, OutputStream out, OutputStream err, String... args)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 }
54