+ Use the Jackpot-format upgrade hint. +
+
+ Utilities
gets new methods toURI(File)
+ and toFile(URI)
which offer the UNC safety of NIO.2
+ even on JDK 6.
+
file:
URL.
* @param f a file (absolute only)
@@ -3030,7 +3113,7 @@
* @see #toFile
* @see Issue #29711
* @since 3.26
- * @deprecated Use {@link File#toURI} and {@link URI#toURL} instead under JDK 1.4.
+ * @deprecated Use {@link #toURI} and {@link URI#toURL} instead under JDK 1.4.
* ({@link File#toURL} is buggy in JDK 1.3 and the bugs are not fixed in JDK 1.4.)
*/
@Deprecated
@@ -3043,7 +3126,7 @@
throw new IllegalArgumentException("Relative path: " + f); // NOI18N
}
- URI uri = f.toURI();
+ URI uri = toURI(f);
return uri.toURL();
}
@@ -3059,7 +3142,7 @@
* @see #toURL
* @see Issue #29711
* @since 3.26
- * @deprecated Use {@link URI#URI(String)} and {@link File#File(URI)} instead under JDK 1.4.
+ * @deprecated Use {@link URL#toURI} and {@link #toFile(URI)} instead under JDK 1.4.
* (There was no proper equivalent under JDK 1.3.)
*/
@Deprecated
@@ -3069,9 +3152,9 @@
}
try {
- URI uri = new URI(u.toExternalForm());
-
- return new File(uri);
+ URI uri = u.toURI();
+
+ return toFile(uri);
} catch (URISyntaxException use) {
// malformed URL
return null;
--- a/openide.util/test/unit/src/org/netbeans/modules/openide/util/NbBundleProcessorTest.java
+++ a/openide.util/test/unit/src/org/netbeans/modules/openide/util/NbBundleProcessorTest.java
@@ -54,6 +54,7 @@
import org.openide.util.NbBundle.Messages;
import org.openide.util.test.TestFileUtils;
import static org.netbeans.modules.openide.util.Bundle.*;
+import org.openide.util.Utilities;
@Messages("k3=value #3")
public class NbBundleProcessorTest extends NbTestCase {
@@ -103,7 +104,7 @@
"public static final Object X = new Object() {public String toString() {return Bundle.k();}};",
"}");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
- ClassLoader l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ ClassLoader l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v", l.loadClass("p.C").getField("X").get(null).toString());
}
@@ -138,7 +139,7 @@
public void testPackageKeys() throws Exception {
AnnotationProcessorTestUtils.makeSource(src, "p.package-info", "@org.openide.util.NbBundle.Messages(\"k=v\")", "package p;");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
- ClassLoader l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ ClassLoader l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
Method m = l.loadClass("p.Bundle").getDeclaredMethod("k");
m.setAccessible(true);
assertEquals("v", m.invoke(null));
@@ -230,17 +231,17 @@
AnnotationProcessorTestUtils.makeSource(src, "p.C2", "@org.openide.util.NbBundle.Messages(\"k2=v2\")", "public class C2 {public @Override String toString() {return Bundle.k2();}}");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
- ClassLoader l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ ClassLoader l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v1", l.loadClass("p.C1").newInstance().toString());
assertEquals("v2", l.loadClass("p.C2").newInstance().toString());
AnnotationProcessorTestUtils.makeSource(src, "p.C1", "@org.openide.util.NbBundle.Messages(\"k1=v3\")", "public class C1 {public @Override String toString() {return Bundle.k1();}}");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, "C1.java", dest, null, null));
- l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v3", l.loadClass("p.C1").newInstance().toString());
assertEquals("v2", l.loadClass("p.C2").newInstance().toString());
AnnotationProcessorTestUtils.makeSource(src, "p.C1", "@org.openide.util.NbBundle.Messages(\"k3=v4\")", "public class C1 {public @Override String toString() {return Bundle.k3();}}");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, "C1.java", dest, null, null));
- l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v4", l.loadClass("p.C1").newInstance().toString());
assertEquals("v2", l.loadClass("p.C2").newInstance().toString());
}
@@ -251,7 +252,7 @@
AnnotationProcessorTestUtils.makeSource(src, "p.C3", "class C3 {C3() {new Runnable() {public @Override void run() {new Runnable() {public @Override void run() {}};}};}}");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
- ClassLoader l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ ClassLoader l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v1", l.loadClass("p.C1").newInstance().toString());
assertEquals("v2", l.loadClass("p.C2").newInstance().toString());
assertTrue(new File(dest, "p/C3.class").delete());
@@ -259,7 +260,7 @@
assertTrue(new File(dest, "p/C3$1$1.class").isFile());
AnnotationProcessorTestUtils.makeSource(src, "p.C1", "@org.openide.util.NbBundle.Messages(\"k1=v3\")", "public class C1 {public @Override String toString() {return Bundle.k1();}}");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, "C1.java", dest, null, null));
- l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v3", l.loadClass("p.C1").newInstance().toString());
assertEquals("v2", l.loadClass("p.C2").newInstance().toString());
}
@@ -269,11 +270,11 @@
AnnotationProcessorTestUtils.makeSource(src, "p.package-info", "@org.openide.util.NbBundle.Messages(\"k2=v2\")", "package p;");
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null));
- ClassLoader l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ ClassLoader l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v1v2", l.loadClass("p.C").newInstance().toString());
assertTrue(new File(dest, "p/C.class").delete());
assertTrue(AnnotationProcessorTestUtils.runJavac(src, "C.java", dest, null, null));
- l = new URLClassLoader(new URL[] {dest.toURI().toURL()});
+ l = new URLClassLoader(new URL[] {Utilities.toURI(dest).toURL()});
assertEquals("v1v2", l.loadClass("p.C").newInstance().toString());
}
--- a/openide.util/test/unit/src/org/openide/util/EditablePropertiesTest.java
+++ a/openide.util/test/unit/src/org/openide/util/EditablePropertiesTest.java
@@ -388,7 +388,7 @@
private String filenameOfTestProperties() {
// #50987: never use URL.path for this purpose...
- return new File(URI.create(EditablePropertiesTest.class.getResource("data/test.properties").toExternalForm())).getAbsolutePath();
+ return Utilities.toFile(URI.create(EditablePropertiesTest.class.getResource("data/test.properties").toExternalForm())).getAbsolutePath();
}
private EditableProperties loadTestProperties() throws IOException {
--- a/openide.util/test/unit/src/org/openide/util/UtilitiesTest.java
+++ a/openide.util/test/unit/src/org/openide/util/UtilitiesTest.java
@@ -112,6 +112,7 @@
import java.awt.peer.TextFieldPeer;
import java.awt.peer.WindowPeer;
import java.io.File;
+import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@@ -380,6 +381,31 @@
}
}
+ public void testFileURI() throws Exception {
+ if (Utilities.isWindows()) {
+ assertFileURI("C:\\some\\path #1", "file:/C:/some/path%20%231");
+ assertEquals(new File("C:\\some\\path"), Utilities.toFile(new URI("file:/C:/some/path")));
+ assertEquals(new File("C:\\some\\path"), Utilities.toFile(new URI("file:///C:/some/path")));
+ assertEquals(new File("C:\\some\\path"), Utilities.toFile(new URI("file:/C:/some/path/")));
+ assertFileURI("\\\\server\\share\\path", "file://server/share/path");
+ assertEquals(new File("\\\\server\\share\\path"), Utilities.toFile(new URI("file:////server/share/path")));
+ } else {
+ assertFileURI("/some/path #1", "file:/some/path%20%231");
+ assertEquals(new File("/some/path"), Utilities.toFile(new URI("file:/some/path")));
+ assertEquals(new File("/some/path"), Utilities.toFile(new URI("file:///some/path")));
+ assertEquals(new File("/some/path"), Utilities.toFile(new URI("file:/some/path/")));
+ }
+ String s = Utilities.toURI(getWorkDir()).toString();
+ assertTrue(s, s.endsWith("/"));
+ // XXX test that IllegalArgumentException is thrown where appropriate
+ }
+ private static void assertFileURI(String file, String uri) throws Exception {
+ URI u = new URI(uri);
+ File f = new File(file);
+ assertEquals(u, Utilities.toURI(f));
+ assertEquals(f, Utilities.toFile(u));
+ }
+
private static class CustomToolkitComponent extends Component {
private Toolkit customToolkit;
--- a/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java
+++ a/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java
@@ -59,6 +59,7 @@
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.netbeans.junit.NbTestCase;
+import org.openide.util.Utilities;
import org.openide.util.test.TestFileUtils;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
@@ -454,7 +455,7 @@
File ent = new File(d, "ent.xml");
TestFileUtils.writeFile(main, "]>