Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.

Commit 6ef9eba

Browse files
committed
Remove try with resources.
1 parent c047bbe commit 6ef9eba

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed

src/com/google/javascript/jscomp/AbstractCommandLineRunner.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,16 @@ public FlagUsageException(String message) {
573573

574574
public List<JsonFileSpec> parseJsonFilesFromInputStream() throws IOException {
575575
List<JsonFileSpec> jsonFiles = new ArrayList<JsonFileSpec>();
576-
try (JsonReader reader = new JsonReader(new InputStreamReader(this.in, inputCharset))) {
576+
JsonReader reader = new JsonReader(new InputStreamReader(this.in, inputCharset));
577+
try {
577578
reader.beginArray();
578579
while (reader.hasNext()) {
579580
JsonFileSpec jsonFile = gson.fromJson(reader, JsonFileSpec.class);
580581
jsonFiles.add(jsonFile);
581582
}
582583
reader.endArray();
584+
} finally {
585+
reader.close();
583586
}
584587
return jsonFiles;
585588
}
@@ -1216,8 +1219,11 @@ int processResults(Result result, List<JSModule> modules, B options) throws IOEx
12161219

12171220
// Output the externs if required.
12181221
if (options.externExportsPath != null) {
1219-
try (Writer eeOut = openExternExportsStream(options, config.jsOutputFile)) {
1222+
Writer eeOut = openExternExportsStream(options, config.jsOutputFile);
1223+
try {
12201224
eeOut.append(result.externExport);
1225+
} finally {
1226+
eeOut.close();
12211227
}
12221228
}
12231229

@@ -1289,8 +1295,9 @@ JsonFileSpec createJsonFile(B options, String outputMarker,
12891295
}
12901296

12911297
void outputJsonStream() throws IOException {
1292-
try (JsonWriter jsonWriter =
1293-
new JsonWriter(new BufferedWriter(new OutputStreamWriter(defaultJsOutput, "UTF-8")))) {
1298+
JsonWriter jsonWriter =
1299+
new JsonWriter(new BufferedWriter(new OutputStreamWriter(defaultJsOutput, "UTF-8")));
1300+
try {
12941301
jsonWriter.beginArray();
12951302
for (JsonFileSpec jsonFile : this.filesToStreamOut) {
12961303
jsonWriter.beginObject();
@@ -1302,6 +1309,8 @@ void outputJsonStream() throws IOException {
13021309
jsonWriter.endObject();
13031310
}
13041311
jsonWriter.endArray();
1312+
} finally {
1313+
jsonWriter.close();
13051314
}
13061315
}
13071316

@@ -1335,14 +1344,17 @@ private DiagnosticType outputModuleBinaryAndSourceMaps(List<JSModule> modules, B
13351344
}
13361345

13371346
String moduleFilename = getModuleOutputFileName(m);
1338-
try (Writer writer = fileNameToLegacyOutputWriter(moduleFilename)) {
1347+
Writer writer = fileNameToLegacyOutputWriter(moduleFilename);
1348+
try {
13391349
if (options.sourceMapOutputPath != null) {
13401350
compiler.getSourceMap().reset();
13411351
}
13421352
writeModuleOutput(writer, m);
13431353
if (options.sourceMapOutputPath != null) {
13441354
compiler.getSourceMap().appendTo(mapFileOut, moduleFilename);
13451355
}
1356+
} finally {
1357+
writer.close();
13461358
}
13471359

13481360
if (shouldGenerateMapPerModule(options) && mapFileOut != null) {
@@ -1595,8 +1607,11 @@ private void outputSourceMap(B options, String associatedName)
15951607

15961608
String outName = expandSourceMapPath(options, null);
15971609
maybeCreateDirsForPath(outName);
1598-
try (Writer out = fileNameToOutputWriter2(outName)) {
1610+
Writer out = fileNameToOutputWriter2(outName);
1611+
try {
15991612
compiler.getSourceMap().appendTo(out, associatedName);
1613+
} finally {
1614+
out.close();
16001615
}
16011616
}
16021617

@@ -1689,10 +1704,13 @@ private void outputNameMaps() throws IOException {
16891704

16901705
if (functionInformationMapOutputPath != null
16911706
&& compiler.getFunctionalInformationMap() != null) {
1692-
try (final OutputStream file = filenameToOutputStream(functionInformationMapOutputPath)) {
1707+
final OutputStream file = filenameToOutputStream(functionInformationMapOutputPath);
1708+
try {
16931709
CodedOutputStream outputStream = CodedOutputStream.newInstance(file);
16941710
compiler.getFunctionalInformationMap().writeTo(outputStream);
16951711
outputStream.flush();
1712+
} finally {
1713+
file.close();
16961714
}
16971715
}
16981716
}
@@ -1803,17 +1821,21 @@ private void outputManifestOrBundle(List<String> outputFiles,
18031821
JSModuleGraph graph = compiler.getDegenerateModuleGraph();
18041822
Iterable<JSModule> modules = graph.getAllModules();
18051823
for (JSModule module : modules) {
1806-
try (Writer out = fileNameToOutputWriter2(expandCommandLinePath(output, module))) {
1824+
Writer out = fileNameToOutputWriter2(expandCommandLinePath(output, module));
1825+
try {
18071826
if (isManifest) {
18081827
printManifestTo(module.getInputs(), out);
18091828
} else {
18101829
printBundleTo(module.getInputs(), out);
18111830
}
1831+
} finally {
1832+
out.close();
18121833
}
18131834
}
18141835
} else {
18151836
// Generate a single file manifest or bundle.
1816-
try (Writer out = fileNameToOutputWriter2(expandCommandLinePath(output, null))) {
1837+
Writer out = fileNameToOutputWriter2(expandCommandLinePath(output, null));
1838+
try {
18171839
if (config.module.isEmpty()) {
18181840
if (isManifest) {
18191841
printManifestTo(compiler.getInputsInOrder(), out);
@@ -1824,6 +1846,8 @@ private void outputManifestOrBundle(List<String> outputFiles,
18241846
printModuleGraphManifestOrBundleTo(
18251847
compiler.getDegenerateModuleGraph(), out, isManifest);
18261848
}
1849+
} finally {
1850+
out.close();
18271851
}
18281852
}
18291853
}
@@ -1835,8 +1859,11 @@ private void outputManifestOrBundle(List<String> outputFiles,
18351859
private void outputModuleGraphJson() throws IOException {
18361860
if (config.outputModuleDependencies != null &&
18371861
config.outputModuleDependencies.length() != 0) {
1838-
try (Writer out = fileNameToOutputWriter2(config.outputModuleDependencies)) {
1862+
Writer out = fileNameToOutputWriter2(config.outputModuleDependencies);
1863+
try {
18391864
printModuleGraphJsonTo(out);
1865+
} finally {
1866+
out.close();
18401867
}
18411868
}
18421869
}

src/com/google/javascript/jscomp/CommandLineRunner.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,8 +1566,9 @@ protected CompilerOptions createOptions() {
15661566
if (!flags.instrumentationFile.isEmpty()) {
15671567
String instrumentationPb;
15681568
Instrumentation.Builder builder = Instrumentation.newBuilder();
1569-
try (BufferedReader br =
1570-
new BufferedReader(Files.newReader(new File(flags.instrumentationFile), UTF_8))) {
1569+
BufferedReader br = null;
1570+
try {
1571+
br = new BufferedReader(Files.newReader(new File(flags.instrumentationFile), UTF_8));
15711572
StringBuilder sb = new StringBuilder();
15721573
String line = br.readLine();
15731574

@@ -1584,6 +1585,12 @@ protected CompilerOptions createOptions() {
15841585

15851586
} catch (IOException e) {
15861587
throw new RuntimeException("Error reading instrumentation template", e);
1588+
} finally {
1589+
try {
1590+
if (br != null) br.close();
1591+
} catch (IOException e) {
1592+
throw new RuntimeException("Error reading instrumentation template", e);
1593+
}
15871594
}
15881595
}
15891596

src/com/google/javascript/jscomp/SourceFile.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ public static List<SourceFile> fromZipFile(String zipName, Charset inputCharset)
352352
final String absoluteZipPath = new File(zipName).getAbsolutePath();
353353
List<SourceFile> sourceFiles = new ArrayList<SourceFile>();
354354

355-
try (ZipFile zipFile = new ZipFile(absoluteZipPath)) {
355+
ZipFile zipFile = new ZipFile(absoluteZipPath);
356+
try {
356357
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
357358

358359
while (zipEntries.hasMoreElements()) {
@@ -368,6 +369,8 @@ public static List<SourceFile> fromZipFile(String zipName, Charset inputCharset)
368369
.withOriginalPath(zipName + "!/" + zipEntry.getName())
369370
.buildFromUrl(zipEntryUrl));
370371
}
372+
} finally {
373+
zipFile.close();
371374
}
372375
return sourceFiles;
373376
}

src/com/google/javascript/jscomp/WhitelistWarningsGuard.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,11 @@ public void report(CheckLevel level, JSError error) {
242242
* can read back later.
243243
*/
244244
public void writeWhitelist(File out) throws IOException {
245-
try (PrintStream stream = new PrintStream(out)) {
245+
PrintStream stream = new PrintStream(out);
246+
try {
246247
appendWhitelist(stream);
248+
} finally {
249+
stream.close();
247250
}
248251
}
249252

test/com/google/javascript/jscomp/CommandLineRunnerTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,11 +2087,14 @@ private static FlagEntry<JsSourceType> createZipFile(Map<String, String> entryCo
20872087
File tempZipFile = File.createTempFile("testdata", ".js.zip",
20882088
java.nio.file.Files.createTempDirectory("jscomp").toFile());
20892089

2090-
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(tempZipFile))) {
2090+
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(tempZipFile));
2091+
try {
20912092
for (Entry<String, String> entry : entryContentsByName.entrySet()) {
20922093
zipOutputStream.putNextEntry(new ZipEntry(entry.getKey()));
20932094
zipOutputStream.write(entry.getValue().getBytes(java.nio.charset.StandardCharsets.UTF_8));
20942095
}
2096+
} finally {
2097+
zipOutputStream.close();
20952098
}
20962099

20972100
return new FlagEntry<JsSourceType>(JsSourceType.JS_ZIP, tempZipFile.getAbsolutePath());
@@ -2101,8 +2104,11 @@ private FlagEntry<JsSourceType> createJsFile(String filename, String fileContent
21012104
throws IOException {
21022105
File tempJsFile = File.createTempFile(filename, ".js",
21032106
java.nio.file.Files.createTempDirectory("jscomp").toFile());
2104-
try (FileOutputStream fileOutputStream = new FileOutputStream(tempJsFile)) {
2107+
FileOutputStream fileOutputStream = new FileOutputStream(tempJsFile);
2108+
try {
21052109
fileOutputStream.write(fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8));
2110+
} finally {
2111+
fileOutputStream.close();
21062112
}
21072113

21082114
return new FlagEntry<JsSourceType>(JsSourceType.JS, tempJsFile.getAbsolutePath());

0 commit comments

Comments
 (0)