package jetbrains.mps.debugger.java.api.state.proxy; /*Generated by MPS */ import jetbrains.mps.debug.api.programState.IThread; import org.apache.log4j.Logger; import org.apache.log4j.LogManager; import org.jetbrains.annotations.NotNull; import com.sun.jdi.ThreadReference; import org.jetbrains.annotations.Nullable; import javax.swing.Icon; import java.util.List; import jetbrains.mps.debug.api.programState.IStackFrame; import jetbrains.mps.internal.collections.runtime.ListSequence; import java.util.ArrayList; import jetbrains.mps.debug.api.AbstractDebugSession; import com.sun.jdi.IncompatibleThreadStateException; import com.sun.jdi.AbsentInformationException; import jetbrains.mps.ide.ThreadUtils; import jetbrains.mps.debugger.java.api.ui.Icons; public class JavaThread extends ProxyForJava implements IThread { private static final Logger LOG = LogManager.getLogger(JavaThread.class); @NotNull private final ThreadReference myThreadReference; @Nullable private final Icon myCachedIcon; private final List<IStackFrame> myStackFrames = ListSequence.fromList(new ArrayList<IStackFrame>()); private boolean myInitialized = false; private final String myPresentation; private final AbstractDebugSession<?> myDebugSession; public JavaThread(@NotNull AbstractDebugSession<?> debugSession, @NotNull ThreadReference threadReference) { super(threadReference); myThreadReference = threadReference; myPresentation = calculatePresentation(); myCachedIcon = calculateIcon(); myDebugSession = debugSession; } public synchronized void initializeFrames() { if (myInitialized) { return; } myInitialized = true; try { for (int i = 0; i < myThreadReference.frameCount(); i++) { ListSequence.fromList(myStackFrames).addElement(new JavaStackFrame(this, i)); } } catch (IncompatibleThreadStateException ex) { if (LOG.isDebugEnabled()) { LOG.debug("IncompatibleThreadStateException", ex); } } catch (AbsentInformationException e) { if (LOG.isDebugEnabled()) { LOG.debug("AbsecntInformationException", e); } } } @Override public synchronized List<IStackFrame> getFrames() { return myStackFrames; } @Override public synchronized int getFramesCount() { return ListSequence.fromList(myStackFrames).count(); } @Nullable public synchronized IStackFrame getFrame(int index) { return ListSequence.fromList(myStackFrames).getElement(index); } @NotNull public ThreadReference getThread() { return myThreadReference; } @NotNull public AbstractDebugSession<?> getDebugSession() { return myDebugSession; } @Override public String getName() { return myThreadReference.name(); } @Override public String getPresentation() { return myPresentation; } private String calculatePresentation() { assert !(ThreadUtils.isInEDT()); return myThreadReference.name() + " (" + myThreadReference.referenceType().name() + " from group " + myThreadReference.threadGroup().name() + ") : " + JavaThread.getThreadStatusText(myThreadReference.status()); } @Override public Icon getPresentationIcon() { return myCachedIcon; } private Icon calculateIcon() { assert !(ThreadUtils.isInEDT()); if (myThreadReference.isAtBreakpoint()) { return Icons.THREAD_AT_BREAKPOINT; } else if (myThreadReference.isSuspended()) { return Icons.THREAD_SUSPENDED; } else { return Icons.THREAD_RUNNING; } } private static String getThreadStatusText(int statusId) { switch (statusId) { case ThreadReference.THREAD_STATUS_MONITOR: return "MONITOR"; case ThreadReference.THREAD_STATUS_NOT_STARTED: return "NOT_STARTED"; case ThreadReference.THREAD_STATUS_RUNNING: return "RUNNING"; case ThreadReference.THREAD_STATUS_SLEEPING: return "SLEEPING"; case ThreadReference.THREAD_STATUS_UNKNOWN: return "UNKNOWN"; case ThreadReference.THREAD_STATUS_WAIT: return "WAIT"; case ThreadReference.THREAD_STATUS_ZOMBIE: return "ZOMBIE"; default: return "UNDEFINED"; } } }