com.sun.jdi.event.MethodEntryEvent Java Examples

The following examples show how to use com.sun.jdi.event.MethodEntryEvent. 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: DebugUtility.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Suspend the main thread when the program enters the main method of the specified main class.
 * @param debugSession
 *                  the debug session.
 * @param mainClass
 *                  the fully qualified name of the main class.
 * @return
 *        a {@link CompletableFuture} that contains the suspended main thread id.
 */
public static CompletableFuture<Long> stopOnEntry(IDebugSession debugSession, String mainClass) {
    CompletableFuture<Long> future = new CompletableFuture<>();

    EventRequestManager manager = debugSession.getVM().eventRequestManager();
    MethodEntryRequest request = manager.createMethodEntryRequest();
    request.addClassFilter(mainClass);
    request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);

    debugSession.getEventHub().events().filter(debugEvent -> {
        return debugEvent.event instanceof MethodEntryEvent && request.equals(debugEvent.event.request());
    }).subscribe(debugEvent -> {
        Method method = ((MethodEntryEvent) debugEvent.event).method();
        if (method.isPublic() && method.isStatic() && method.name().equals("main")
                && method.signature().equals("([Ljava/lang/String;)V")) {
            deleteEventRequestSafely(debugSession.getVM().eventRequestManager(), request);
            debugEvent.shouldResume = false;
            ThreadReference bpThread = ((MethodEntryEvent) debugEvent.event).thread();
            future.complete(bpThread.uniqueID());
        }
    });
    request.enable();

    return future;
}