Java Data Type Tutorial - Java SecurityManager .checkMemberAccess (Class <?> clazz, int which)








Syntax

SecurityManager.checkMemberAccess(Class <?> clazz, int which) has the following syntax.

public void checkMemberAccess(Class <?> clazz,   int which)

Example

In the following code shows how to use SecurityManager.checkMemberAccess(Class <?> clazz, int which) method.

// w  ww  . j av a 2  s  .c o  m
import java.lang.reflect.Member;

public class Main extends SecurityManager {

   // checkMemberAccess needs to be overriden.
   @Override
   public void checkMemberAccess(Class<?>  clazz, int which) {
      throw new SecurityException();
   }

   public static void main(String[] args) {
      System.setProperty("java.security.policy", "file:/C:/java.policy");

      Main sm = new Main();

      System.setSecurityManager(sm);

      sm.checkMemberAccess(Main.class, Member.PUBLIC);

      System.out.println("Allowed!");
   }
}