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. 9
      spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java

9
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,6 +148,7 @@ public class ClassPathResource extends AbstractFileResolvingResource { @@ -148,6 +148,7 @@ public class ClassPathResource extends AbstractFileResolvingResource {
*/
@Nullable
protected URL resolveURL() {
try {
if (this.clazz != null) {
return this.clazz.getResource(this.path);
}
@ -158,6 +159,12 @@ public class ClassPathResource extends AbstractFileResolvingResource { @@ -158,6 +159,12 @@ public class ClassPathResource extends AbstractFileResolvingResource {
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;
}
}
/**
* This implementation opens an InputStream for the given class path resource.

Loading…
Cancel
Save