Search in sources :

Example 1 with StackBrowser

use of org.jikesrvm.runtime.StackBrowser in project JikesRVM by JikesRVM.

the class VMStackWalker method getCallingClass.

// JavaDoc not quoted because Classpath 0.97.2's JavaDoc is missing a @return tag
public static Class<?> getCallingClass(int skip) {
    StackBrowser b = new StackBrowser();
    VM.disableGC();
    b.init();
    // skip VMStackWalker.getCallingClass(int) (this call)
    b.up();
    while (// Skip what the caller asked for.
    skip-- > 0) b.up();
    /* Skip Method.invoke, (if the caller was called by reflection) */
    if (b.getMethod() == Entrypoints.java_lang_reflect_Method_invokeMethod) {
        b.up();
    }
    /* skip past another frame, whatever getClassContext()[0] would be. */
    if (!b.hasMoreFrames())
        return null;
    b.up();
    /* OK, we're there at getClassContext()[1] now.  Return it. */
    RVMType ret = b.getCurrentClass();
    VM.enableGC();
    return ret.getClassForType();
}
Also used : RVMType(org.jikesrvm.classloader.RVMType) StackBrowser(org.jikesrvm.runtime.StackBrowser)

Example 2 with StackBrowser

use of org.jikesrvm.runtime.StackBrowser in project JikesRVM by JikesRVM.

the class RVMClass method getClassFromStackFrame.

/**
 * Used for accessibility checks in reflection code.
 * Find the class of the method that corresponds to the requested frame.
 *
 * @param skip   Specifies the number of frames back from the
 *               caller to the method whose class is required
 *
 * @return the class that declares the method at the desired frame
 */
@Entrypoint
public static RVMClass getClassFromStackFrame(int skip) {
    // account for stack frame of this function
    skip++;
    StackBrowser browser = new StackBrowser();
    VM.disableGC();
    browser.init();
    while (skip-- > 0) browser.up();
    VM.enableGC();
    return browser.getCurrentClass();
}
Also used : StackBrowser(org.jikesrvm.runtime.StackBrowser) Entrypoint(org.vmmagic.pragma.Entrypoint)

Example 3 with StackBrowser

use of org.jikesrvm.runtime.StackBrowser in project JikesRVM by JikesRVM.

the class VM method getNonBootstrapClassLoader.

/**
 * This method must be provided by the vm vendor, as it is used by other
 * provided class implementations. For example,
 * java.io.ObjectInputStream.readObject() and
 * java.io.ObjectInputStream.resolveProxyClass(). It is also useful for
 * other classes, such as java.rmi.server.RMIClassLoader. Walk the stack and
 * answer the most recent non-null and non-bootstrap ClassLoader on the
 * stack of the calling thread. If no such ClassLoader is found, null is
 * returned. Notes: 1) This method operates on the defining classes of
 * methods on stack. NOT the classes of receivers.
 *
 * @return the first non-bootstrap ClassLoader on the stack
 */
public static final ClassLoader getNonBootstrapClassLoader() {
    StackBrowser browser = new StackBrowser();
    browser.init();
    while (browser.hasMoreFrames()) {
        ClassLoader cl = browser.getClassLoader();
        if (cl != BootstrapClassLoader.getBootstrapClassLoader() && cl != null) {
            return cl;
        }
        browser.up();
    }
    return null;
}
Also used : StackBrowser(org.jikesrvm.runtime.StackBrowser) BootstrapClassLoader(org.jikesrvm.classloader.BootstrapClassLoader)

Example 4 with StackBrowser

use of org.jikesrvm.runtime.StackBrowser in project JikesRVM by JikesRVM.

the class Class method getStackClasses.

// TODO: Harmony
static Class<?>[] getStackClasses(int maxDepth, boolean stopAtPrivileged) {
    StackBrowser browser = new StackBrowser();
    if (maxDepth == -1) {
        browser.init();
        maxDepth = 0;
        while (browser.hasMoreFrames()) {
            maxDepth++;
            browser.up();
        }
    }
    if (maxDepth == 0)
        return new Class[0];
    else if (maxDepth < 0) {
        throw new Error("Unexpected negative call stack size" + maxDepth);
    }
    Class<?>[] result = new Class[maxDepth];
    browser.init();
    for (int i = 0; i < maxDepth; i++) {
        result[i] = browser.getCurrentClass().getClassForType();
        browser.up();
    }
    return result;
}
Also used : StackBrowser(org.jikesrvm.runtime.StackBrowser) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 5 with StackBrowser

use of org.jikesrvm.runtime.StackBrowser in project JikesRVM by JikesRVM.

the class VMStackWalker method getClassContext.

/**
 * Classpath's Javadoc for this method says:
 * <blockquote>
 * Get a list of all the classes currently executing methods on the
 * Java stack. <code>getClassContext()[0]</code> is the class associated
 * with the currently executing method, i.e., the method that called
 * <code>VMStackWalker.getClassContext()</code> (possibly through
 * reflection). So you may need to pop off these stack frames from
 * the top of the stack:
 * <ul>
 * <li><code>VMStackWalker.getClassContext()</code>
 * <li><code>Method.invoke()</code>
 * </ul>
 *
 * @return an array of the declaring classes of each stack frame
 * </blockquote>
 */
public static Class<?>[] getClassContext() {
    StackBrowser b = new StackBrowser();
    int frames = 0;
    VM.disableGC();
    b.init();
    // skip VMStackWalker.getClassContext (this call)
    b.up();
    // Were we invoked by reflection?
    boolean reflected;
    if (b.getMethod() == Entrypoints.java_lang_reflect_Method_invokeMethod) {
        reflected = true;
        // Skip Method.invoke, (if we were called by reflection)
        b.up();
    } else {
        reflected = false;
    }
    /* Count # of frames. */
    while (b.hasMoreFrames()) {
        frames++;
        b.up();
    }
    VM.enableGC();
    RVMType[] iclasses = new RVMType[frames];
    int i = 0;
    b = new StackBrowser();
    VM.disableGC();
    b.init();
    // skip this method
    b.up();
    if (reflected)
        // Skip Method.invoke if we were called by reflection
        b.up();
    while (b.hasMoreFrames()) {
        iclasses[i++] = b.getCurrentClass();
        b.up();
    }
    VM.enableGC();
    Class<?>[] classes = new Class[frames];
    for (int j = 0; j < iclasses.length; j++) {
        classes[j] = iclasses[j].getClassForType();
    }
    return classes;
}
Also used : RVMType(org.jikesrvm.classloader.RVMType) StackBrowser(org.jikesrvm.runtime.StackBrowser)

Aggregations

StackBrowser (org.jikesrvm.runtime.StackBrowser)6 RVMType (org.jikesrvm.classloader.RVMType)2 Entrypoint (org.vmmagic.pragma.Entrypoint)2 BootstrapClassLoader (org.jikesrvm.classloader.BootstrapClassLoader)1 RVMClass (org.jikesrvm.classloader.RVMClass)1