Home » Java programming language

Java SecurityManager getClassContext() method with example

SecurityManager Class getClassContext() method: Here, we are going to learn about the getClassContext() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 18, 2019

SecurityManager Class getClassContext() method

  • getClassContext() method is available in java.lang package.
  • getClassContext() method is used to return currently executing stack trace as an array of "Class" type.
  • The number of the methods on the stack is the length of an array and the index[0] element indicates the class name of currently executed method and adjacent index[1] element indicate the class name of currently executed method caller and so..on.
  • getClassContext() 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.
  • getClassContext() method does not throw an exception at the time of Class [] of current execution stack.

Syntax:

    public Class[] getClassContext();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Class[], it returns current execution stack trace as an array of "Class" types.

Example:

// Java program to demonstrate the example 
// of Class[] getClassContext() method of SecurityManager 

import java.security.*;

public class GetClassContext extends SecurityManager {
    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 GetClassContext object
        GetClassContext gc = new GetClassContext();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(gc);

        // By using getContext() method is to return the
        // array of Class context
        Class[] cl = gc.getClassContext();

        //  Display Class context array
        for (int k = 0; k < cl.length; ++k)
            System.out.println("cl[k] = " + cl[k]);
    }
}

Output

cl[k] = class GetClassContext


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.