Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 0c718ab

Browse files
candrewsBrent Christian
authored and
Brent Christian
committedMar 15, 2021
8262277: URLClassLoader.getResource throws undocumented IllegalArgumentException
Reviewed-by: alanb, bchristi, psadhukhan
1 parent 4f1cda4 commit 0c718ab

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed
 

‎src/java.base/share/classes/jdk/internal/loader/URLClassPath.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -600,7 +600,7 @@ URL findResource(final String name, boolean check) {
600600
try {
601601
url = new URL(base, ParseUtil.encodePath(name, false));
602602
} catch (MalformedURLException e) {
603-
throw new IllegalArgumentException("name");
603+
return null;
604604
}
605605

606606
try {
@@ -636,7 +636,7 @@ Resource getResource(final String name, boolean check) {
636636
try {
637637
url = new URL(base, ParseUtil.encodePath(name, false));
638638
} catch (MalformedURLException e) {
639-
throw new IllegalArgumentException("name");
639+
return null;
640640
}
641641
final URLConnection uc;
642642
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8262277
27+
* @summary Test if URLClassLoader throws IllegalArgumentException when getting
28+
* or finding resources.
29+
*/
30+
31+
import java.net.URL;
32+
import java.net.URLClassLoader;
33+
import java.util.Enumeration;
34+
35+
public class FindResourceDoesNotThrowException {
36+
public static void main(String[] args) throws Exception {
37+
// URL ends in slash, does not start with file: or jar:
38+
URL url = new URL("https://127.0.0.1/");
39+
// resource name is not a valid url component
40+
String resource = "c:/windows";
41+
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url})) {
42+
if (urlClassLoader.findResource(resource) != null) {
43+
throw new RuntimeException("findResource should return null");
44+
}
45+
if (urlClassLoader.getResource(resource) != null) {
46+
throw new RuntimeException("getResource should return null");
47+
}
48+
if (urlClassLoader.findResources(resource).hasMoreElements()) {
49+
throw new RuntimeException("findResources should return an empty enumeration");
50+
}
51+
if (urlClassLoader.getResources(resource).hasMoreElements()) {
52+
throw new RuntimeException("getResources should return an empty enumeration");
53+
}
54+
}
55+
}
56+
private FindResourceDoesNotThrowException() {}
57+
}

0 commit comments

Comments
 (0)