Java.util.PropertyPermission.newPermission Collection() 方法

描述

java.util.PropertyPermission.newPermissionCollection() 方法创建并返回一个新的 PermissionCollection 对象。


声明

以下是 java.util.PropertyPermission.newPermissionCollection() 方法的声明

public PermissionCollection newPermissionCollection()

参数

NA


返回值

该方法返回新创建的 PermissionCollection 对象。


异常

NA


示例

下面的例子展示了 java.util.PropertyPermission.newPermissionCollection() 方法的使用。

package com.tutorialspoint;

import java.security.PermissionCollection;
import java.util.PropertyPermission;

public class PropertyPermissionDemo {
   private static PermissionCollection permissions;
   
   public static void main(String[] args) {

      // Build property permissions collection
      PropertyPermission permission = 
      new PropertyPermission("java.*", "read");
      permissions = permission.newPermissionCollection();
      permissions.add(permission);
      permissions.add(new PropertyPermission("java.home.*", "read,write"));

      // Check permissions
      checkFileReadPermission("java.home");
      checkFileWritePermission("java.home");
      checkFileReadPermission("java.home.usr");
      checkFileWritePermission("java.home.usr");
   }

   private static void checkFileReadPermission(String path) {
      
      // Check permission on read action
      if(permissions.implies(new PropertyPermission(path, "read"))) {
         System.out.println("Has permissions on "+path+" for read");
      }
   }
   
   private static void checkFileWritePermission(String path) {
      
      // Check permission on read action
      if(permissions.implies(new PropertyPermission(path, "write"))) {
         System.out.println("Has permissions on "+path+" for write");
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 −

Has permissions on java.home for read
Has permissions on java.home.usr for read
Has permissions on java.home.usr for write