Skip to content

Commit 74b248a

Browse files
committed
getResource can throw IllegalArgumentException
Class.getResource, ClassLoader.getResource, and ClassLoader.getSystemResource will throw IllegalArgumentException if a malformed URL is provided to them. According to its javadoc, resolveURL should return null if not resolvable, so catch the IllegalArgumentException and return null. Closes gh-26574
1 parent 60e4189 commit 74b248a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,14 +148,21 @@ public boolean exists() {
148148
*/
149149
@Nullable
150150
protected URL resolveURL() {
151-
if (this.clazz != null) {
152-
return this.clazz.getResource(this.path);
153-
}
154-
else if (this.classLoader != null) {
155-
return this.classLoader.getResource(this.path);
151+
try {
152+
if (this.clazz != null) {
153+
return this.clazz.getResource(this.path);
154+
}
155+
else if (this.classLoader != null) {
156+
return this.classLoader.getResource(this.path);
157+
}
158+
else {
159+
return ClassLoader.getSystemResource(this.path);
160+
}
156161
}
157-
else {
158-
return ClassLoader.getSystemResource(this.path);
162+
catch (IllegalArgumentException ex) {
163+
// Should not happen according to the JDK's contract:
164+
// see https://github.com/openjdk/jdk/pull/2662
165+
return null;
159166
}
160167
}
161168

0 commit comments

Comments
 (0)