Skip to content

Commit 8cf65c7

Browse files
java-team-github-botgoogle-java-format Team
authored andcommitted
Automated rollback of commit 4c91240.
*** Reason for rollback *** Breaks test/protobuf.msan [failure]([] *** Original change description *** Internal change PiperOrigin-RevId: 745520292
1 parent 4c91240 commit 8cf65c7

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ record CommandLineOptions(
5757
boolean setExitIfChanged,
5858
Optional<String> assumeFilename,
5959
boolean reflowLongStrings,
60-
boolean formatJavadoc,
61-
Optional<String> profile) {
60+
boolean formatJavadoc) {
6261

6362
/** Returns true if partial formatting was selected. */
6463
boolean isSelection() {
@@ -130,8 +129,6 @@ default Builder addLength(Integer length) {
130129

131130
Builder formatJavadoc(boolean formatJavadoc);
132131

133-
Builder profile(String profile);
134-
135132
CommandLineOptions build();
136133
}
137134
}

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17+
import static java.nio.charset.StandardCharsets.UTF_8;
1718

1819
import com.google.common.base.CharMatcher;
1920
import com.google.common.base.Splitter;
@@ -25,6 +26,7 @@
2526
import java.io.UncheckedIOException;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;
29+
import java.nio.file.Paths;
2830
import java.util.ArrayList;
2931
import java.util.Iterator;
3032
import java.util.List;
@@ -62,67 +64,72 @@ static CommandLineOptions parse(Iterable<String> options) {
6264
flag = option;
6365
value = null;
6466
}
65-
flag = flag.startsWith("--") ? flag.substring(1) : flag;
6667
// NOTE: update usage information in UsageException when new flags are added
6768
switch (flag) {
6869
case "-i":
6970
case "-r":
7071
case "-replace":
72+
case "--replace":
7173
optionsBuilder.inPlace(true);
7274
break;
75+
case "--lines":
7376
case "-lines":
77+
case "--line":
7478
case "-line":
7579
parseRangeSet(linesBuilder, getValue(flag, it, value));
7680
break;
81+
case "--offset":
7782
case "-offset":
7883
optionsBuilder.addOffset(parseInteger(it, flag, value));
7984
break;
85+
case "--length":
8086
case "-length":
8187
optionsBuilder.addLength(parseInteger(it, flag, value));
8288
break;
89+
case "--aosp":
8390
case "-aosp":
8491
case "-a":
8592
optionsBuilder.aosp(true);
8693
break;
94+
case "--version":
8795
case "-version":
8896
case "-v":
8997
optionsBuilder.version(true);
9098
break;
99+
case "--help":
91100
case "-help":
92101
case "-h":
93102
optionsBuilder.help(true);
94103
break;
95-
case "-fix-imports-only":
104+
case "--fix-imports-only":
96105
optionsBuilder.fixImportsOnly(true);
97106
break;
98-
case "-skip-sorting-imports":
107+
case "--skip-sorting-imports":
99108
optionsBuilder.sortImports(false);
100109
break;
101-
case "-skip-removing-unused-imports":
110+
case "--skip-removing-unused-imports":
102111
optionsBuilder.removeUnusedImports(false);
103112
break;
104-
case "-skip-reflowing-long-strings":
113+
case "--skip-reflowing-long-strings":
105114
optionsBuilder.reflowLongStrings(false);
106115
break;
107-
case "-skip-javadoc-formatting":
116+
case "--skip-javadoc-formatting":
108117
optionsBuilder.formatJavadoc(false);
109118
break;
110119
case "-":
111120
optionsBuilder.stdin(true);
112121
break;
113122
case "-n":
114-
case "-dry-run":
123+
case "--dry-run":
115124
optionsBuilder.dryRun(true);
116125
break;
117-
case "-set-exit-if-changed":
126+
case "--set-exit-if-changed":
118127
optionsBuilder.setExitIfChanged(true);
119128
break;
120129
case "-assume-filename":
130+
case "--assume-filename":
121131
optionsBuilder.assumeFilename(getValue(flag, it, value));
122132
break;
123-
case "-profile":
124-
optionsBuilder.profile(getValue(flag, it, value));
125-
break;
126133
default:
127134
throw new IllegalArgumentException("unexpected flag: " + flag);
128135
}
@@ -195,16 +202,14 @@ private static void expandParamsFiles(Iterable<String> args, List<String> expand
195202
} else if (arg.startsWith("@@")) {
196203
expanded.add(arg.substring(1));
197204
} else {
198-
Path path = Path.of(arg.substring(1));
205+
Path path = Paths.get(arg.substring(1));
199206
try {
200-
String sequence = Files.readString(path);
207+
String sequence = new String(Files.readAllBytes(path), UTF_8);
201208
expandParamsFiles(ARG_SPLITTER.split(sequence), expanded);
202209
} catch (IOException e) {
203210
throw new UncheckedIOException(path + ": could not read file: " + e.getMessage(), e);
204211
}
205212
}
206213
}
207214
}
208-
209-
private CommandLineOptionsParser() {}
210215
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ static int main(InputStream in, PrintWriter out, PrintWriter err, String... args
108108
*/
109109
public int format(String... args) throws UsageException {
110110
CommandLineOptions parameters = processArgs(args);
111-
return format(parameters);
112-
}
113-
114-
int format(CommandLineOptions parameters) {
115111
if (parameters.version()) {
116112
errWriter.println(versionString());
117113
return 0;
118114
}
115+
if (parameters.help()) {
116+
throw new UsageException();
117+
}
118+
119119
JavaFormatterOptions options =
120120
JavaFormatterOptions.builder()
121121
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
@@ -278,9 +278,6 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
278278
if (parameters.dryRun() && parameters.inPlace()) {
279279
throw new UsageException("cannot use --dry-run and --in-place at the same time");
280280
}
281-
if (parameters.help()) {
282-
throw new UsageException();
283-
}
284281
return parameters;
285282
}
286283
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,7 @@ public void numberOfOffsetsMustMatchNumberOfLengths() throws UsageException {
107107
public void noFilesToFormatRequiresEitherHelpOrVersion() throws UsageException {
108108
Main.processArgs("-version");
109109

110-
try {
111-
Main.processArgs("-help");
112-
fail();
113-
} catch (UsageException e) {
114-
// expected
115-
}
110+
Main.processArgs("-help");
116111

117112
try {
118113
Main.processArgs();

0 commit comments

Comments
 (0)