From 944956c9e7a942ee1f1e8d57bfe6bfed90443f34 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Sat, 20 Feb 2021 14:17:12 -0500 Subject: [PATCH] JDK-8262277: java.net.URLClassLoader.getResource throws undocumented IllegalArgumentException --- .../jdk/internal/loader/URLClassPath.java | 6 +- .../FindResourceDoesNotThrowException.java | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 test/jdk/java/net/URLClassLoader/FindResourceDoesNotThrowException.java diff --git a/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java b/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java index 4640b5b3a7dcf..616af8b49e364 100644 --- a/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java +++ b/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -600,7 +600,7 @@ URL findResource(final String name, boolean check) { try { url = new URL(base, ParseUtil.encodePath(name, false)); } catch (MalformedURLException e) { - throw new IllegalArgumentException("name"); + return null; } try { @@ -636,7 +636,7 @@ Resource getResource(final String name, boolean check) { try { url = new URL(base, ParseUtil.encodePath(name, false)); } catch (MalformedURLException e) { - throw new IllegalArgumentException("name"); + return null; } final URLConnection uc; try { diff --git a/test/jdk/java/net/URLClassLoader/FindResourceDoesNotThrowException.java b/test/jdk/java/net/URLClassLoader/FindResourceDoesNotThrowException.java new file mode 100644 index 0000000000000..696ba2735ebad --- /dev/null +++ b/test/jdk/java/net/URLClassLoader/FindResourceDoesNotThrowException.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8262277 + * @summary Test if URLClassLoader throws IllegalArgumentException when getting + * or finding resources. + */ + +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; + +public class FindResourceDoesNotThrowException { + public static void main(String[] args) throws Exception { + // URL ends in slash, does not start with file: or jar: + URL url = new URL("https://127.0.0.1/"); + // resource name is not a valid url component + String resource = "c:/windows"; + try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url})) { + if (urlClassLoader.findResource(resource) != null) { + throw new RuntimeException("findResource should return null"); + } + if (urlClassLoader.getResource(resource) != null) { + throw new RuntimeException("getResource should return null"); + } + if (urlClassLoader.findResources(resource).hasMoreElements()) { + throw new RuntimeException("findResources should return an empty enumeration"); + } + if (urlClassLoader.getResources(resource).hasMoreElements()) { + throw new RuntimeException("getResources should return an empty enumeration"); + } + } + } + private FindResourceDoesNotThrowException() {} +}