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