Skip to content

Commit 2b6f856

Browse files
authored
Deprecate Util for removal (#10143)
This class is intended to be "less public" than Utility (deprecated in #10083), but is just as accessible to developers as Util is. Like Utility, many methods could be replaced by either newer JRE method or Guava. Some implementations were too general, or masked errors by returning null, unnecessarily buffered in-memory reads/writes, these have been updated as part of this patch. Hashing changes were merged in #10102. The patch slightly increases the size of the project, but will be by far a net decrease after Util.java itself is removed. The class itself has been deprecated for removal, allowing downstream Generators/Linkers/etc time to update. Fixes #10030
1 parent 4a85a12 commit 2b6f856

File tree

92 files changed

+987
-705
lines changed

Some content is hidden

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

92 files changed

+987
-705
lines changed

dev/codeserver/java/com/google/gwt/dev/codeserver/OutboxDir.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.TreeLogger.Type;
2121
import com.google.gwt.core.ext.UnableToCompleteException;
22-
import com.google.gwt.dev.util.Util;
22+
import com.google.gwt.thirdparty.guava.common.io.MoreFiles;
2323

2424
import java.io.File;
2525
import java.io.IOException;
@@ -87,7 +87,7 @@ static OutboxDir create(File dir, TreeLogger logger) throws IOException {
8787
// (This is not guaranteed to delete all directories on Windows if a directory is locked.)
8888
for (File candidate : children) {
8989
if (candidate.getName().startsWith(COMPILE_DIR_PREFIX)) {
90-
Util.recursiveDelete(candidate, false);
90+
MoreFiles.deleteRecursively(candidate.toPath());
9191
if (candidate.exists()) {
9292
logger.log(Type.WARN, "unable to delete '" + candidate + "' (skipped)");
9393
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package com.google.gwt.dev.codeserver;
1818

1919
import com.google.gwt.core.ext.TreeLogger;
20-
import com.google.gwt.dev.util.Util;
2120
import com.google.gwt.thirdparty.debugging.sourcemap.SourceMapConsumerV3;
2221
import com.google.gwt.thirdparty.debugging.sourcemap.SourceMapParseException;
2322

2423
import java.io.File;
24+
import java.io.IOException;
25+
import java.nio.charset.StandardCharsets;
26+
import java.nio.file.Files;
2527

2628
/**
2729
* A mapping from Java lines to JavaScript.
@@ -39,10 +41,13 @@ private ReverseSourceMap(SourceMapConsumerV3 consumer) {
3941
*/
4042
static ReverseSourceMap load(TreeLogger logger, File sourceMapFile) {
4143
SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
42-
String unparsed = Util.readFileAsString(sourceMapFile);
4344
try {
45+
String unparsed = Files.readString(sourceMapFile.toPath(), StandardCharsets.UTF_8);
4446
consumer.parse(unparsed);
4547
return new ReverseSourceMap(consumer);
48+
} catch (IOException ex) {
49+
logger.log(TreeLogger.Type.WARN, "can't read source map", ex);
50+
return new ReverseSourceMap(null);
4651
} catch (SourceMapParseException e) {
4752
logger.log(TreeLogger.WARN, "can't parse source map", e);
4853
return new ReverseSourceMap(null);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import com.google.gwt.dev.json.JsonArray;
2020
import com.google.gwt.dev.json.JsonException;
2121
import com.google.gwt.dev.json.JsonObject;
22-
import com.google.gwt.dev.util.Util;
2322

2423
import java.io.File;
2524
import java.io.IOException;
2625
import java.io.StringReader;
26+
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
2728
import java.util.ArrayList;
2829
import java.util.Collections;
2930
import java.util.HashSet;
@@ -37,21 +38,19 @@ class SourceMap {
3738
private final JsonObject json;
3839

3940
/**
40-
*@see #load
41+
* @see #load
4142
*/
4243
private SourceMap(JsonObject json) {
4344
this.json = json;
4445
}
4546

4647
static SourceMap load(File file) {
47-
String sourceMapJson = Util.readFileAsString(file);
4848

4949
JsonObject json;
5050
try {
51+
String sourceMapJson = Files.readString(file.toPath(), StandardCharsets.UTF_8);
5152
json = JsonObject.parse(new StringReader(sourceMapJson));
52-
} catch (JsonException e) {
53-
throw new RuntimeException("can't parse sourcemap as json", e);
54-
} catch (IOException e) {
53+
} catch (JsonException | IOException e) {
5554
throw new RuntimeException("can't parse sourcemap as json", e);
5655
}
5756

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package com.google.gwt.core.ext.linker;
1717

18+
import static java.nio.charset.StandardCharsets.UTF_8;
19+
1820
import com.google.gwt.core.ext.Linker;
1921
import com.google.gwt.core.ext.TreeLogger;
2022
import com.google.gwt.core.ext.UnableToCompleteException;
21-
import com.google.gwt.dev.util.Util;
2223
import com.google.gwt.thirdparty.guava.common.hash.Hashing;
2324

24-
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
2526
import java.io.InputStream;
2627
import java.util.Locale;
2728

@@ -69,9 +70,12 @@ protected final SyntheticArtifact emitBytes(TreeLogger logger, byte[] what,
6970
*/
7071
protected final SyntheticArtifact emitInputStream(TreeLogger logger,
7172
InputStream what, String partialPath) throws UnableToCompleteException {
72-
ByteArrayOutputStream out = new ByteArrayOutputStream();
73-
Util.copy(logger, what, out);
74-
return emitBytes(logger, out.toByteArray(), partialPath);
73+
try (what) {
74+
return emitBytes(logger, what.readAllBytes(), partialPath);
75+
} catch (IOException e) {
76+
logger.log(TreeLogger.ERROR, "Error during copy", e);
77+
throw new UnableToCompleteException();
78+
}
7579
}
7680

7781
/**
@@ -87,9 +91,12 @@ protected final SyntheticArtifact emitInputStream(TreeLogger logger,
8791
protected final SyntheticArtifact emitInputStream(TreeLogger logger,
8892
InputStream what, String partialPath, long lastModified)
8993
throws UnableToCompleteException {
90-
ByteArrayOutputStream out = new ByteArrayOutputStream();
91-
Util.copy(logger, what, out);
92-
return emitBytes(logger, out.toByteArray(), partialPath, lastModified);
94+
try (what) {
95+
return emitBytes(logger, what.readAllBytes(), partialPath, lastModified);
96+
} catch (IOException e) {
97+
logger.log(TreeLogger.ERROR, "Error during copy", e);
98+
throw new UnableToCompleteException();
99+
}
93100
}
94101

95102
/**
@@ -102,7 +109,7 @@ protected final SyntheticArtifact emitInputStream(TreeLogger logger,
102109
*/
103110
protected final SyntheticArtifact emitString(TreeLogger logger, String what,
104111
String partialPath) throws UnableToCompleteException {
105-
return emitBytes(logger, Util.getBytes(what), partialPath);
112+
return emitBytes(logger, what.getBytes(UTF_8), partialPath);
106113
}
107114

108115
/**
@@ -116,7 +123,7 @@ protected final SyntheticArtifact emitString(TreeLogger logger, String what,
116123
*/
117124
protected final SyntheticArtifact emitString(TreeLogger logger, String what,
118125
String partialPath, long lastModified) throws UnableToCompleteException {
119-
return emitBytes(logger, Util.getBytes(what), partialPath, lastModified);
126+
return emitBytes(logger, what.getBytes(UTF_8), partialPath, lastModified);
120127
}
121128

122129
/**

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import com.google.gwt.core.ext.Linker;
1919
import com.google.gwt.core.ext.TreeLogger;
2020
import com.google.gwt.core.ext.UnableToCompleteException;
21-
import com.google.gwt.dev.util.Util;
22-
import com.google.gwt.thirdparty.guava.common.io.Closeables;
2321

2422
import java.io.BufferedInputStream;
2523
import java.io.IOException;
@@ -196,15 +194,11 @@ public String toString() {
196194
*/
197195
public void writeTo(TreeLogger logger, OutputStream out)
198196
throws UnableToCompleteException {
199-
InputStream in = null;
200-
try {
201-
in = new BufferedInputStream(getContents(logger));
202-
Util.copyNoClose(in, out);
197+
try (InputStream in = new BufferedInputStream(getContents(logger))) {
198+
in.transferTo(out);
203199
} catch (IOException e) {
204200
logger.log(TreeLogger.ERROR, "Unable to copy artifact: " + getPartialPath(), e);
205201
throw new UnableToCompleteException();
206-
} finally {
207-
Closeables.closeQuietly(in);
208202
}
209203
}
210204

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.google.gwt.core.ext.linker.impl;
1717

18+
import static java.nio.charset.StandardCharsets.UTF_8;
19+
1820
import com.google.gwt.core.ext.LinkerContext;
1921
import com.google.gwt.core.ext.TreeLogger;
2022
import com.google.gwt.core.ext.TreeLogger.Type;
@@ -30,7 +32,6 @@
3032
import com.google.gwt.core.ext.linker.SoftPermutation;
3133
import com.google.gwt.core.ext.linker.StatementRanges;
3234
import com.google.gwt.core.linker.SymbolMapsLinker;
33-
import com.google.gwt.dev.util.Util;
3435

3536
import java.io.File;
3637
import java.io.IOException;
@@ -257,8 +258,9 @@ protected Collection<Artifact<?>> doEmitCompilation(TreeLogger logger,
257258
primary = null;
258259

259260
for (int i = 1; i < js.length; i++) {
260-
byte[] bytes = Util.getBytes(generateDeferredFragment(logger, context, i, js[i], artifacts,
261-
result));
261+
String s = generateDeferredFragment(logger, context, i, js[i], artifacts,
262+
result);
263+
byte[] bytes = s.getBytes(UTF_8);
262264
toReturn.add(emitBytes(logger, bytes, FRAGMENT_SUBDIR + File.separator
263265
+ result.getStrongName() + File.separator + i + FRAGMENT_EXTENSION));
264266
}
@@ -384,7 +386,7 @@ protected byte[] generatePrimaryFragment(TreeLogger logger,
384386
charsPerChunk(context, logger), getScriptChunkSeparator(logger, context), context);
385387
String primaryFragmentString =
386388
generatePrimaryFragmentString(logger, context, result, temp, js.length, artifacts);
387-
return Util.getBytes(primaryFragmentString);
389+
return primaryFragmentString.getBytes(UTF_8);
388390
}
389391

390392
protected String generatePrimaryFragmentString(TreeLogger logger,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import com.google.gwt.core.ext.linker.SymbolData;
2323
import com.google.gwt.dev.jjs.PermutationResult;
2424
import com.google.gwt.dev.util.DiskCache;
25-
import com.google.gwt.dev.util.Util;
2625
import com.google.gwt.dev.util.collect.Lists;
2726

2827
import java.io.Serializable;
28+
import java.nio.charset.StandardCharsets;
2929
import java.util.Collections;
3030
import java.util.Comparator;
3131
import java.util.Iterator;
@@ -124,7 +124,7 @@ public String[] getJavaScript() {
124124

125125
String[] jsStrings = new String[applicationFragmentCount];
126126
for (int fragmentIndex = 0; fragmentIndex < applicationFragmentCount; fragmentIndex++) {
127-
jsStrings[fragmentIndex] = Util.toString(applicationJs[fragmentIndex]);
127+
jsStrings[fragmentIndex] = new String(applicationJs[fragmentIndex], StandardCharsets.UTF_8);
128128
}
129129
return jsStrings;
130130
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import com.google.gwt.core.ext.UnableToCompleteException;
2020
import com.google.gwt.core.ext.linker.PublicResource;
2121
import com.google.gwt.dev.resource.Resource;
22-
import com.google.gwt.dev.util.Util;
2322

2423
import java.io.ByteArrayInputStream;
25-
import java.io.ByteArrayOutputStream;
2624
import java.io.IOException;
2725
import java.io.InputStream;
2826
import java.io.OutputStream;
@@ -99,10 +97,8 @@ private Object writeReplace() {
9997
return this;
10098
}
10199
// Resource is not serializable, must replace myself.
102-
try {
103-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
104-
Util.copy(resource.openContents(), baos);
105-
return new SerializedPublicResource(getPartialPath(), baos.toByteArray(),
100+
try (InputStream is = resource.openContents()) {
101+
return new SerializedPublicResource(getPartialPath(), is.readAllBytes(),
106102
getLastModified());
107103
} catch (IOException e) {
108104
throw new RuntimeException(e);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.gwt.dev.jjs.ast.JDeclaredType;
1919
import com.google.gwt.dev.jjs.ast.JField;
2020
import com.google.gwt.dev.jjs.ast.JMethod;
21-
import com.google.gwt.dev.util.Util;
2221
import com.google.gwt.thirdparty.guava.common.collect.Maps;
2322

2423
import java.util.Collection;
@@ -35,9 +34,11 @@ public class ClassDescriptor extends EntityDescriptor {
3534
* Creates a class descriptor from a JDeclaredType.
3635
*/
3736
public static ClassDescriptor from(JDeclaredType classType) {
38-
return new ClassDescriptor(Util.getClassName(classType.getName()),
39-
Util.getPackageName(classType.getName()),
40-
classType);
37+
String name = classType.getName();
38+
int lastDot = name.lastIndexOf('.');
39+
String className = name.substring(lastDot + 1);
40+
final String packageName = lastDot > 0 ? name.substring(0, lastDot) : "";
41+
return new ClassDescriptor(className, packageName, classType);
4142
}
4243

4344
private final String packageName;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import com.google.gwt.dev.js.ast.JsLiteral;
2525
import com.google.gwt.dev.js.ast.JsName;
2626
import com.google.gwt.dev.js.ast.JsStringLiteral;
27-
import com.google.gwt.dev.util.Util;
2827

2928
import java.io.IOException;
3029
import java.io.OutputStream;
3130
import java.io.OutputStreamWriter;
3231
import java.io.Writer;
32+
import java.nio.charset.StandardCharsets;
3333
import java.util.Comparator;
3434
import java.util.Map;
3535
import java.util.Map.Entry;
@@ -177,7 +177,7 @@ public static void escapeXml(String code, int start, int end, boolean quoteApost
177177
public static void recordMap(TreeLogger logger, OutputStream out, SizeBreakdown[] sizeBreakdowns,
178178
JavaToJavaScriptMap jjsmap, Map<JsName, JsLiteral> internedLiteralByVariableName) throws IOException {
179179
out = new GZIPOutputStream(out);
180-
Writer writer = new OutputStreamWriter(out, Util.DEFAULT_ENCODING);
180+
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
181181

182182
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
183183
writer.append("<sizemaps>\n");

0 commit comments

Comments
 (0)