Skip to content

Commit e8ad13a

Browse files
committed
minor refactor: remove URL constructor deprecation warnings
1 parent bd3020b commit e8ad13a

5 files changed

Lines changed: 20 additions & 10 deletions

File tree

src/main/java/groovy/lang/GroovyClassLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ public Class defineClass(final String name, final byte[] bytes) throws ClassForm
218218
public Class defineClass(final ClassNode classNode, final String file, final String newCodeBase) {
219219
CodeSource codeSource = null;
220220
try {
221-
codeSource = new CodeSource(new URL("file", "", newCodeBase), (java.security.cert.Certificate[]) null);
222-
} catch (MalformedURLException ignore) {
221+
codeSource = new CodeSource(new URI("file", "", newCodeBase, null).toURL(), (java.security.cert.Certificate[]) null);
222+
} catch (MalformedURLException | URISyntaxException ignore) {
223223
}
224224

225225
CompilationUnit unit = createCompilationUnit(config, codeSource);

src/main/java/groovy/lang/GroovyCodeSource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.Reader;
3030
import java.net.MalformedURLException;
3131
import java.net.URI;
32+
import java.net.URISyntaxException;
3233
import java.net.URL;
3334
import java.net.URLConnection;
3435
import java.security.CodeSource;
@@ -231,9 +232,9 @@ private static CodeSource createCodeSource(final String codeBase) {
231232
sm.checkPermission(new GroovyCodeSourcePermission(codeBase));
232233
}
233234
try {
234-
return new CodeSource(new URL("file", "", codeBase), (java.security.cert.Certificate[]) null);
235+
return new CodeSource(new URI("file", "", codeBase, null).toURL(), (java.security.cert.Certificate[]) null);
235236
}
236-
catch (MalformedURLException e) {
237+
catch (MalformedURLException | URISyntaxException e) {
237238
throw new RuntimeException("A CodeSource file URL cannot be constructed from the supplied codeBase: " + codeBase);
238239
}
239240
}

src/main/java/groovy/ui/GroovySocketServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public GroovySocketServer(GroovyShell groovy, GroovyCodeSource source, boolean a
137137
this.source = source;
138138
this.autoOutput = autoOutput;
139139
try {
140-
url = new URL("http", InetAddress.getLocalHost().getHostAddress(), port, "/");
140+
url = new URI("http", null, InetAddress.getLocalHost().getHostAddress(), port, "/", null, null).toURL();
141141
System.out.println("groovy is listening on port " + port);
142-
} catch (IOException e) {
142+
} catch (IOException | URISyntaxException e) {
143143
e.printStackTrace();
144144
}
145145
new Thread(this).start();

src/main/java/groovy/util/GroovyScriptEngine.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import java.io.InputStreamReader;
4646
import java.lang.ref.WeakReference;
4747
import java.net.MalformedURLException;
48+
import java.net.URI;
49+
import java.net.URISyntaxException;
4850
import java.net.URL;
4951
import java.net.URLConnection;
5052
import java.security.CodeSource;
@@ -363,11 +365,11 @@ public URLConnection getResourceConnection(String resourceName) throws ResourceE
363365
for (URL root : roots) {
364366
URL scriptURL = null;
365367
try {
366-
scriptURL = new URL(root, resourceName);
368+
scriptURL = root.toURI().resolve(resourceName).toURL();
367369
groovyScriptConn = openConnection(scriptURL);
368370

369371
break; // Now this is a bit unusual
370-
} catch (MalformedURLException e) {
372+
} catch (MalformedURLException | URISyntaxException e) {
371373
String message = "Malformed URL: with context=" + root + " and spec=" + resourceName + " because " + e.getMessage();
372374
if (se == null) {
373375
se = new ResourceException(message);
@@ -464,7 +466,7 @@ private static URL[] createRoots(String[] urls) throws MalformedURLException {
464466
URL[] roots = new URL[urls.length];
465467
for (int i = 0; i < roots.length; i++) {
466468
if (urls[i].contains("://")) {
467-
roots[i] = new URL(urls[i]);
469+
roots[i] = URI.create(urls[i]).toURL();
468470
} else {
469471
roots[i] = new File(urls[i]).toURI().toURL();
470472
}

subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.io.InputStream;
4545
import java.io.Reader;
4646
import java.io.StringReader;
47+
import java.net.URISyntaxException;
4748
import java.net.URL;
4849
import java.nio.file.Files;
4950
import java.nio.file.Path;
@@ -349,7 +350,13 @@ public void setEntityResolver(final EntityResolver entityResolver) {
349350
* @param base The URL used to resolve relative URLs
350351
*/
351352
public void setEntityBaseUrl(final URL base) {
352-
reader.setEntityResolver((publicId, systemId) -> new InputSource(new URL(base, systemId).openStream()));
353+
reader.setEntityResolver((publicId, systemId) -> {
354+
try {
355+
return new InputSource(base.toURI().resolve(systemId).toURL().openStream());
356+
} catch (URISyntaxException e) {
357+
throw new SAXException(e);
358+
}
359+
});
353360
}
354361

355362
/* (non-Javadoc)

0 commit comments

Comments
 (0)