java.net.URI对象的getPath方法是否可能返回null?(若有,何时?)

java.net.URI对象的getPath方法是否可能返回null?(若有,何时?),java,null,uri,Java,Null,Uri,根据getPath方法返回“此URI的解码路径组件,,如果路径未定义,则返回null”(强调添加)。这会让我相信,如果我的应用程序依赖于getPath的返回值,我可能需要检查它是否为null。然而,这似乎永远不会发生 下面的代码显示了我试图构造一个URI对象,该对象的getPath返回null,但是正如您所看到的,我还没有找到这样的情况。有人能解释一下这是怎么发生的吗 编辑:我注意到,mailtoURI没有路径。然而,我真正想问的是:是否存在使用http、https、ftp或文件模式的URI,其

根据getPath方法返回“此URI的解码路径组件,,如果路径未定义,则返回null”(强调添加)。这会让我相信,如果我的应用程序依赖于
getPath
的返回值,我可能需要检查它是否为
null
。然而,这似乎永远不会发生

下面的代码显示了我试图构造一个
URI
对象,该对象的
getPath
返回null,但是正如您所看到的,我还没有找到这样的情况。有人能解释一下这是怎么发生的吗

编辑:我注意到,
mailto
URI没有路径。
然而,我真正想问的是:是否存在使用http、https、ftp或文件模式的URI,其路径未定义/为空

import java.net.URI;
import java.net.URISyntaxException;

public class URIGetPathNullTest {

    public static void main(String []args) throws Exception {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();
    }


    public static void test1() throws URISyntaxException {
        String urlString = "";
        URI uri = new URI(urlString);
        printUri(uri); 
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false         
    }

    public static void test2() throws URISyntaxException{
        String scheme = null;
        String ssp = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                ssp,
                fragment
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false
    }

    public static void test3() throws URISyntaxException {
        String scheme = null;
        String userInfo = null;
        String host = null;
        int port = -1;
        String path = null;
        String query = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                userInfo,
                host,
                port,
                path,
                query,
                fragment             
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false

    }

    public static void test4() throws URISyntaxException {
        String scheme = null;
        String host = null;
        String path = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                host,
                path,
                fragment             
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false
    }

    public static void test5() throws URISyntaxException {
        String scheme = null;
        String authority = null;
        String path = null;
        String query = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                authority,
                path,
                query,
                fragment             
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false
    }

    public static void test6() throws URISyntaxException {
        String urlString = "?some-query";
        URI uri = new URI(urlString);
        printUri(uri); 
        // Output:
        //  toString() --> ?some-query
        //  getPath --> 
        //  getPath null? false         
    }

    public static void test7() throws URISyntaxException {
        String urlString = "#some-fragment";
        URI uri = new URI(urlString);
        printUri(uri); 
        // Output:
        //  toString() --> #some-fragment
        //  getPath --> 
        //  getPath null? false         
    }

    public static void printUri(URI uri) {
        System.out.println("toString() --> " + uri.toString());
        System.out.println("getPath --> " + uri.getPath());
        System.out.println("getPath null? " + (uri.getPath() == null));
    }
}
我仔细阅读了一遍,注意到我只想到了对应于http/ftp/file方案的uri。
mailto
URI就是一个没有路径的示例

public static void test8() throws URISyntaxException {
    String urlString = "mailto:mduerst@ifi.unizh.ch";
    URI uri = new URI(urlString);
    printUri(uri); 
    // Output:
    //  toString() --> mailto:mduerst@ifi.unizh.ch
    //  getPath --> null
    //  getPath null? true          
}
编辑:与
新闻
方案类似

public static void test9() throws URISyntaxException {
    String urlString = "news:comp.infosystems.www.servers.unix";
    URI uri = new URI(urlString);
    printUri(uri); 
    // Output:
    //  toString() --> news:comp.infosystems.www.servers.unix
    //  getPath --> null
    //  getPath null? true          
}

(我将下面的其他答案合并起来)

这里有一个很好的线索:

不透明URI是其方案特定部分不以斜杠开头的绝对URI 字符(“/”)。不透明URI不受进一步解析的约束。不透明的一些例子 URI是:

mailto:java-net@java.sun.com
新闻:comp.lang.java
urn:isbn:096139210x

似乎
getPath
仅当
URI
不透明时才返回null,这很容易用。因此,如果我确保URI不是不透明的,那么我就不需要检查
getPath
null
结果

查看openjdk 7-b147中的注释,我注意到
路径
字段中有一条注释,然后查看
isOpaque
的源代码,这似乎证实了这一点:

private transient String path;  // null ==> opaque

// ...

public boolean isOpaque() {
    return path == null;
}