From 74b248a6b367c82f3a84ff3810c864357279cf1e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 25 Feb 2021 19:07:58 +0100 Subject: [PATCH] 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 --- .../core/io/ClassPathResource.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java index c618dfddbd..6374f2768b 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java @@ -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 { */ @Nullable protected URL resolveURL() { - if (this.clazz != null) { - return this.clazz.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 if (this.classLoader != null) { - return this.classLoader.getResource(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; } }