From fc35f840af677bf618daf17f6c4c1dbd25e53a24 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 25 Feb 2021 18:08:04 +0100 Subject: [PATCH] Polishing See gh-26574 See gh-26575 --- .../beans/propertyeditors/PathEditor.java | 3 ++- .../beans/propertyeditors/PathEditorTests.java | 18 +++++++++--------- .../core/io/ClassPathResource.java | 4 +++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java index 3aabd846b904..39754c169702 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.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. @@ -98,6 +98,7 @@ public void setAsText(String text) throws IllegalArgumentException { setValue(null); } else if (!resource.isFile() && !resource.exists() && nioPathCandidate) { + // Prefer getFile().toPath() below for non-existent file handles setValue(Paths.get(text).normalize()); } else { diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java index 60df90c7f103..30cec400bd3c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 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. @@ -34,7 +34,7 @@ public class PathEditorTests { @Test - public void testClasspathPathName() throws Exception { + public void testClasspathPathName() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); @@ -46,14 +46,14 @@ public void testClasspathPathName() throws Exception { } @Test - public void testWithNonExistentResource() throws Exception { + public void testWithNonExistentResource() { PropertyEditor propertyEditor = new PathEditor(); assertThatIllegalArgumentException().isThrownBy(() -> propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc")); } @Test - public void testWithNonExistentPath() throws Exception { + public void testWithNonExistentPath() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("file:/no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); @@ -65,7 +65,7 @@ public void testWithNonExistentPath() throws Exception { } @Test - public void testAbsolutePath() throws Exception { + public void testAbsolutePath() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("/no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); @@ -77,7 +77,7 @@ public void testAbsolutePath() throws Exception { } @Test - public void testWindowsAbsolutePath() throws Exception { + public void testWindowsAbsolutePath() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("C:\\no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); @@ -89,7 +89,7 @@ public void testWindowsAbsolutePath() throws Exception { } @Test - public void testWindowsAbsoluteFilePath() throws Exception { + public void testWindowsAbsoluteFilePath() { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); @@ -101,7 +101,7 @@ public void testWindowsAbsoluteFilePath() throws Exception { } @Test - public void testUnqualifiedPathNameFound() throws Exception { + public void testUnqualifiedPathNameFound() { PropertyEditor pathEditor = new PathEditor(); String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"; @@ -120,7 +120,7 @@ public void testUnqualifiedPathNameFound() throws Exception { } @Test - public void testUnqualifiedPathNameNotFound() throws Exception { + public void testUnqualifiedPathNameNotFound() { PropertyEditor pathEditor = new PathEditor(); String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".clazz"; 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 2dde2a2cf071..6374f2768b9d 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. @@ -160,6 +160,8 @@ else if (this.classLoader != null) { } } catch (IllegalArgumentException ex) { + // Should not happen according to the JDK's contract: + // see https://github.com/openjdk/jdk/pull/2662 return null; } }