Java SecurityManager checkMemberAccess()方法与示例

SecurityManager类的checkMemberAccess()方法 (SecurityManager Class checkMemberAccess() method)

  • checkMemberAccess() method is available in java.lang package.

    checkMemberAccess()方法在java.lang包中可用。

  • In checkMemberAccess() method we access public members and classes that have similar class loaders like caller by default and in other cases, it calls checkPermission("accessDeclaredMembers") permission.

    checkMemberAccess()方法中 ,默认情况下,我们访问具有类似类加载器的公共成员和类,例如调用者,在其他情况下,它调用checkPermission(“ accessDeclaredMembers”)权限。

  • checkMemberAccess() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    checkMemberAccess()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • checkMemberAccess() method may throw an exception at the time of accessing members.

    checkMemberAccess()方法在访问成员时可能会引发异常。

    • SecurityException – This exception may throw when the calling thread does not have the right to access members.
    • NullPointerException – This exception may throw when the given first parameter is null.

Syntax:

句法:

    public void checkMemberAccess(Class cl, int type);

Parameter(s):

参数:

  • Class cl – represents the class that indication is to be operated on.

    cl类 –表示要对其进行操作的类。

  • int type – represents the access type like PUBLIC OR DECLARED.

    int type –表示访问类型,例如PUBLIC或DECLARED。

Return value:

返回值:

The return type of this method is void, it returns nothing.

此方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example 
// of void checkMemberAccess(Class cl, int type)
// method of SecurityManager 

import java.lang.reflect.*;

public class CheckMemberAccess extends SecurityManager {
    public void checkMemberAccess(Class cl, int type) {
        throw new SecurityException("Restricted..");
    }

    public static void main(String[] args) {

        // By using setProperty() method is to set the policy property 
        // with security manager
        System.setProperty("java.security.policy", "file:/C:/java.policy");

        // Instantiating a CheckMemberAccess object
        CheckMemberAccess cma = new CheckMemberAccess();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(cma);

        // By using checkMemberAccess(Class,type) method is to check 
        // accessibility of the member
        cma.checkMemberAccess(CheckMemberAccess.class, Member.DECLARED);

        // Display the message
        System.out.println("Not Restricted..");
    }
}

Output

输出量

Exception in thread "main" java.lang.SecurityException: Restricted..
	at CheckMemberAccess.checkMemberAccess(CheckMemberAccess.java:9)
	at CheckMemberAccess.main(CheckMemberAccess.java:27)


翻译自: https://www.includehelp.com/java/securitymanager-checkmemberaccess-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值