From e360e55dc9b668f99c4b67005831cc32491f425c Mon Sep 17 00:00:00 2001 From: Chris Midgley Date: Sat, 30 Jul 2022 01:11:17 +0100 Subject: [PATCH] Make getCharacterEncoding in UrlModuleSourceProvider protected (#1233) * Make getCharacterEncoding in UrlModuleSourceProvider protected Allows subclasses to override the encoding used in case a local javascript file is interpreted as being latin-1, when it should be whatever the file is encoded with. Before openJDK 17.0.3 this was automatically interpreted as utf-8. --- .../module/provider/UrlModuleSourceProvider.java | 4 ++-- .../module/provider/UrlModuleSourceProviderTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/org/mozilla/javascript/commonjs/module/provider/UrlModuleSourceProvider.java b/src/org/mozilla/javascript/commonjs/module/provider/UrlModuleSourceProvider.java index 7decc72f72..7ada3f8bc9 100644 --- a/src/org/mozilla/javascript/commonjs/module/provider/UrlModuleSourceProvider.java +++ b/src/org/mozilla/javascript/commonjs/module/provider/UrlModuleSourceProvider.java @@ -156,12 +156,12 @@ protected ModuleSource loadFromActualUri(URI uri, URI base, Object validator) } } - private static Reader getReader(URLConnection urlConnection) throws IOException { + private Reader getReader(URLConnection urlConnection) throws IOException { return new InputStreamReader( urlConnection.getInputStream(), getCharacterEncoding(urlConnection)); } - private static String getCharacterEncoding(URLConnection urlConnection) { + protected String getCharacterEncoding(URLConnection urlConnection) { final ParsedContentType pct = new ParsedContentType(urlConnection.getContentType()); final String encoding = pct.getEncoding(); if (encoding != null) { diff --git a/testsrc/org/mozilla/javascript/tests/commonjs/module/provider/UrlModuleSourceProviderTest.java b/testsrc/org/mozilla/javascript/tests/commonjs/module/provider/UrlModuleSourceProviderTest.java index 4924b1afd8..70b8e6c6b1 100644 --- a/testsrc/org/mozilla/javascript/tests/commonjs/module/provider/UrlModuleSourceProviderTest.java +++ b/testsrc/org/mozilla/javascript/tests/commonjs/module/provider/UrlModuleSourceProviderTest.java @@ -4,8 +4,11 @@ package org.mozilla.javascript.tests.commonjs.module.provider; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.FileTime; @@ -67,6 +70,15 @@ public void testModuleModified() throws Exception { Assert.assertNotEquals("Modified", ModuleSourceProvider.NOT_MODIFIED, result); } + @Test + public void getCharacterEncodingCanBeModifiedInSubclass() throws NoSuchMethodException { + Method method = + UrlModuleSourceProvider.class.getDeclaredMethod( + "getCharacterEncoding", new Class[] {URLConnection.class}); + int mods = method.getModifiers(); + Assert.assertTrue(Modifier.isPublic(mods) || Modifier.isProtected(mods)); + } + private static URI getModuleURI(final Path filePath) throws URISyntaxException { final String uriString = filePath.toUri().toASCIIString(); return new URI(uriString.substring(0, uriString.lastIndexOf('.')));