Skip to content

Commit d487651

Browse files
authored
Deprecate Utility for removal (#10083)
This class is an odd mix of useful methods, and was used all over the code base. Many of the methods have a better way to implement them in modern Java (try-with-resources, `InputStream.transferTo()`, `InputStream.readAllBytes()`, `java.nio` utilities), and some had odd quirks or bugs (closing while writing a buffer file _must_ be allowed to throw an error! don't double-buffer reads or writes!, especially when you're just pulling the results into memory anyway!). Some methods have been moved to their own types, to be closer to their use and not effectively require every class have access to this same package - SourceLevel, LinkerUtils, and CommandLineCreatorUtils Also modified CompilerTest and RecompilerTest to also avoid deprecated Guava `Files.createTempDir()`, `Files.write()`, etc in favor of `java.nio.file.Files`. The class itself has been deprecated for removal, allowing downstream Generators/Linkers/etc time to update. Fixes #10031
1 parent f1c4578 commit d487651

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+698
-515
lines changed

build_tools/doctool/src/com/google/doctool/ResourceIncluder.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.google.doctool;
1717

18-
import java.io.ByteArrayOutputStream;
1918
import java.io.FileNotFoundException;
2019
import java.io.IOException;
2120
import java.io.InputStream;
@@ -26,18 +25,6 @@
2625
*/
2726
public class ResourceIncluder {
2827

29-
/**
30-
* Copied from {@link com.google.gwt.util.tools.Utility#close(AutoCloseable)}.
31-
*/
32-
public static void close(AutoCloseable is) {
33-
try {
34-
if (is != null) {
35-
is.close();
36-
}
37-
} catch (Exception e) {
38-
}
39-
}
40-
4128
public static String getResourceFromClasspathScrubbedForHTML(String partialPath)
4229
throws IOException {
4330
String contents;
@@ -46,35 +33,14 @@ public static String getResourceFromClasspathScrubbedForHTML(String partialPath)
4633
return contents;
4734
}
4835

49-
/**
50-
* Copied from
51-
* {@link com.google.gwt.util.tools.Utility#getFileFromClassPath(String)}.
52-
*/
5336
private static String getFileFromClassPath(String partialPath)
5437
throws IOException {
55-
InputStream in = ResourceIncluder.class.getClassLoader().getResourceAsStream(
56-
partialPath);
57-
try {
38+
try (InputStream in = ResourceIncluder.class.getClassLoader().getResourceAsStream(
39+
partialPath)) {
5840
if (in == null) {
5941
throw new FileNotFoundException(partialPath);
6042
}
61-
ByteArrayOutputStream os = new ByteArrayOutputStream();
62-
byte[] buffer = new byte[1024];
63-
int bytesRead;
64-
while (true) {
65-
bytesRead = in.read(buffer);
66-
if (bytesRead >= 0) {
67-
// Copy the bytes out.
68-
os.write(buffer, 0, bytesRead);
69-
} else {
70-
// End of input stream.
71-
break;
72-
}
73-
}
74-
75-
return os.toString(StandardCharsets.UTF_8);
76-
} finally {
77-
close(in);
43+
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
7844
}
7945
}
8046

dev/codeserver/java/com/google/gwt/dev/codeserver/CodeServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import com.google.gwt.dev.util.DiskCachingUtil;
2626
import com.google.gwt.dev.util.log.PrintWriterTreeLogger;
2727
import com.google.gwt.thirdparty.guava.common.collect.ImmutableMap;
28-
import com.google.gwt.util.tools.Utility;
2928

3029
import java.io.File;
3130
import java.io.IOException;
31+
import java.nio.file.Files;
3232

3333
/**
3434
* <p>This class contains the {@link #main main method} that starts the code server for
@@ -201,7 +201,7 @@ private static OutboxTable makeOutboxTable(Options options, TreeLogger logger,
201201
private static File ensureWorkDir(Options options) throws IOException {
202202
File workDir = options.getWorkDir();
203203
if (workDir == null) {
204-
workDir = Utility.makeTemporaryDirectory(null, "gwt-codeserver-");
204+
workDir = Files.createTempDirectory("gwt-codeserver-").toFile();
205205
} else {
206206
if (!workDir.isDirectory()) {
207207
throw new IOException("workspace directory doesn't exist: " + workDir);

dev/codeserver/javatests/com/google/gwt/dev/codeserver/RecompilerTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
import com.google.gwt.thirdparty.guava.common.base.Charsets;
2929
import com.google.gwt.thirdparty.guava.common.collect.ImmutableMap;
3030
import com.google.gwt.thirdparty.guava.common.collect.Lists;
31-
import com.google.gwt.thirdparty.guava.common.io.Files;
3231

3332
import junit.framework.TestCase;
3433

3534
import java.io.File;
3635
import java.io.FilenameFilter;
3736
import java.io.IOException;
37+
import java.nio.file.Files;
3838
import java.util.Arrays;
3939
import java.util.Comparator;
4040
import java.util.HashMap;
@@ -68,7 +68,7 @@ private static void writeResourcesTo(List<MockResource> resources, File dir) thr
6868
File resourceFile =
6969
new File(dir.getAbsolutePath() + File.separator + applicationResource.getPath());
7070
resourceFile.getParentFile().mkdirs();
71-
Files.write(applicationResource.getContent(), resourceFile, Charsets.UTF_8);
71+
Files.write(resourceFile.toPath(), List.of(applicationResource.getContent()), Charsets.UTF_8);
7272
}
7373
}
7474

@@ -163,7 +163,7 @@ public void testIncrementalRecompile_compileErrorDoesntCorruptMinimalRebuildCach
163163
PrintWriterTreeLogger logger = new PrintWriterTreeLogger();
164164
logger.setMaxDetail(TreeLogger.ERROR);
165165

166-
File sourcePath = Files.createTempDir();
166+
File sourcePath = createTempDir();
167167
// Setup options to perform a per-file compile and compile the given module.
168168
Options options = new Options();
169169
options.parseArgs(new String[] {
@@ -175,12 +175,12 @@ public void testIncrementalRecompile_compileErrorDoesntCorruptMinimalRebuildCach
175175
fooResource);
176176
writeResourcesTo(originalResources, sourcePath);
177177

178-
File baseCacheDir = Files.createTempDir();
178+
File baseCacheDir = createTempDir();
179179
UnitCache unitCache = UnitCacheSingleton.get(
180180
logger, null, baseCacheDir, new CompilerOptionsImpl(options));
181181
MinimalRebuildCacheManager minimalRebuildCacheManager =
182182
new MinimalRebuildCacheManager(logger, baseCacheDir, ImmutableMap.<String, String>of());
183-
Recompiler recompiler = new Recompiler(OutboxDir.create(Files.createTempDir(), logger), null,
183+
Recompiler recompiler = new Recompiler(OutboxDir.create(createTempDir(), logger), null,
184184
moduleName, options, unitCache, minimalRebuildCacheManager);
185185
Outbox outbox = new Outbox("Transactional Cache", recompiler, options, logger);
186186
OutboxTable outboxTable = new OutboxTable();
@@ -211,7 +211,7 @@ public void testIncrementalRecompile_modulePropertyEditsWork() throws UnableToCo
211211
PrintWriterTreeLogger logger = new PrintWriterTreeLogger();
212212
logger.setMaxDetail(TreeLogger.ERROR);
213213

214-
File sourcePath = Files.createTempDir();
214+
File sourcePath = createTempDir();
215215
// Setup options to perform a per-file compile and compile the given module.
216216
Options options = new Options();
217217
options.parseArgs(new String[] {
@@ -223,12 +223,12 @@ public void testIncrementalRecompile_modulePropertyEditsWork() throws UnableToCo
223223
fooResource);
224224
writeResourcesTo(originalResources, sourcePath);
225225

226-
File baseCacheDir = Files.createTempDir();
226+
File baseCacheDir = createTempDir();
227227
UnitCache unitCache = UnitCacheSingleton.get(
228228
logger, null, baseCacheDir, new CompilerOptionsImpl(options));
229229
MinimalRebuildCacheManager minimalRebuildCacheManager =
230230
new MinimalRebuildCacheManager(logger, baseCacheDir, ImmutableMap.<String, String>of());
231-
Recompiler recompiler = new Recompiler(OutboxDir.create(Files.createTempDir(), logger), null,
231+
Recompiler recompiler = new Recompiler(OutboxDir.create(createTempDir(), logger), null,
232232
moduleName, options, unitCache, minimalRebuildCacheManager);
233233
Outbox outbox = new Outbox("Transactional Cache", recompiler, options, logger);
234234
OutboxTable outboxTable = new OutboxTable();
@@ -276,4 +276,8 @@ private Result compileWithChanges(TreeLogger logger, JobRunner runner, Outbox ou
276276
runner.submit(job);
277277
return job.waitForResult();
278278
}
279+
280+
private static File createTempDir() throws IOException {
281+
return Files.createTempDirectory("RecompilerTest").toFile();
282+
}
279283
}

dev/core/src/com/google/gwt/core/ext/linker/EmittedArtifact.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.google.gwt.core.ext.TreeLogger;
2020
import com.google.gwt.core.ext.UnableToCompleteException;
2121
import com.google.gwt.dev.util.Util;
22-
import com.google.gwt.util.tools.Utility;
22+
import com.google.gwt.thirdparty.guava.common.io.Closeables;
2323

2424
import java.io.BufferedInputStream;
2525
import java.io.IOException;
@@ -204,7 +204,7 @@ public void writeTo(TreeLogger logger, OutputStream out)
204204
logger.log(TreeLogger.ERROR, "Unable to copy artifact: " + getPartialPath(), e);
205205
throw new UnableToCompleteException();
206206
} finally {
207-
Utility.close(in);
207+
Closeables.closeQuietly(in);
208208
}
209209
}
210210

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.core.ext.linker;
17+
18+
import java.io.FileNotFoundException;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.nio.charset.StandardCharsets;
22+
23+
/**
24+
* Utility methods for Linkers.
25+
*/
26+
public class LinkerUtils {
27+
/**
28+
* Gets the contents of a file from the class path as a String. Note: this
29+
* method is only guaranteed to work for resources in the same class loader
30+
* that contains this {@link LinkerUtils} class.
31+
*
32+
* @param path the partial path to the resource on the class path
33+
* @return the contents of the file
34+
* @throws IOException if the file could not be found or an error occurred
35+
* while reading it
36+
*/
37+
public static String readClasspathFileAsString(String path) throws IOException {
38+
try (InputStream in = LinkerUtils.class.getClassLoader().getResourceAsStream(path)) {
39+
if (in == null) {
40+
throw new FileNotFoundException(path);
41+
}
42+
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
43+
}
44+
}
45+
46+
private LinkerUtils() {
47+
}
48+
}

dev/core/src/com/google/gwt/core/ext/linker/impl/HostedModeLinker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.google.gwt.core.ext.TreeLogger;
2020
import com.google.gwt.core.ext.UnableToCompleteException;
2121
import com.google.gwt.core.ext.linker.ArtifactSet;
22+
import com.google.gwt.core.ext.linker.LinkerUtils;
2223
import com.google.gwt.core.ext.linker.Shardable;
23-
import com.google.gwt.util.tools.Utility;
2424

2525
import java.io.IOException;
2626

@@ -32,7 +32,7 @@
3232
public final class HostedModeLinker extends SelectionScriptLinker {
3333

3434
public static String getHostedHtml() throws IOException {
35-
return Utility.getFileFromClassPath("com/google/gwt/core/ext/linker/impl/hosted.html");
35+
return LinkerUtils.readClasspathFileAsString("com/google/gwt/core/ext/linker/impl/hosted.html");
3636
}
3737

3838
/**

dev/core/src/com/google/gwt/core/ext/linker/impl/SelectionScriptLinker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import com.google.gwt.core.ext.linker.CompilationResult;
2626
import com.google.gwt.core.ext.linker.ConfigurationProperty;
2727
import com.google.gwt.core.ext.linker.EmittedArtifact;
28+
import com.google.gwt.core.ext.linker.LinkerUtils;
2829
import com.google.gwt.core.ext.linker.SelectionProperty;
2930
import com.google.gwt.core.ext.linker.SoftPermutation;
3031
import com.google.gwt.core.ext.linker.StatementRanges;
3132
import com.google.gwt.core.linker.SymbolMapsLinker;
3233
import com.google.gwt.dev.util.Util;
33-
import com.google.gwt.util.tools.Utility;
3434

3535
import java.io.File;
3636
import java.io.IOException;
@@ -323,8 +323,8 @@ protected String fillSelectionScriptTemplate(StringBuffer selectionScript,
323323
String computeScriptBase;
324324
String processMetas;
325325
try {
326-
computeScriptBase = Utility.getFileFromClassPath(COMPUTE_SCRIPT_BASE_JS);
327-
processMetas = Utility.getFileFromClassPath(PROCESS_METAS_JS);
326+
computeScriptBase = LinkerUtils.readClasspathFileAsString(COMPUTE_SCRIPT_BASE_JS);
327+
processMetas = LinkerUtils.readClasspathFileAsString(PROCESS_METAS_JS);
328328
} catch (IOException e) {
329329
logger.log(TreeLogger.ERROR, "Unable to read selection script template",
330330
e);
@@ -566,7 +566,7 @@ protected StringBuffer readFileToStringBuffer(String filename,
566566
TreeLogger logger) throws UnableToCompleteException {
567567
StringBuffer buffer;
568568
try {
569-
buffer = new StringBuffer(Utility.getFileFromClassPath(filename));
569+
buffer = new StringBuffer(LinkerUtils.readClasspathFileAsString(filename));
570570
} catch (IOException e) {
571571
logger.log(TreeLogger.ERROR, "Unable to read file: " + filename, e);
572572
throw new UnableToCompleteException();

dev/core/src/com/google/gwt/core/ext/linker/impl/StandardLinkerContext.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import com.google.gwt.dev.resource.ResourceOracle;
5353
import com.google.gwt.dev.util.DefaultTextOutput;
5454
import com.google.gwt.dev.util.OutputFileSet;
55-
import com.google.gwt.util.tools.Utility;
5655

5756
import java.io.BufferedOutputStream;
5857
import java.io.File;
@@ -521,10 +520,8 @@ public void produceOutput(TreeLogger logger, ArtifactSet artifacts,
521520
partialPath = partialPath.substring(1);
522521
}
523522
}
524-
OutputStream artifactStream = null;
525-
try {
526-
artifactStream = new BufferedOutputStream(out.openForWrite(partialPath,
527-
artifact.getLastModified()));
523+
try (OutputStream artifactStream = new BufferedOutputStream(
524+
out.openForWrite(partialPath, artifact.getLastModified()))) {
528525
artifact.writeTo(artifactLogger, artifactStream);
529526
} catch (IOException e) {
530527
artifactLogger.log(TreeLogger.ERROR,
@@ -533,8 +530,6 @@ public void produceOutput(TreeLogger logger, ArtifactSet artifacts,
533530
if (visibility != Visibility.Private) {
534531
throw new UnableToCompleteException();
535532
}
536-
} finally {
537-
Utility.close(artifactStream);
538533
}
539534
}
540535
}

dev/core/src/com/google/gwt/core/ext/soyc/coderef/EntityRecorder.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
import com.google.gwt.thirdparty.json.JSONArray;
3939
import com.google.gwt.thirdparty.json.JSONException;
4040
import com.google.gwt.thirdparty.json.JSONObject;
41-
import com.google.gwt.util.tools.Utility;
4241

43-
import java.io.ByteArrayOutputStream;
4442
import java.io.IOException;
45-
import java.io.PrintWriter;
43+
import java.nio.charset.StandardCharsets;
4644
import java.util.List;
4745
import java.util.Map;
4846
import java.util.Map.Entry;
@@ -170,14 +168,9 @@ private void recordCodeReferences(DependencyGraphRecorder codeGraph,
170168
}
171169

172170
private String addArtifactFromJson(Object value, String named) throws IOException {
173-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
174-
PrintWriter writer = new PrintWriter(baos);
175-
writer.write(value.toString());
176-
Utility.close(writer);
177-
178171
// TODO(ocallau) Must be updated with the correct/final linker
179172
SyntheticArtifact artifact = new SyntheticArtifact(
180-
SoycReportLinker.class, named, baos.toByteArray());
173+
SoycReportLinker.class, named, value.toString().getBytes(StandardCharsets.UTF_8));
181174
artifact.setVisibility(Visibility.LegacyDeploy);
182175

183176
toReturn.add(artifact);

dev/core/src/com/google/gwt/core/ext/soyc/impl/DependencyRecorder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.gwt.dev.jjs.ast.JRunAsync;
2323
import com.google.gwt.dev.jjs.impl.ControlFlowAnalyzer;
2424
import com.google.gwt.dev.jjs.impl.codesplitter.MultipleDependencyGraphRecorder;
25-
import com.google.gwt.util.tools.Utility;
2625

2726
import java.io.IOException;
2827
import java.io.OutputStream;
@@ -108,7 +107,10 @@ public void startDependencyGraph(String identifier, String extnds) {
108107

109108
/**
110109
* Used to record dependencies of a program.
110+
*
111+
* @deprecated this method is unused internally, and may be removed in a future release.
111112
*/
113+
@Deprecated
112114
protected void recordDependenciesImpl(TreeLogger logger, JProgram jprogram) {
113115

114116
logger = logger.branch(TreeLogger.DEBUG, "Creating dependencies file for the compile report");
@@ -129,7 +131,7 @@ protected void recordDependenciesImpl(TreeLogger logger, JProgram jprogram) {
129131
printPost();
130132

131133
flushOutput();
132-
Utility.close(writer);
134+
writer.close();
133135

134136
} catch (Throwable e) {
135137
logger.log(TreeLogger.WARN, "Could not write dependency file; proceeding anyway.", e);

0 commit comments

Comments
 (0)