Java Class getProtectionDomain() 方法



描述

Java Class getProtectionDomain() 方法返回此類的 ProtectionDomain。

宣告

以下是 java.lang.Class.getProtectionDomain() 方法的宣告

public ProtectionDomain getProtectionDomain()

引數

返回值

此方法返回此類的 ProtectionDomain。

異常

SecurityException − 如果存在安全管理器並且其 checkPermission 方法不允許獲取 ProtectionDomain。

獲取類的 ProtectionDomain 示例

以下示例演示了 java.lang.Class.getName() 方法的使用。在此程式中,使用 forName() 方法檢索 ClassDemo 的類。使用 getName(),我們檢索了類的 ProtectionDomain 並打印出來。

package com.tutorialspoint;

import java.security.ProtectionDomain;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = Class.forName("com.tutorialspoint.ClassDemo"); 

         // returns the name of the class
         System.out.println("Class = " + cls.getName());
      
         // returns the ProtectionDomain of this class.
         ProtectionDomain p = cls.getProtectionDomain();
         System.out.println(p);
      } catch(ClassNotFoundException ex) {
        System.out.println(ex.toString());
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Class = com.tutorialspoint.ClassDemo
ProtectionDomain  (file:/C:/Users/Tutorialspoint/eclipse-workspace/Tutorialspoint/bin/ <no signer certificates>)
 jdk.internal.loader.ClassLoaders$AppClassLoader@5c647e05
 <no principals>
 java.security.Permissions@816f27d (
 ("java.io.FilePermission" "C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\bin\-" "read")
 ("java.lang.RuntimePermission" "exitVM")
)

獲取 ArrayList 的 ProtectionDomain 示例

以下示例演示了 java.lang.Class.getName() 方法的使用。在此程式中,檢索 ArrayList 的類。使用 getName(),我們檢索了類的 ProtectionDomain 並打印出來。

package com.tutorialspoint;

import java.security.ProtectionDomain;
import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {

      Class cls = ArrayList.class; 

      // returns the name of the class
      System.out.println("Class = " + cls.getName());

      // returns the ProtectionDomain of this class.
      ProtectionDomain p = cls.getProtectionDomain();
      System.out.println(p);
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Class = java.util.ArrayList
ProtectionDomain  null
 null
 <no principals>
 java.security.Permissions@33c7353a (
 ("java.security.AllPermission" "<all permissions>" "<all actions>")
)
java_lang_class.htm
廣告

© . All rights reserved.