📌  相关文章
📜  Java的.lang.reflect.ReflectPermission类在Java中

📅  最后修改于: 2022-05-13 01:54:19.809000             🧑  作者: Mango

Java的.lang.reflect.ReflectPermission类在Java中

ReflectPermission 类扩展了 BasicPermission 类。它是一个“命名”权限,即它包含一个名称但不包含任何操作。如果需要,它可以在 BasicPermission 之上实现操作。它用于获取有关构造函数行为的信息。

ConstructorsDescription
ReflectPermission(String name)It is used to create a ReflectPermission with the specified name.
ReflectPermission(String name, String action)It is used to create a ReflectPermission with the specified name and action.

从类Java.security.BasicPermission 继承的方法:

MethodsDescription
equals(Object obj)It checks whether the two BasicPermission objects are equal or not.
getActions()It returns the actions in String format, which is currently an empty string as there is no action for ReflectPermission.
hashCode()It returns the hash code value for this object.
implies(Permission permission)It checks whether the given permission is implied by this object or not.
newPermissionCollection()It returns a new PermissionCollection object.

以下是给定类的示例/使用:

Java
// Use of java.lang.reflect.ReflectPermission Class in Java
import java.lang.reflect.ReflectPermission;
class GFG {
    public static void main(String[] args)
    {
        if (canAccessPrivateMethods()) {
            System.out.println("Permission granted");
        }
        else {
            System.out.println("Permission not granted");
        }
    }
    static boolean canAccessPrivateMethods()
    {
        try {
            SecurityManager securityManager
                = System.getSecurityManager();
            if (null != securityManager) {
                securityManager.checkPermission(
                    new ReflectPermission(
                        "suppressAccessChecks"));
            }
        }
        catch (SecurityException e) {
            return false;
        }
        return true;
    }
}


输出
Permission not granted