Skip to content

Commit

Permalink
Support all kinds of resource URLs when loading template resources.
Browse files Browse the repository at this point in the history
This should have no user-visible effect. It is intended for the unusual case where someone is generating AutoValue (etc) code from code that has been compiled with GraalVM.

Fixes #1783.

RELNOTES=n/a
PiperOrigin-RevId: 638426982
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed May 29, 2024
1 parent 260b61e commit 80b0ada
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static Reader readerFromUrl(String resourceName) throws IOException {
} else if (Ascii.equalsIgnoreCase(resourceUrl.getProtocol(), "jar")) {
return readerFromJar(resourceUrl);
} else {
throw new AssertionError("Template search logic fails for: " + resourceUrl);
return readerFromOther(resourceUrl);
}
} catch (URISyntaxException e) {
throw new IOException(e);
Expand Down Expand Up @@ -174,12 +174,18 @@ private static Reader readerFromJar(URL resourceUrl) throws URISyntaxException,

// In most execution environments, we'll be dealing with a jar, but we handle individual files
// just for cases like running our tests with Maven.
private static Reader readerFromFile(URL resourceUrl)
throws IOException, URISyntaxException {
private static Reader readerFromFile(URL resourceUrl) throws IOException, URISyntaxException {
File resourceFile = new File(resourceUrl.toURI());
return new InputStreamReader(new FileInputStream(resourceFile), UTF_8);
}

// As a fallback, we handle other kinds of URL naively. For example, if we're executing in GraalVM
// code, we might have a `resource:` URL. This code is not currently covered by unit tests.
// See https://github.com/google/auto/issues/1783.
private static Reader readerFromOther(URL resourceUrl) throws IOException {
return new InputStreamReader(resourceUrl.openStream(), UTF_8);
}

private static Object fieldValue(Field field, Object container) {
try {
return field.get(container);
Expand Down

0 comments on commit 80b0ada

Please sign in to comment.