欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

Java SecurityManager checkMemberAccess()方法与示例

发布时间:2025/3/11 java 66 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 managerSystem.setProperty("java.security.policy", "file:/C:/java.policy");// Instantiating a CheckMemberAccess objectCheckMemberAccess cma = new CheckMemberAccess();// By using setSecurityManager() method is to set the// security managerSystem.setSecurityManager(cma);// By using checkMemberAccess(Class,type) method is to check // accessibility of the membercma.checkMemberAccess(CheckMemberAccess.class, Member.DECLARED);// Display the messageSystem.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

总结

以上是生活随笔为你收集整理的Java SecurityManager checkMemberAccess()方法与示例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。