KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > ThreadReferenceImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal;
12
13
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.DataInputStream JavaDoc;
16 import java.io.DataOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
26 import org.eclipse.jdi.internal.jdwp.JdwpID;
27 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket;
28 import org.eclipse.jdi.internal.jdwp.JdwpThreadID;
29
30 import com.ibm.icu.text.MessageFormat;
31 import com.sun.jdi.ClassNotLoadedException;
32 import com.sun.jdi.IncompatibleThreadStateException;
33 import com.sun.jdi.InternalException;
34 import com.sun.jdi.InvalidStackFrameException;
35 import com.sun.jdi.InvalidTypeException;
36 import com.sun.jdi.NativeMethodException;
37 import com.sun.jdi.ObjectCollectedException;
38 import com.sun.jdi.ObjectReference;
39 import com.sun.jdi.StackFrame;
40 import com.sun.jdi.ThreadGroupReference;
41 import com.sun.jdi.ThreadReference;
42 import com.sun.jdi.VMCannotBeModifiedException;
43 import com.sun.jdi.VMDisconnectedException;
44 import com.sun.jdi.Value;
45
46
47 /**
48  * This class implements the corresponding interfaces
49  * declared by the JDI specification. See the com.sun.jdi package
50  * for more information.
51  *
52  */

53 public class ThreadReferenceImpl extends ObjectReferenceImpl implements ThreadReference, org.eclipse.jdi.hcr.ThreadReference {
54     /** ThreadStatus Constants. */
55     public static final int JDWP_THREAD_STATUS_ZOMBIE = 0;
56     public static final int JDWP_THREAD_STATUS_RUNNING = 1;
57     public static final int JDWP_THREAD_STATUS_SLEEPING = 2;
58     public static final int JDWP_THREAD_STATUS_MONITOR = 3;
59     public static final int JDWP_THREAD_STATUS_WAIT = 4;
60
61     /** SuspendStatus Constants. */
62     public static final int SUSPEND_STATUS_SUSPENDED = 0x01;
63     
64     /** Mapping of command codes to strings. */
65     private static Map JavaDoc fgThreadStatusMap = null;
66
67     /** Map with Strings for flag bits. */
68     private static String JavaDoc[] fgSuspendStatusStrings = null;
69
70     /** JDWP Tag. */
71     protected static final byte tag = JdwpID.THREAD_TAG;
72
73     /** Is thread currently at a breakpoint? */
74     private boolean fIsAtBreakpoint = false;
75     
76     /**
77      * The cached thread group. A thread's thread group cannot be changed.
78      */

79     private ThreadGroupReferenceImpl fThreadGroup = null;
80
81     /**
82      * Creates new ThreadReferenceImpl.
83      */

84     public ThreadReferenceImpl(VirtualMachineImpl vmImpl, JdwpThreadID threadID) {
85         super("ThreadReference", vmImpl, threadID); //$NON-NLS-1$
86
}
87
88     /**
89      * Sets at breakpoint flag.
90      */

91     public void setIsAtBreakpoint() {
92         fIsAtBreakpoint = true;
93     }
94     
95     /**
96      * Reset flags that can be set when event occurs.
97      */

98     public void resetEventFlags() {
99         fIsAtBreakpoint = false;
100     }
101
102     /**
103      * @returns Value tag.
104      */

105     public byte getTag() {
106         return tag;
107     }
108     
109     /**
110      * @returns Returns an ObjectReference for the monitor, if any, for which this thread is currently waiting.
111      */

112     public ObjectReference currentContendedMonitor() throws IncompatibleThreadStateException {
113         if (!virtualMachine().canGetCurrentContendedMonitor()) {
114             throw new UnsupportedOperationException JavaDoc();
115         }
116         // Note that this information should not be cached.
117
initJdwpRequest();
118         try {
119             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_CURRENT_CONTENDED_MONITOR, this);
120             switch (replyPacket.errorCode()) {
121                 case JdwpReplyPacket.INVALID_THREAD:
122                     throw new ObjectCollectedException();
123                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
124                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_Thread_was_not_suspended_1);
125             }
126             defaultReplyErrorHandler(replyPacket.errorCode());
127             
128             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
129             ObjectReference result = ObjectReferenceImpl.readObjectRefWithTag(this, replyData);
130             return result;
131         } catch (IOException JavaDoc e) {
132             defaultIOExceptionHandler(e);
133             return null;
134         } finally {
135             handledJdwpRequest();
136         }
137     }
138     
139     /**
140      * @see com.sun.jdi.ThreadReference#forceEarlyReturn(com.sun.jdi.Value)
141      * @since 3.3
142      */

143     public void forceEarlyReturn(Value value) throws InvalidTypeException, ClassNotLoadedException, IncompatibleThreadStateException {
144         if(!virtualMachineImpl().canBeModified()) {
145             throw new VMCannotBeModifiedException(JDIMessages.ThreadReferenceImpl_vm_read_only);
146         }
147         initJdwpRequest();
148         ByteArrayOutputStream JavaDoc byteOutStream = new ByteArrayOutputStream JavaDoc();
149         DataOutputStream JavaDoc dataOutStream = new DataOutputStream JavaDoc(byteOutStream);
150         try {
151             write(this, dataOutStream);
152             ((ValueImpl)value).writeWithTag((ValueImpl)value, dataOutStream);
153             JdwpReplyPacket reply = requestVM(JdwpCommandPacket.TR_FORCE_EARLY_RETURN, byteOutStream);
154             switch(reply.errorCode()) {
155                 case JdwpReplyPacket.INVALID_THREAD:
156                     throw new ObjectCollectedException(JDIMessages.ThreadReferenceImpl_thread_object_invalid);
157                 case JdwpReplyPacket.INVALID_OBJECT:
158                     throw new ClassNotLoadedException(JDIMessages.ThreadReferenceImpl_thread_or_value_unknown);
159                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
160                 case JdwpReplyPacket.THREAD_NOT_ALIVE:
161                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_thread_not_suspended);
162                 case JdwpReplyPacket.NOT_IMPLEMENTED:
163                     throw new UnsupportedOperationException JavaDoc(JDIMessages.ThreadReferenceImpl_no_force_early_return_on_threads);
164                 case JdwpReplyPacket.OPAQUE_FRAME:
165                     throw new NativeMethodException(JDIMessages.ThreadReferenceImpl_thread_cannot_force_native_method);
166                 case JdwpReplyPacket.NO_MORE_FRAMES:
167                     throw new InvalidStackFrameException(JDIMessages.ThreadReferenceImpl_thread_no_stackframes);
168                 case JdwpReplyPacket.TYPE_MISMATCH:
169                     throw new InvalidTypeException(JDIMessages.ThreadReferenceImpl_incapatible_return_type);
170                 case JdwpReplyPacket.VM_DEAD:
171                     throw new VMDisconnectedException(JDIMessages.vm_dead);
172             }
173             defaultReplyErrorHandler(reply.errorCode());
174         }
175         catch (IOException JavaDoc e) {
176             defaultIOExceptionHandler(e);
177         }
178         finally {
179             handledJdwpRequest();
180         }
181     }
182     
183     /**
184      * @returns Returns the StackFrame at the given index in the thread's current call stack.
185      */

186     public StackFrame frame(int index) throws IncompatibleThreadStateException {
187         return (StackFrameImpl)frames(index, 1).get(0);
188     }
189     
190     /**
191      * @see com.sun.jdi.ThreadReference#frameCount()
192      */

193     public int frameCount() throws IncompatibleThreadStateException {
194         // Note that this information should not be cached.
195
initJdwpRequest();
196         try {
197             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_FRAME_COUNT, this);
198             switch (replyPacket.errorCode()) {
199                 case JdwpReplyPacket.INVALID_THREAD:
200                     throw new ObjectCollectedException();
201                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
202                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_Thread_was_not_suspended_1);
203             }
204             defaultReplyErrorHandler(replyPacket.errorCode());
205             
206             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
207             int result = readInt("frame count", replyData); //$NON-NLS-1$
208
return result;
209         } catch (IOException JavaDoc e) {
210             defaultIOExceptionHandler(e);
211             return 0;
212         } finally {
213             handledJdwpRequest();
214         }
215     }
216         
217     /**
218      * @returns Returns a List containing each StackFrame in the thread's current call stack.
219      */

220     public List JavaDoc frames() throws IncompatibleThreadStateException {
221         return frames(0, -1);
222     }
223     
224     /**
225      * @returns Returns a List containing each StackFrame in the thread's current call stack.
226      */

227     public List JavaDoc frames(int start, int length) throws IndexOutOfBoundsException JavaDoc, IncompatibleThreadStateException {
228         // Note that this information should not be cached.
229
initJdwpRequest();
230         try {
231             ByteArrayOutputStream JavaDoc outBytes = new ByteArrayOutputStream JavaDoc();
232             DataOutputStream JavaDoc outData = new DataOutputStream JavaDoc(outBytes);
233             write(this, outData);
234             writeInt(start, "start", outData); //$NON-NLS-1$
235
writeInt(length, "length", outData); //$NON-NLS-1$
236

237             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_FRAMES, outBytes);
238             switch (replyPacket.errorCode()) {
239                 case JdwpReplyPacket.INVALID_THREAD:
240                     throw new ObjectCollectedException();
241                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
242                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_Thread_was_not_suspended_1);
243                 case JdwpReplyPacket.INVALID_INDEX:
244                     throw new IndexOutOfBoundsException JavaDoc(JDIMessages.ThreadReferenceImpl_Invalid_index_of_stack_frames_given_4);
245             }
246             defaultReplyErrorHandler(replyPacket.errorCode());
247             
248             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
249             int nrOfElements = readInt("elements", replyData); //$NON-NLS-1$
250
List JavaDoc frames = new ArrayList JavaDoc(nrOfElements);
251             for (int i = 0; i < nrOfElements; i++) {
252                 StackFrameImpl frame = StackFrameImpl.readWithLocation(this, this, replyData);
253                 if (frame == null) {
254                     continue;
255                 }
256                 frames.add(frame);
257             }
258             return frames;
259         } catch (IOException JavaDoc e) {
260             defaultIOExceptionHandler(e);
261             return null;
262         } finally {
263             handledJdwpRequest();
264         }
265     }
266
267     /**
268      * Interrupts this thread
269      * @see com.sun.jdi.ThreadReference#interrupt()
270      */

271     public void interrupt() {
272         // Note that this information should not be cached.
273
initJdwpRequest();
274         try {
275             requestVM(JdwpCommandPacket.TR_INTERRUPT, this);
276         } finally {
277             handledJdwpRequest();
278         }
279     }
280
281     /**
282      * @return Returns whether the thread is suspended at a breakpoint.
283      */

284     public boolean isAtBreakpoint() {
285         return isSuspended() && fIsAtBreakpoint;
286     }
287
288     /**
289      * @return Returns whether the thread has been suspended by the the debugger.
290      */

291     public boolean isSuspended() {
292         // Note that this information should not be cached.
293
initJdwpRequest();
294         try {
295             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_STATUS, this);
296             switch (replyPacket.errorCode()) {
297                 case JdwpReplyPacket.INVALID_THREAD:
298                     throw new ObjectCollectedException();
299             }
300             defaultReplyErrorHandler(replyPacket.errorCode());
301             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
302             //remove the thread status reply
303
readInt("thread status", threadStatusMap(), replyData); //$NON-NLS-1$
304
int suspendStatus = readInt("suspend status", suspendStatusStrings(), replyData); //$NON-NLS-1$
305
boolean result = suspendStatus == SUSPEND_STATUS_SUSPENDED;
306             return result;
307         } catch (IOException JavaDoc e) {
308             defaultIOExceptionHandler(e);
309             return false;
310         } finally {
311             handledJdwpRequest();
312         }
313     }
314
315     /**
316      * @return Returns the name of this thread.
317      */

318     public String JavaDoc name() {
319         initJdwpRequest();
320         try {
321             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_NAME, this);
322             switch (replyPacket.errorCode()) {
323                 case JdwpReplyPacket.INVALID_THREAD:
324                     throw new ObjectCollectedException();
325             }
326             defaultReplyErrorHandler(replyPacket.errorCode());
327             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
328             return readString("name", replyData); //$NON-NLS-1$
329
} catch (IOException JavaDoc e) {
330             defaultIOExceptionHandler(e);
331             return null;
332         } finally {
333             handledJdwpRequest();
334         }
335     }
336     
337     /**
338      * @return Returns a List containing an ObjectReference for each monitor owned by the thread.
339      */

340     public List JavaDoc ownedMonitors() throws IncompatibleThreadStateException {
341         if (!virtualMachine().canGetOwnedMonitorInfo()) {
342             throw new UnsupportedOperationException JavaDoc();
343         }
344         // Note that this information should not be cached.
345
initJdwpRequest();
346         try {
347             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_OWNED_MONITORS, this);
348             switch (replyPacket.errorCode()) {
349                 case JdwpReplyPacket.INVALID_THREAD:
350                     throw new ObjectCollectedException();
351                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
352                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_Thread_was_not_suspended_5);
353             }
354             defaultReplyErrorHandler(replyPacket.errorCode());
355             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
356             
357             int nrOfMonitors = readInt("nr of monitors", replyData); //$NON-NLS-1$
358
List JavaDoc result = new ArrayList JavaDoc(nrOfMonitors);
359             for (int i = 0; i < nrOfMonitors; i++) {
360                 result.add(ObjectReferenceImpl.readObjectRefWithTag(this, replyData));
361             }
362             return result;
363         } catch (IOException JavaDoc e) {
364             defaultIOExceptionHandler(e);
365             return null;
366         } finally {
367             handledJdwpRequest();
368         }
369     }
370
371     /**
372      * @see com.sun.jdi.ThreadReference#ownedMonitorsAndFrames()
373      * @since 3.3
374      */

375     public List JavaDoc ownedMonitorsAndFrames() throws IncompatibleThreadStateException {
376         initJdwpRequest();
377         try {
378             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_OWNED_MONITOR_STACK_DEPTH, this);
379             switch (replyPacket.errorCode()) {
380                 case JdwpReplyPacket.INVALID_THREAD:
381                 case JdwpReplyPacket.INVALID_OBJECT:
382                     throw new ObjectCollectedException(JDIMessages.ThreadReferenceImpl_thread_object_invalid);
383                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
384                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_Thread_was_not_suspended_5);
385                 case JdwpReplyPacket.NOT_IMPLEMENTED:
386                     throw new UnsupportedOperationException JavaDoc(JDIMessages.ThreadReferenceImpl_no_force_early_return_on_threads);
387                 case JdwpReplyPacket.VM_DEAD:
388                     throw new VMDisconnectedException(JDIMessages.vm_dead);
389             }
390             defaultReplyErrorHandler(replyPacket.errorCode());
391             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
392             
393             int owned = readInt("owned monitors", replyData); //$NON-NLS-1$
394
List JavaDoc result = new ArrayList JavaDoc(owned);
395             for (int i = 0; i < owned; i++) {
396                 result.add(new MonitorInfoImpl(this,
397                                               readInt("stack depth", replyData), //$NON-NLS-1$
398
ObjectReferenceImpl.readObjectRefWithTag(this, replyData),
399                                               virtualMachineImpl()));
400             }
401             return result;
402         }
403         catch (IOException JavaDoc e) {
404             defaultIOExceptionHandler(e);
405             return null;
406         }
407         finally {
408             handledJdwpRequest();
409         }
410     }
411     
412     /**
413      * Resumes this thread.
414      * @see com.sun.jdi.ThreadReference#resume()
415      */

416     public void resume() {
417         initJdwpRequest();
418         try {
419             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_RESUME, this);
420             switch (replyPacket.errorCode()) {
421                 case JdwpReplyPacket.INVALID_THREAD:
422                     throw new ObjectCollectedException();
423             }
424             defaultReplyErrorHandler(replyPacket.errorCode());
425             resetEventFlags();
426         } finally {
427             handledJdwpRequest();
428         }
429     }
430     
431     /**
432      * @return Returns the thread's status.
433      */

434     public int status() {
435         // Note that this information should not be cached.
436
initJdwpRequest();
437         try {
438             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_STATUS, this);
439             switch (replyPacket.errorCode()) {
440                 case JdwpReplyPacket.ABSENT_INFORMATION:
441                     return THREAD_STATUS_UNKNOWN;
442                 case JdwpReplyPacket.INVALID_THREAD:
443                     return THREAD_STATUS_NOT_STARTED;
444             }
445             defaultReplyErrorHandler(replyPacket.errorCode());
446             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
447             int threadStatus = readInt("thread status", threadStatusMap(), replyData); //$NON-NLS-1$
448
readInt("suspend status", suspendStatusStrings(), replyData); //$NON-NLS-1$
449
switch (threadStatus) {
450                 case JDWP_THREAD_STATUS_ZOMBIE:
451                     return THREAD_STATUS_ZOMBIE;
452                 case JDWP_THREAD_STATUS_RUNNING:
453                     return THREAD_STATUS_RUNNING;
454                 case JDWP_THREAD_STATUS_SLEEPING:
455                     return THREAD_STATUS_SLEEPING;
456                 case JDWP_THREAD_STATUS_MONITOR:
457                     return THREAD_STATUS_MONITOR;
458                 case JDWP_THREAD_STATUS_WAIT:
459                     return THREAD_STATUS_WAIT;
460                 case -1: // see bug 30816
461
return THREAD_STATUS_UNKNOWN;
462             }
463             throw new InternalException(JDIMessages.ThreadReferenceImpl_Unknown_thread_status_received___6 + threadStatus);
464         } catch (IOException JavaDoc e) {
465             defaultIOExceptionHandler(e);
466             return 0;
467         } finally {
468             handledJdwpRequest();
469         }
470     }
471     
472     /**
473      * Stops this thread with an asynchronous exception.
474      * @see com.sun.jdi.ThreadReference#stop(com.sun.jdi.ObjectReference)
475      */

476     public void stop(ObjectReference throwable) throws InvalidTypeException {
477         checkVM(throwable);
478         ObjectReferenceImpl throwableImpl = (ObjectReferenceImpl) throwable;
479         
480         initJdwpRequest();
481         try {
482             ByteArrayOutputStream JavaDoc outBytes = new ByteArrayOutputStream JavaDoc();
483             DataOutputStream JavaDoc outData = new DataOutputStream JavaDoc(outBytes);
484             write(this, outData);
485             throwableImpl.write(this, outData);
486     
487             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_STOP, outBytes);
488             switch (replyPacket.errorCode()) {
489                 case JdwpReplyPacket.INVALID_THREAD:
490                     throw new ObjectCollectedException();
491                 case JdwpReplyPacket.INVALID_CLASS:
492                     throw new InvalidTypeException (JDIMessages.ThreadReferenceImpl_Stop_argument_not_an_instance_of_java_lang_Throwable_in_the_target_VM_7);
493             }
494             defaultReplyErrorHandler(replyPacket.errorCode());
495         } catch (IOException JavaDoc e) {
496             defaultIOExceptionHandler(e);
497         } finally {
498             handledJdwpRequest();
499         }
500     }
501         
502     /**
503      * Suspends this thread.
504      * @see com.sun.jdi.ThreadReference#suspend()
505      */

506     public void suspend() {
507         initJdwpRequest();
508         try {
509             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_SUSPEND, this);
510             switch (replyPacket.errorCode()) {
511                 case JdwpReplyPacket.INVALID_THREAD:
512                     throw new ObjectCollectedException();
513             }
514             defaultReplyErrorHandler(replyPacket.errorCode());
515         } finally {
516             handledJdwpRequest();
517         }
518     }
519
520     /**
521      * @return Returns the number of pending suspends for this thread.
522      */

523     public int suspendCount() {
524         // Note that this information should not be cached.
525
initJdwpRequest();
526         try {
527             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_SUSPEND_COUNT, this);
528             defaultReplyErrorHandler(replyPacket.errorCode());
529             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
530             int result = readInt("suspend count", replyData); //$NON-NLS-1$
531
return result;
532         } catch (IOException JavaDoc e) {
533             defaultIOExceptionHandler(e);
534             return 0;
535         } finally {
536             handledJdwpRequest();
537         }
538     }
539     
540     /**
541      * @return Returns this thread's thread group.
542      */

543     public ThreadGroupReference threadGroup() {
544         if (fThreadGroup != null) {
545             return fThreadGroup;
546         }
547         initJdwpRequest();
548         try {
549             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TR_THREAD_GROUP, this);
550             switch (replyPacket.errorCode()) {
551                 case JdwpReplyPacket.INVALID_THREAD:
552                     throw new ObjectCollectedException();
553             }
554             defaultReplyErrorHandler(replyPacket.errorCode());
555             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
556             fThreadGroup= ThreadGroupReferenceImpl.read(this, replyData);
557             return fThreadGroup;
558         } catch (IOException JavaDoc e) {
559             defaultIOExceptionHandler(e);
560             return null;
561         } finally {
562             handledJdwpRequest();
563         }
564     }
565     
566     /**
567      * Simulate the execution of a return instruction instead of executing the next bytecode in a method.
568      * @return Returns whether any finally or synchronized blocks are enclosing the current instruction.
569      */

570     public boolean doReturn(Value returnValue, boolean triggerFinallyAndSynchronized) throws org.eclipse.jdi.hcr.OperationRefusedException {
571         virtualMachineImpl().checkHCRSupported();
572         ValueImpl valueImpl;
573         if (returnValue != null) { // null is used if no value is returned.
574
checkVM(returnValue);
575             valueImpl = (ValueImpl)returnValue;
576         } else {
577             try {
578                 TypeImpl returnType = (TypeImpl)frame(0).location().method().returnType();
579                 valueImpl = (ValueImpl)returnType.createNullValue();
580             } catch (IncompatibleThreadStateException e) {
581                 throw new org.eclipse.jdi.hcr.OperationRefusedException(e.toString());
582             } catch (ClassNotLoadedException e) {
583                 throw new org.eclipse.jdi.hcr.OperationRefusedException(e.toString());
584             }
585         }
586
587         // Note that this information should not be cached.
588
initJdwpRequest();
589         try {
590             ByteArrayOutputStream JavaDoc outBytes = new ByteArrayOutputStream JavaDoc();
591             DataOutputStream JavaDoc outData = new DataOutputStream JavaDoc(outBytes);
592             write(this, outData);
593             valueImpl.writeWithTag(this, outData);
594             writeBoolean(triggerFinallyAndSynchronized, "trigger finaly+sync", outData); //$NON-NLS-1$
595

596             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.HCR_DO_RETURN, outBytes);
597             switch (replyPacket.errorCode()) {
598                 case JdwpReplyPacket.INVALID_THREAD:
599                     throw new ObjectCollectedException();
600             }
601             defaultReplyErrorHandler(replyPacket.errorCode());
602             
603             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
604             boolean result = readBoolean("is enclosed", replyData); //$NON-NLS-1$
605
return result;
606         } catch (IOException JavaDoc e) {
607             defaultIOExceptionHandler(e);
608             return false;
609         } finally {
610             handledJdwpRequest();
611         }
612     }
613     
614     /**
615      * @return Returns description of Mirror object.
616      */

617     public String JavaDoc toString() {
618         try {
619             return MessageFormat.format(JDIMessages.ThreadReferenceImpl_8, new String JavaDoc[]{type().toString(), name(), getObjectID().toString()});
620         } catch (ObjectCollectedException e) {
621             return JDIMessages.ThreadReferenceImpl__Garbage_Collected__ThreadReference__9 + idString();
622         } catch (Exception JavaDoc e) {
623             return fDescription;
624         }
625     }
626
627     /**
628      * @return Reads JDWP representation and returns new instance.
629      */

630     public static ThreadReferenceImpl read(MirrorImpl target, DataInputStream JavaDoc in) throws IOException JavaDoc {
631         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
632         JdwpThreadID ID = new JdwpThreadID(vmImpl);
633         ID.read(in);
634         if (target.fVerboseWriter != null)
635             target.fVerboseWriter.println("threadReference", ID.value()); //$NON-NLS-1$
636

637         if (ID.isNull())
638             return null;
639             
640         ThreadReferenceImpl mirror = (ThreadReferenceImpl)vmImpl.getCachedMirror(ID);
641         if (mirror == null) {
642             mirror = new ThreadReferenceImpl(vmImpl, ID);
643             vmImpl.addCachedMirror(mirror);
644         }
645         return mirror;
646      }
647
648     /**
649      * Retrieves constant mappings.
650      */

651     public static void getConstantMaps() {
652         if (fgThreadStatusMap != null) {
653             return;
654         }
655         
656         Field JavaDoc[] fields = ThreadReferenceImpl.class.getDeclaredFields();
657         fgThreadStatusMap = new HashMap JavaDoc();
658         fgSuspendStatusStrings = new String JavaDoc[32]; // Int
659

660         for (int i = 0; i < fields.length; i++) {
661             Field JavaDoc field = fields[i];
662             if ((field.getModifiers() & Modifier.PUBLIC) == 0 || (field.getModifiers() & Modifier.STATIC) == 0 || (field.getModifiers() & Modifier.FINAL) == 0)
663                 continue;
664                 
665             try {
666                 String JavaDoc name = field.getName();
667                 int value = field.getInt(null);
668                 Integer JavaDoc intValue = new Integer JavaDoc(value);
669
670                 if (name.startsWith("JDWP_THREAD_STATUS_")) { //$NON-NLS-1$
671
name = name.substring(19);
672                     fgThreadStatusMap.put(intValue, name);
673                 } else if (name.startsWith("SUSPEND_STATUS_")) { //$NON-NLS-1$
674
name = name.substring(15);
675                     for (int j = 0; j < fgSuspendStatusStrings.length; j++) {
676                         if ((1 << j & value) != 0) {
677                             fgSuspendStatusStrings[j]= name;
678                             break;
679                         }
680                     }
681                 }
682             } catch (IllegalAccessException JavaDoc e) {
683                 // Will not occur for own class.
684
} catch (IllegalArgumentException JavaDoc e) {
685                 // Should not occur.
686
// We should take care that all public static final constants
687
// in this class are numbers that are convertible to int.
688
}
689         }
690     }
691     
692     /**
693      * @return Returns a map with string representations of tags.
694      */

695      public static Map JavaDoc threadStatusMap() {
696         getConstantMaps();
697         return fgThreadStatusMap;
698      }
699
700     /**
701      * @return Returns a map with string representations of tags.
702      */

703      public static String JavaDoc[] suspendStatusStrings() {
704         getConstantMaps();
705         return fgSuspendStatusStrings;
706      }
707
708     /**
709      * @see ThreadReference#popFrames(StackFrame)
710      */

711     public void popFrames(StackFrame frameToPop) throws IncompatibleThreadStateException {
712         if (!isSuspended()) {
713             throw new IncompatibleThreadStateException();
714         }
715         if (!virtualMachineImpl().canPopFrames()) {
716             throw new UnsupportedOperationException JavaDoc();
717         }
718         
719         StackFrameImpl frame = (StackFrameImpl) frameToPop;
720         
721         initJdwpRequest();
722         try {
723             ByteArrayOutputStream JavaDoc outBytes = new ByteArrayOutputStream JavaDoc();
724             DataOutputStream JavaDoc outData = new DataOutputStream JavaDoc(outBytes);
725             frame.writeWithThread(frame, outData);
726             
727             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.SF_POP_FRAME, outBytes);
728             switch (replyPacket.errorCode()) {
729                 case JdwpReplyPacket.INVALID_THREAD:
730                     throw new InvalidStackFrameException();
731                 case JdwpReplyPacket.INVALID_FRAMEID:
732                     throw new InvalidStackFrameException(JDIMessages.ThreadReferenceImpl_Unable_to_pop_the_requested_stack_frame_from_the_call_stack__Reasons_include__The_frame_id_was_invalid__The_thread_was_resumed__10);
733                 case JdwpReplyPacket.THREAD_NOT_SUSPENDED:
734                     throw new IncompatibleThreadStateException(JDIMessages.ThreadReferenceImpl_Unable_to_pop_the_requested_stack_frame__The_requested_stack_frame_is_not_suspended_11);
735                 case JdwpReplyPacket.NO_MORE_FRAMES:
736                     throw new InvalidStackFrameException(JDIMessages.ThreadReferenceImpl_Unable_to_pop_the_requested_stack_frame_from_the_call_stack__Reasons_include__The_requested_frame_was_the_last_frame_on_the_call_stack__The_requested_frame_was_the_last_frame_above_a_native_frame__12);
737                 default:
738                     defaultReplyErrorHandler(replyPacket.errorCode());
739             }
740         } catch (IOException JavaDoc ioe) {
741             defaultIOExceptionHandler(ioe);
742         } finally {
743             handledJdwpRequest();
744         }
745     }
746
747 }
748
Popular Tags