Search in sources :

Example 1 with StackFrame

use of java.lang.StackWalker.StackFrame in project openj9 by eclipse.

the class MyLoader method testStackTop.

@Test
public void testStackTop() {
    StackWalker myWalker = StackWalker.getInstance();
    StackFrame result = myWalker.walk(s -> s.findFirst().orElse(null));
}
Also used : StackFrame(java.lang.StackWalker.StackFrame) Test(org.testng.annotations.Test)

Example 2 with StackFrame

use of java.lang.StackWalker.StackFrame in project openj9 by eclipse.

the class MyLoader method dumpModules.

private void dumpModules() {
    HashSet<Option> opts = new HashSet<>(Arrays.asList(new Option[] { Option.RETAIN_CLASS_REFERENCE, Option.SHOW_HIDDEN_FRAMES, Option.SHOW_REFLECT_FRAMES }));
    List<StackFrame> frameList = (StackWalker.getInstance(opts)).walk(s -> s.collect(Collectors.toList()));
    for (StackFrame f : frameList) {
        Module frameModule = f.getDeclaringClass().getModule();
        if ((null != frameModule)) {
            ModuleDescriptor d = frameModule.getDescriptor();
            if (null != d) {
                StackTraceElement e = f.toStackTraceElement();
                String modName = d.name();
                if (null != modName) {
                    assertEquals(modName, e.getModuleName(), "Wrong module name");
                }
                Optional<Version> modVersion = d.version();
                if (modVersion.isPresent()) {
                    assertEquals(modVersion.get(), e.getModuleVersion(), "Wrong module version");
                }
                if (f.getDeclaringClass() == Thread.class) {
                    assertEquals("java.base", e.getModuleName(), "Wrong module name");
                }
            }
        }
    }
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) Version(java.lang.module.ModuleDescriptor.Version) StackFrame(java.lang.StackWalker.StackFrame) Option(java.lang.StackWalker.Option) Module(java.lang.Module) HashSet(java.util.HashSet)

Example 3 with StackFrame

use of java.lang.StackWalker.StackFrame in project openj9 by eclipse.

the class MyLoader method sanityTest.

void sanityTest(StackWalker myWalker) {
    logMessage("Methods from getStackTrace");
    for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
        String fullMethodNameFromElement = getFullMethodNameFromElement(e);
        logMessage(fullMethodNameFromElement);
    }
    List<StackFrame> frameList = myWalker.walk(s -> s.collect(Collectors.toList()));
    logMessage("Methods from StackWalker");
    for (StackFrame f : frameList) {
        logMessage(getFullMethodNameFromFrame(f) + (f.isNativeMethod() ? "(native)" : ""));
    }
    logMessage("---------------------");
    HashSet<String> nativeElements = new HashSet<>();
    logMessage("Native methods from getStackTrace");
    for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
        String fullMethodNameFromElement = getFullMethodNameFromElement(e);
        if (e.isNativeMethod() && !fullMethodNameFromElement.equals("java.lang.Thread.getStackTraceImpl") && !isInvokeFrame(e)) {
            logMessage(fullMethodNameFromElement);
            nativeElements.add(fullMethodNameFromElement);
        }
    }
    logMessage("Native methods from StackWalker");
    List<StackFrame> nativeMethodList = StackWalker.getInstance().walk(s -> s.filter(f -> f.isNativeMethod()).collect(Collectors.toList()));
    for (StackFrame f : nativeMethodList) {
        String methodName = getFullMethodNameFromFrame(f);
        logMessage(methodName);
    }
    assertEquals(nativeMethodList.size(), nativeElements.size(), "wrong number of native frames");
    for (StackFrame f : nativeMethodList) {
        String methodName = getFullMethodNameFromFrame(f);
        assertTrue(nativeElements.contains(methodName), methodName + "  missing");
    }
}
Also used : StackFrame(java.lang.StackWalker.StackFrame) HashSet(java.util.HashSet)

Example 4 with StackFrame

use of java.lang.StackWalker.StackFrame in project openj9 by eclipse.

the class StackWalkerTestJava10 method printStack.

public void printStack(String arg) {
    final ClassLoader myClassLoader = this.getClass().getClassLoader();
    List<StackFrame> frameList = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.collect(Collectors.toList()));
    String[] expectedDescriptors = { // $NON-NLS-1$
    "(Ljava/lang/String;)V", // $NON-NLS-1$
    "(Ljava/lang/Integer;Ljava/util/HashMap;)I", // $NON-NLS-1$
    "(Ljava/lang/Integer;)V", // $NON-NLS-1$
    "(I)V", // $NON-NLS-1$
    "(I)V", // $NON-NLS-1$
    "(I)V", // $NON-NLS-1$
    "(I)V", // $NON-NLS-1$
    "()V" };
    Iterator<StackFrame> frameIterator = frameList.iterator();
    for (String s : expectedDescriptors) {
        // $NON-NLS-1$
        assertTrue(frameIterator.hasNext(), "too few frames");
        StackFrame f = frameIterator.next();
        final String actualDescriptor = f.getDescriptor();
        // $NON-NLS-1$
        assertEquals(actualDescriptor, s, "wrong descriptor");
        assertEquals(MethodType.fromMethodDescriptorString(s, myClassLoader), f.getMethodType(), // $NON-NLS-1$
        "wrong descriptor");
    }
}
Also used : StackFrame(java.lang.StackWalker.StackFrame)

Example 5 with StackFrame

use of java.lang.StackWalker.StackFrame in project openj9 by eclipse.

the class StackWalkerTestJava10 method getMyMethodType.

String getMyMethodType(StackWalker walker) {
    Optional<StackFrame> callerFrameOption = walker.walk(s -> s.findFirst());
    // $NON-NLS-1$
    assertTrue(callerFrameOption.isPresent(), "missing stack frame");
    StackFrame callerFrame = callerFrameOption.get();
    assertEquals(callerFrame.getDescriptor(), // $NON-NLS-1$
    "(Ljava/lang/StackWalker;)Ljava/lang/String;", // $NON-NLS-1$
    "wrong descriptor when RETAIN_CLASS_REFERENCE not configured");
    try {
        callerFrame.getMethodType();
        // $NON-NLS-1$
        fail("UnsupportedOperationException not thrown");
    } catch (UnsupportedOperationException e) {
        // $NON-NLS-1$
        logger.info("expected exception thrown", e);
    }
    // $NON-NLS-1$
    return "hello";
}
Also used : StackFrame(java.lang.StackWalker.StackFrame)

Aggregations

StackFrame (java.lang.StackWalker.StackFrame)5 HashSet (java.util.HashSet)2 Module (java.lang.Module)1 Option (java.lang.StackWalker.Option)1 ModuleDescriptor (java.lang.module.ModuleDescriptor)1 Version (java.lang.module.ModuleDescriptor.Version)1 Test (org.testng.annotations.Test)1