如果class.getProtectionDomain返回null,这意味着什么

时间:2012-01-09 19:25:59

标签: class

我有一个方法可以返回加载特定类的jar。方法如下。 对于某些类,下面的行返回null

ProtectionDomain protectionDomain = c.getProtectionDomain();

我想了解它在什么情况下是null。代码编译所以我认为类在编译时是可见的,并且特定类所在的项目的依赖性也是编译时依赖性。

这是方法

  public static String jarFor(Class c) {
    ProtectionDomain protectionDomain = c.getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URL url = codeSource.getLocation();
    String path = url.getPath();
    if (Os.isWindows() && path.startsWith("/")) {
      path = path.substring(1);
    }
    return URLDecoder.decode(path);
  }

1 个答案:

答案 0 :(得分:1)

javadoc或java代码本身都没有表明getProtectionDomain可以返回null。

public java.security.ProtectionDomain getProtectionDomain() {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(SecurityConstants.GET_PD_PERMISSION);
    }
    java.security.ProtectionDomain pd = getProtectionDomain0();
    if (pd == null) {
        if (allPermDomain == null) {
            java.security.Permissions perms = 
                new java.security.Permissions();
            perms.add(SecurityConstants.ALL_PERMISSION);
            allPermDomain = 
                new java.security.ProtectionDomain(null, perms);
        }
        pd = allPermDomain;
    }
    return pd;
}
相关问题