Browse Source

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
pull/27107/head
Juergen Hoeller 5 years ago
parent
commit
74b248a6b3
  1. 23
      spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -148,14 +148,21 @@ public class ClassPathResource extends AbstractFileResolvingResource { @@ -148,14 +148,21 @@ public class ClassPathResource extends AbstractFileResolvingResource {
*/
@Nullable
protected URL resolveURL() {
if (this.clazz != null) {
return this.clazz.getResource(this.path);
}
else if (this.classLoader != null) {
return this.classLoader.getResource(this.path);
try {
if (this.clazz != null) {
return this.clazz.getResource(this.path);
}
else if (this.classLoader != null) {
return this.classLoader.getResource(this.path);
}
else {
return ClassLoader.getSystemResource(this.path);
}
}
else {
return ClassLoader.getSystemResource(this.path);
catch (IllegalArgumentException ex) {
// Should not happen according to the JDK's contract:
// see https://github.com/openjdk/jdk/pull/2662
return null;
}
}

Loading…
Cancel
Save