2012-01-09 1 views
1

특정 클래스가로드 된 jar를 리턴하는 메소드가 있습니다. 방법은 다음과 같습니다. 일부 클래스의 경우 아래 줄은 null을 반환합니다.class.getProtectionDomain이 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

는 어느 쪽도 아니 javadoc 또는 자바 코드 그 자체가 null getProtectionDomain 반환 할 수있어서 나타냄을한다.

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; 
} 
관련 문제