Java SecurityManager checkMemberAccess()方法与示例
SecurityManager類(lèi)的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()方法中 ,默認(rèn)情況下,我們?cè)L問(wèn)具有類(lèi)似類(lèi)加載器的公共成員和類(lèi),例如調(diào)用者,在其他情況下,它調(diào)用checkPermission(“ accessDeclaredMembers”)權(quán)限。
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()方法是一個(gè)非靜態(tài)方法,只能通過(guò)類(lèi)對(duì)象訪問(wèn),如果嘗試使用類(lèi)名稱(chēng)訪問(wèn)該方法,則會(huì)收到錯(cuò)誤消息。
checkMemberAccess() method may throw an exception at the time of accessing members.
checkMemberAccess()方法在訪問(wèn)成員時(shí)可能會(huì)引發(fā)異常。
- 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):
參數(shù):
Class cl – represents the class that indication is to be operated on.
cl類(lèi) –表示要對(duì)其進(jìn)行操作的類(lèi)。
int type – represents the access type like PUBLIC OR DECLARED.
int type –表示訪問(wèn)類(lèi)型,例如PUBLIC或DECLARED。
Return value:
返回值:
The return type of this method is void, it returns nothing.
此方法的返回類(lèi)型為void ,不返回任何內(nèi)容。
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
總結(jié)
以上是生活随笔為你收集整理的Java SecurityManager checkMemberAccess()方法与示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: css3浏览,css3支持哪些浏览器?
- 下一篇: 字符串的回文子序列个数_计算给定字符串中