Java Code Examples for java.lang.StackWalker.StackFrame#getDeclaringClass()

The following examples show how to use java.lang.StackWalker.StackFrame#getDeclaringClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBCI.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void verify(StackFrame frame) {
    if (frame.getDeclaringClass() != clazz)
        return;

    int bci = frame.getByteCodeIndex();
    int lineNumber = frame.getLineNumber();
    System.out.format("%s.%s bci %d (%s:%d)%n",
                      frame.getClassName(), frame.getMethodName(), bci,
                      frame.getFileName(), lineNumber);

    MethodInfo method = methods.get(frame.getMethodName());
    SortedSet<Integer> values = method.findLineNumbers(bci).get();
    if (!values.contains(lineNumber)) {
        throw new RuntimeException("line number for bci: " + bci + " "
            + lineNumber + " not matched line number table: " + values);
    }
}
 
Example 2
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void checkFrame(String loaderName, StackFrame frame,
                              StackTraceElement ste) {
    System.err.println("checking " + ste.toString() + " expected: " + frame.toString());
    Class<?> c = frame.getDeclaringClass();
    Module module = c.getModule();
    assertEquals(ste.getModuleName(), module.getName(), "module name");
    assertEquals(ste.getClassLoaderName(), loaderName, "class loader name");
    assertEquals(ste.getClassLoaderName(), c.getClassLoader().getName(),
                 "class loader name");
    assertEquals(ste.getClassName(), c.getName(), "class name");
    assertEquals(ste.getMethodName(), frame.getMethodName(), "method name");
    assertEquals(ste.getFileName(), frame.getFileName(), "file name");

}
 
Example 3
Source File: MultiThreadStackWalk.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void consume(StackFrame sfi) {
    if (frameCounter.get() == 0 && isStreamPipeline(sfi.getDeclaringClass())) {
        return;
    }

    final long count = frameCounter.getAndIncrement();
    final StringBuilder builder = new StringBuilder();
    builder.append("Declaring class[")
           .append(count)
           .append("]: ")
           .append(sfi.getDeclaringClass());
    builder.append('\n');
    builder.append("\t")
           .append(sfi.getClassName())
           .append(".")
           .append(sfi.toStackTraceElement().getMethodName())
           .append(sfi.toStackTraceElement().isNativeMethod()
                   ? "(native)"
                   : "(" + sfi.toStackTraceElement().getFileName()
                     +":"+sfi.toStackTraceElement().getLineNumber()+")");
    builder.append('\n');
    if (debug.get()) {
        System.out.print("[debug] " + builder.toString());
        builder.setLength(0);
    }
    if (count == max) {
        maxReached.incrementAndGet();
    }
    if (count  == checkMarkAt) {
        if (sfi.getDeclaringClass() != MultiThreadStackWalk.Marker.class) {
            throw new RuntimeException("Expected Marker at " + count
                    + ", found " + sfi.getDeclaringClass());
        }
    } else {
        if (count <= 0 && sfi.getDeclaringClass() != MultiThreadStackWalk.Call.class) {
            throw new RuntimeException("Expected Call at " + count
                    + ", found " + sfi.getDeclaringClass());
        } else if (count > 0 && count < max && sfi.getDeclaringClass() != MultiThreadStackWalk.Test.class) {
            throw new RuntimeException("Expected Test at " + count
                    + ", found " + sfi.getDeclaringClass());
        } else if (count == max && sfi.getDeclaringClass() != MultiThreadStackWalk.class) {
            throw new RuntimeException("Expected MultiThreadStackWalk at "
                    + count + ", found " + sfi.getDeclaringClass());
        } else if (count == max &&  !sfi.toStackTraceElement().getMethodName().equals("runTest")) {
            throw new RuntimeException("Expected runTest method at "
                    + count + ", found " + sfi.toStackTraceElement().getMethodName());
        } else if (count == max+1) {
            if (sfi.getDeclaringClass() != MultiThreadStackWalk.WalkThread.class) {
                throw new RuntimeException("Expected MultiThreadStackWalk at "
                    + count + ", found " + sfi.getDeclaringClass());
            }
            if (count == max && !sfi.toStackTraceElement().getMethodName().equals("run")) {
                throw new RuntimeException("Expected main method at "
                    + count + ", found " + sfi.toStackTraceElement().getMethodName());
            }
        } else if (count > max+1) {
            // expect JTreg infrastructure...
            if (!infrastructureClasses.contains(sfi.getDeclaringClass().getName())) {
                System.err.println("**** WARNING: encountered unexpected infrastructure class at "
                        + count +": " + sfi.getDeclaringClass().getName());
                unexpected.add(sfi.getDeclaringClass().getName());
            }
        }
    }
    if (count == 100) {
        // Maybe we should had some kind of checking inside that lambda
        // too. For the moment we should be satisfied if it doesn't throw
        // any exception and doesn't make the outer walk fail...
        StackWalker.getInstance(RETAIN_CLASS_REFERENCE).forEach(x -> {
            StackTraceElement st = x.toStackTraceElement();
            StringBuilder b = new StringBuilder();
            b.append("*** inner walk: ")
                    .append(x.getClassName())
                    .append(st == null ? "- no stack trace element -" :
                            ("." + st.getMethodName()
                                    + (st.isNativeMethod() ? "(native)" :
                                    "(" + st.getFileName()
                                            + ":" + st.getLineNumber() + ")")))
                    .append('\n');
            if (debug.get()) {
                System.out.print(b.toString());
                b.setLength(0);
            }
        });
    }
}
 
Example 4
Source File: StackRecorderUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compare the given StackFrame returned from the StackWalker to the
 * recorded frame at the given index.
 *
 * Tests for equality, as well as functional correctness with respect to
 * the StackWalker's options (e.g. throws or doesn't throw exceptions)
 */
public void compareFrame(int index, StackFrame sf) {
    TestFrame tf = testFrames.get(index);
    if (compareClasses) {
        if (!tf.declaringClass.equals(sf.getDeclaringClass())) {
            throw new RuntimeException("Expected class: " +
              tf.declaringClass.toString() + ", but got: " +
              sf.getDeclaringClass().toString());
        }
    } else {
        boolean caught = false;
        try {
            sf.getDeclaringClass();
        } catch (UnsupportedOperationException e) {
            caught = true;
        }
        if (!caught) {
            throw new RuntimeException("StackWalker did not have " +
              "RETAIN_CLASS_REFERENCE Option, but did not throw " +
              "UnsupportedOperationException");
        }
    }

    if (compareClassNames && !tf.className().equals(sf.getClassName())) {
        throw new RuntimeException("Expected class name: " + tf.className() +
                ", but got: " + sf.getClassName());
    }
    if (compareMethodNames && !tf.methodName.equals(sf.getMethodName())) {
        throw new RuntimeException("Expected method name: " + tf.methodName +
                ", but got: " + sf.getMethodName());
    }
    if (compareSTEs) {
        StackTraceElement ste = sf.toStackTraceElement();
        if (!(ste.getClassName().equals(tf.className()) &&
              ste.getMethodName().equals(tf.methodName)) &&
              ste.getFileName().equals(tf.fileName)) {
            throw new RuntimeException("Expected StackTraceElement info: " +
                    tf + ", but got: " + ste);
        }
        if (!Objects.equals(ste.getClassName(), sf.getClassName())
            || !Objects.equals(ste.getMethodName(), sf.getMethodName())
            || !Objects.equals(ste.getFileName(), sf.getFileName())
            || !Objects.equals(ste.getLineNumber(), sf.getLineNumber())
            || !Objects.equals(ste.isNativeMethod(), sf.isNativeMethod())) {
            throw new RuntimeException("StackFrame and StackTraceElement differ: " +
                    "sf=" + sf + ", ste=" + ste);
        }
    }
}
 
Example 5
Source File: ReflectionFrames.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public boolean takeWhile(StackFrame f) {
    if (stop) return false;
    if (verbose) System.out.println("    " + f);
    stop = stop || f.getDeclaringClass() == ReflectionFrames.class;
    return true;
}