This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new e8ad13a467 minor refactor: remove URL constructor deprecation warnings
e8ad13a467 is described below
commit e8ad13a4673a6d41105344b3ee639e967a873f6a
Author: Paul King <[email protected]>
AuthorDate: Sat Apr 4 06:02:17 2026 +1000
minor refactor: remove URL constructor deprecation warnings
---
src/main/java/groovy/lang/GroovyClassLoader.java | 4 ++--
src/main/java/groovy/lang/GroovyCodeSource.java | 5 +++--
src/main/java/groovy/ui/GroovySocketServer.java | 4 ++--
src/main/java/groovy/util/GroovyScriptEngine.java | 8 +++++---
subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java | 9 ++++++++-
5 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/src/main/java/groovy/lang/GroovyClassLoader.java
b/src/main/java/groovy/lang/GroovyClassLoader.java
index 97aa0f0add..b786ee181b 100644
--- a/src/main/java/groovy/lang/GroovyClassLoader.java
+++ b/src/main/java/groovy/lang/GroovyClassLoader.java
@@ -218,8 +218,8 @@ public class GroovyClassLoader extends URLClassLoader {
public Class defineClass(final ClassNode classNode, final String file,
final String newCodeBase) {
CodeSource codeSource = null;
try {
- codeSource = new CodeSource(new URL("file", "", newCodeBase),
(java.security.cert.Certificate[]) null);
- } catch (MalformedURLException ignore) {
+ codeSource = new CodeSource(new URI("file", "", newCodeBase,
null).toURL(), (java.security.cert.Certificate[]) null);
+ } catch (MalformedURLException | URISyntaxException ignore) {
}
CompilationUnit unit = createCompilationUnit(config, codeSource);
diff --git a/src/main/java/groovy/lang/GroovyCodeSource.java
b/src/main/java/groovy/lang/GroovyCodeSource.java
index c46d2e9cb9..8e7a20ad55 100644
--- a/src/main/java/groovy/lang/GroovyCodeSource.java
+++ b/src/main/java/groovy/lang/GroovyCodeSource.java
@@ -29,6 +29,7 @@ import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
@@ -231,9 +232,9 @@ public class GroovyCodeSource {
sm.checkPermission(new GroovyCodeSourcePermission(codeBase));
}
try {
- return new CodeSource(new URL("file", "", codeBase),
(java.security.cert.Certificate[]) null);
+ return new CodeSource(new URI("file", "", codeBase, null).toURL(),
(java.security.cert.Certificate[]) null);
}
- catch (MalformedURLException e) {
+ catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException("A CodeSource file URL cannot be
constructed from the supplied codeBase: " + codeBase);
}
}
diff --git a/src/main/java/groovy/ui/GroovySocketServer.java
b/src/main/java/groovy/ui/GroovySocketServer.java
index 40af52b261..3390e364fd 100644
--- a/src/main/java/groovy/ui/GroovySocketServer.java
+++ b/src/main/java/groovy/ui/GroovySocketServer.java
@@ -137,9 +137,9 @@ public class GroovySocketServer implements Runnable {
this.source = source;
this.autoOutput = autoOutput;
try {
- url = new URL("http", InetAddress.getLocalHost().getHostAddress(),
port, "/");
+ url = new URI("http", null,
InetAddress.getLocalHost().getHostAddress(), port, "/", null, null).toURL();
System.out.println("groovy is listening on port " + port);
- } catch (IOException e) {
+ } catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
new Thread(this).start();
diff --git a/src/main/java/groovy/util/GroovyScriptEngine.java
b/src/main/java/groovy/util/GroovyScriptEngine.java
index a7e6d06c56..fb78208bac 100644
--- a/src/main/java/groovy/util/GroovyScriptEngine.java
+++ b/src/main/java/groovy/util/GroovyScriptEngine.java
@@ -45,6 +45,8 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
@@ -363,11 +365,11 @@ public class GroovyScriptEngine implements
ResourceConnector {
for (URL root : roots) {
URL scriptURL = null;
try {
- scriptURL = new URL(root, resourceName);
+ scriptURL = root.toURI().resolve(resourceName).toURL();
groovyScriptConn = openConnection(scriptURL);
break; // Now this is a bit unusual
- } catch (MalformedURLException e) {
+ } catch (MalformedURLException | URISyntaxException e) {
String message = "Malformed URL: with context=" + root + " and
spec=" + resourceName + " because " + e.getMessage();
if (se == null) {
se = new ResourceException(message);
@@ -464,7 +466,7 @@ public class GroovyScriptEngine implements
ResourceConnector {
URL[] roots = new URL[urls.length];
for (int i = 0; i < roots.length; i++) {
if (urls[i].contains("://")) {
- roots[i] = new URL(urls[i]);
+ roots[i] = URI.create(urls[i]).toURL();
} else {
roots[i] = new File(urls[i]).toURI().toURL();
}
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
index 28e1b1b252..3925a53352 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlSlurper.java
@@ -44,6 +44,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
+import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -349,7 +350,13 @@ public class XmlSlurper extends DefaultHandler {
* @param base The URL used to resolve relative URLs
*/
public void setEntityBaseUrl(final URL base) {
- reader.setEntityResolver((publicId, systemId) -> new InputSource(new
URL(base, systemId).openStream()));
+ reader.setEntityResolver((publicId, systemId) -> {
+ try {
+ return new
InputSource(base.toURI().resolve(systemId).toURL().openStream());
+ } catch (URISyntaxException e) {
+ throw new SAXException(e);
+ }
+ });
}
/* (non-Javadoc)