From f8f4ad7f7217885dc08e48838f04426da0b96cd5 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 5 Mar 2023 09:17:34 +0100 Subject: [PATCH] Add factory methods to construct an IPath from io.File/nio.Path --- .../equinox/common/tests/PathTest.java | 29 +++++++++++++++++++ .../src/org/eclipse/core/runtime/IPath.java | 24 ++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/PathTest.java b/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/PathTest.java index cb1161f4252..f198f950b85 100644 --- a/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/PathTest.java +++ b/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/PathTest.java @@ -16,6 +16,7 @@ import static org.junit.Assert.assertNotEquals; +import java.io.File; import java.util.ArrayList; import java.util.List; @@ -403,6 +404,34 @@ public void testFromPortableString() { assertEquals("3.12.posix", isLocalPosix, posix2.isValidSegment(":")); } + public void testFromFile() { + List segments = List.of("first", "first/second/third"); + for (String segment : segments) { + File file0 = new File(segment); + assertEquals(file0, IPath.fromFile(file0).toFile()); + File file1 = new File(segment + "/"); + assertEquals(file1, IPath.fromFile(file1).toFile()); + File file2 = new File("/" + segment); + assertEquals(file2, IPath.fromFile(file2).toFile()); + File file3 = new File("/" + segment + "/"); + assertEquals(file3, IPath.fromFile(file3).toFile()); + } + } + + public void testFromPath() { + List segments = List.of("first", "first/second/third"); + for (String segment : segments) { + java.nio.file.Path path0 = java.nio.file.Path.of(segment); + assertEquals(path0, IPath.fromPath(path0).toPath()); + java.nio.file.Path path1 = java.nio.file.Path.of(segment + "/"); + assertEquals(path1, IPath.fromPath(path1).toPath()); + java.nio.file.Path path2 = java.nio.file.Path.of("/" + segment); + assertEquals(path2, IPath.fromPath(path2).toPath()); + java.nio.file.Path path3 = java.nio.file.Path.of("/" + segment + "/"); + assertEquals(path3, IPath.fromPath(path3).toPath()); + } + } + public void testGetFileExtension() { IPath anyPath = new Path("index.html"); diff --git a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/IPath.java b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/IPath.java index f6591f01701..5cb46940246 100644 --- a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/IPath.java +++ b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/IPath.java @@ -11,7 +11,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Patrick Tasse - Add extra constructor to Path class (bug 454959) - * Hannes Wellmann - Add static IPath factory methods + * Hannes Wellmann - Add static IPath factory methods and add methods to create an IPath from a io.File/nio.Path *******************************************************************************/ package org.eclipse.core.runtime; @@ -117,6 +117,28 @@ public static IPath forWindows(String windowsPath) { return new Path(windowsPath, true); } + /** + * Constructs a new {@code IPath} from the given {@code File}. + * + * @param file the java.io.File object + * @return the IPath representing the given File object + * @since 3.18 + */ + public static IPath fromFile(java.io.File file) { + return fromOSString(file.toString()); + } + + /** + * Constructs a new {@code IPath} from the given {@code java.nio.file.Path}. + * + * @param path the java.nio.file.Path object + * @return the IPath representing the given Path object + * @since 3.18 + */ + public static IPath fromPath(java.nio.file.Path path) { + return fromOSString(path.toString()); + } + /** * Returns a new path which is the same as this path but with * the given file extension added. If this path is empty, root or has a