Skip to content

Commit 08c79bf

Browse files
committed
Handle paths illegal on Windows in ImageDescriptor.createFromFile()
Handle filenames, that are used as file-system paths because the context class is null and are illegal on Windows, for example like: /C:/data/other This happens if retrieved from a URL/URI by just using 'URL/URI.getPath()' instead of using 'Path.of(uri)' or 'new File(uri)'. Using IPath.fromOSString() instead of Path.of(), allows to use these kind of illegal paths.
1 parent 4fa1eee commit 08c79bf

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import java.net.MalformedURLException;
1717
import java.net.URI;
1818
import java.net.URL;
19-
import java.nio.file.Path;
2019
import java.util.function.Supplier;
2120

21+
import org.eclipse.core.runtime.IPath;
2222
import org.eclipse.jface.util.Policy;
2323
import org.eclipse.swt.SWTException;
2424
import org.eclipse.swt.graphics.Device;
@@ -92,7 +92,8 @@ public static ImageDescriptor createFromFile(Class<?> location, String filename)
9292
URL url;
9393
if (location == null) {
9494
try {
95-
url = Path.of(filename).toUri().toURL();
95+
// Use IPath, which can handle illegal path's on Windows like: /C:/data/other
96+
url = IPath.fromOSString(filename).toPath().toUri().toURL();
9697
} catch (MalformedURLException e) {
9798
Policy.logException(e);
9899
url = null;

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/FileImageDescriptorTest.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static org.junit.Assert.assertNotNull;
2525
import static org.junit.Assert.assertNotSame;
2626
import static org.junit.Assert.assertNull;
27-
import static org.junit.Assert.fail;
2827

2928
import java.io.IOException;
3029
import java.net.URL;
@@ -55,7 +54,7 @@ public class FileImageDescriptorTest {
5554
* Test loading the image descriptors.
5655
*/
5756
@Test
58-
public void testFileImageDescriptorWorkbench() {
57+
public void testFileImageDescriptorWorkbench() throws IOException {
5958

6059
Class<?> missing = null;
6160
ArrayList<Image> images = new ArrayList<>();
@@ -64,7 +63,6 @@ public void testFileImageDescriptorWorkbench() {
6463
Enumeration<String> bundleEntries = bundle.getEntryPaths(IMAGES_DIRECTORY);
6564

6665
while (bundleEntries.hasMoreElements()) {
67-
ImageDescriptor descriptor;
6866
String localImagePath = bundleEntries.nextElement();
6967
URL[] files = FileLocator.findEntries(bundle, IPath.fromOSString(localImagePath));
7068

@@ -75,12 +73,8 @@ public void testFileImageDescriptorWorkbench() {
7573
continue;
7674
}
7775

78-
try {
79-
descriptor = ImageDescriptor.createFromFile(missing, FileLocator.toFileURL(file).getFile());
80-
} catch (IOException e) {
81-
fail(e.getLocalizedMessage());
82-
continue;
83-
}
76+
ImageDescriptor descriptor = ImageDescriptor.createFromFile(missing,
77+
FileLocator.toFileURL(file).getFile());
8478

8579
Image image = descriptor.createImage();
8680
images.add(image);

0 commit comments

Comments
 (0)