001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Anton V. Karnachuk
021: * @version $Revision: 1.3 $
022: */
023:
024: /**
025: * Created on 17.03.2005
026: */package org.apache.harmony.jpda.tests.framework.jdwp;
027:
028: import org.apache.harmony.jpda.tests.framework.TestErrorException;
029:
030: /**
031: * This class represent parsed EventPacket with received event set data.
032: */
033: public class ParsedEvent {
034:
035: private byte suspendPolicy;
036:
037: private int requestID;
038:
039: private byte eventKind;
040:
041: /**
042: * Create new instance with specified data.
043: */
044: protected ParsedEvent(byte suspendPolicy, Packet packet,
045: byte eventKind) {
046: this .suspendPolicy = suspendPolicy;
047: this .requestID = packet.getNextValueAsInt();
048: this .eventKind = eventKind;
049: }
050:
051: /**
052: * Returns RequestID of this event set.
053: *
054: * @return RequestID of this event set
055: */
056: public int getRequestID() {
057: return requestID;
058: }
059:
060: /**
061: * Returns suspend policy of this event set.
062: *
063: * @return suspend policy of this event set
064: */
065: public byte getSuspendPolicy() {
066: return suspendPolicy;
067: }
068:
069: /**
070: * @return Returns the eventKind.
071: */
072: public byte getEventKind() {
073: return eventKind;
074: }
075:
076: /**
077: * The class extends ParsedEvent by associating it with a thread.
078: */
079: public static class EventThread extends ParsedEvent {
080:
081: private long threadID;
082:
083: /**
084: * A constructor.
085: *
086: * @param suspendPolicy
087: * @param packet
088: */
089: protected EventThread(byte suspendPolicy, Packet packet,
090: byte eventKind) {
091: super (suspendPolicy, packet, eventKind);
092: this .threadID = packet.getNextValueAsThreadID();
093: }
094:
095: /**
096: * @return Returns the thread id.
097: */
098: public long getThreadID() {
099: return threadID;
100: }
101: }
102:
103: /**
104: * The class extends EventThread by associating it with a location.
105: */
106: private static class EventThreadLocation extends EventThread {
107:
108: private Location location;
109:
110: /**
111: * A constructor.
112: *
113: * @param suspendPolicy
114: * @param packet
115: */
116: protected EventThreadLocation(byte suspendPolicy,
117: Packet packet, byte eventKind) {
118: super (suspendPolicy, packet, eventKind);
119: this .location = packet.getNextValueAsLocation();
120: }
121:
122: /**
123: * @return Returns the location.
124: */
125: public Location getLocation() {
126: return location;
127: }
128: }
129:
130: /**
131: * The class implements JDWP VM_START event.
132: */
133: public static final class Event_VM_START extends EventThread {
134:
135: /**
136: * A constructor.
137: *
138: * @param suspendPolicy
139: * @param packet
140: */
141: private Event_VM_START(byte suspendPolicy, Packet packet) {
142: super (suspendPolicy, packet,
143: JDWPConstants.EventKind.VM_START);
144: }
145: };
146:
147: /**
148: * The class implements JDWP SINGLE_STEP event.
149: */
150: public static final class Event_SINGLE_STEP extends
151: EventThreadLocation {
152:
153: /**
154: * A constructor.
155: *
156: * @param suspendPolicy
157: * @param packet
158: */
159: private Event_SINGLE_STEP(byte suspendPolicy, Packet packet) {
160: super (suspendPolicy, packet,
161: JDWPConstants.EventKind.SINGLE_STEP);
162: }
163: }
164:
165: /**
166: * The class implements JDWP BREAKPOINT event.
167: */
168: public static final class Event_BREAKPOINT extends
169: EventThreadLocation {
170:
171: /**
172: * A constructor.
173: *
174: * @param suspendPolicy
175: * @param packet
176: */
177: private Event_BREAKPOINT(byte suspendPolicy, Packet packet) {
178: super (suspendPolicy, packet,
179: JDWPConstants.EventKind.BREAKPOINT);
180: }
181: }
182:
183: /**
184: * The class implements JDWP METHOD_ENTRY event.
185: */
186: public static final class Event_METHOD_ENTRY extends
187: EventThreadLocation {
188:
189: /**
190: * A constructor.
191: *
192: * @param suspendPolicy
193: * @param packet
194: */
195: private Event_METHOD_ENTRY(byte suspendPolicy, Packet packet) {
196: super (suspendPolicy, packet,
197: JDWPConstants.EventKind.METHOD_ENTRY);
198: }
199: }
200:
201: /**
202: * The class implements JDWP METHOD_EXIT event.
203: */
204: public static final class Event_METHOD_EXIT extends
205: EventThreadLocation {
206:
207: /**
208: * A constructor.
209: *
210: * @param suspendPolicy
211: * @param packet
212: */
213: private Event_METHOD_EXIT(byte suspendPolicy, Packet packet) {
214: super (suspendPolicy, packet,
215: JDWPConstants.EventKind.METHOD_EXIT);
216: }
217: }
218:
219: /**
220: * The class implements JDWP EXCEPTION event.
221: */
222: public static final class Event_EXCEPTION extends
223: EventThreadLocation {
224:
225: private TaggedObject exception;
226:
227: private Location catchLocation;
228:
229: /**
230: * A constructor.
231: *
232: * @param suspendPolicy
233: * @param packet
234: */
235: private Event_EXCEPTION(byte suspendPolicy, Packet packet) {
236: super (suspendPolicy, packet,
237: JDWPConstants.EventKind.EXCEPTION);
238: exception = packet.getNextValueAsTaggedObject();
239: catchLocation = packet.getNextValueAsLocation();
240: }
241:
242: /**
243: * @return Returns the location of the caught exception.
244: */
245: public Location getCatchLocation() {
246: return catchLocation;
247: }
248:
249: /**
250: * @return Returns the exception.
251: */
252: public TaggedObject getException() {
253: return exception;
254: }
255: }
256:
257: /**
258: * The class implements JDWP THREAD_START event.
259: */
260: public static final class Event_THREAD_START extends EventThread {
261:
262: /**
263: * A constructor.
264: *
265: * @param suspendPolicy
266: * @param packet
267: */
268: private Event_THREAD_START(byte suspendPolicy, Packet packet) {
269: super (suspendPolicy, packet,
270: JDWPConstants.EventKind.THREAD_START);
271: }
272: };
273:
274: /**
275: * The class implements JDWP THREAD_DEATH event.
276: */
277: public static final class Event_THREAD_DEATH extends EventThread {
278:
279: /**
280: * A constructor.
281: *
282: * @param suspendPolicy
283: * @param packet
284: */
285: private Event_THREAD_DEATH(byte suspendPolicy, Packet packet) {
286: super (suspendPolicy, packet,
287: JDWPConstants.EventKind.THREAD_DEATH);
288: }
289: };
290:
291: /**
292: * The class implements JDWP CLASS_PREPARE event.
293: */
294: public static final class Event_CLASS_PREPARE extends EventThread {
295:
296: private byte refTypeTag;
297:
298: private long typeID;
299:
300: private String signature;
301:
302: private int status;
303:
304: /**
305: * A constructor.
306: *
307: * @param suspendPolicy
308: * @param packet
309: */
310: protected Event_CLASS_PREPARE(byte suspendPolicy, Packet packet) {
311: super (suspendPolicy, packet,
312: JDWPConstants.EventKind.CLASS_PREPARE);
313: refTypeTag = packet.getNextValueAsByte();
314: typeID = packet.getNextValueAsReferenceTypeID();
315: signature = packet.getNextValueAsString();
316: status = packet.getNextValueAsInt();
317: }
318:
319: /**
320: * @return Returns the refTypeTag.
321: */
322: public byte getRefTypeTag() {
323: return refTypeTag;
324: }
325:
326: /**
327: * @return Returns the signature.
328: */
329: public String getSignature() {
330: return signature;
331: }
332:
333: /**
334: * @return Returns the status.
335: */
336: public int getStatus() {
337: return status;
338: }
339:
340: /**
341: * @return Returns the typeID.
342: */
343: public long getTypeID() {
344: return typeID;
345: }
346: };
347:
348: /**
349: * The class implements JDWP CLASS_UNLOAD event.
350: */
351: public static final class Event_CLASS_UNLOAD extends ParsedEvent {
352:
353: private String signature;
354:
355: /**
356: * A constructor.
357: *
358: * @param suspendPolicy
359: * @param packet
360: */
361: private Event_CLASS_UNLOAD(byte suspendPolicy, Packet packet) {
362: super (suspendPolicy, packet,
363: JDWPConstants.EventKind.CLASS_UNLOAD);
364: signature = packet.getNextValueAsString();
365: }
366:
367: /**
368: * @return Returns the signature.
369: */
370: public String getSignature() {
371: return signature;
372: }
373: };
374:
375: /**
376: * The class implements JDWP FIELD_ACCESS event.
377: */
378: public static final class Event_FIELD_ACCESS extends
379: EventThreadLocation {
380:
381: private byte refTypeTag;
382:
383: private long typeID;
384:
385: private long fieldID;
386:
387: private TaggedObject object;
388:
389: /**
390: * A constructor.
391: *
392: * @param suspendPolicy
393: * @param packet
394: */
395: private Event_FIELD_ACCESS(byte suspendPolicy, Packet packet) {
396: super (suspendPolicy, packet,
397: JDWPConstants.EventKind.FIELD_ACCESS);
398: refTypeTag = packet.getNextValueAsByte();
399: typeID = packet.getNextValueAsReferenceTypeID();
400: fieldID = packet.getNextValueAsFieldID();
401: object = packet.getNextValueAsTaggedObject();
402: }
403:
404: /**
405: * @return Returns the fieldID.
406: */
407: public long getFieldID() {
408: return fieldID;
409: }
410:
411: /**
412: * @return Returns the object.
413: */
414: public TaggedObject getObject() {
415: return object;
416: }
417:
418: /**
419: * @return Returns the refTypeTag.
420: */
421: public byte getRefTypeTag() {
422: return refTypeTag;
423: }
424:
425: /**
426: * @return Returns the typeID.
427: */
428: public long getTypeID() {
429: return typeID;
430: }
431: };
432:
433: /**
434: * The class implements JDWP FIELD_MODIFICATION event.
435: */
436: public static final class Event_FIELD_MODIFICATION extends
437: EventThreadLocation {
438: private byte refTypeTag;
439:
440: private long typeID;
441:
442: private long fieldID;
443:
444: private TaggedObject object;
445:
446: private Value valueToBe;
447:
448: /**
449: * A constructor.
450: * @param suspendPolicy
451: * @param packet
452: */
453: private Event_FIELD_MODIFICATION(byte suspendPolicy,
454: Packet packet) {
455: super (suspendPolicy, packet,
456: JDWPConstants.EventKind.FIELD_MODIFICATION);
457: refTypeTag = packet.getNextValueAsByte();
458: typeID = packet.getNextValueAsReferenceTypeID();
459: fieldID = packet.getNextValueAsFieldID();
460: object = packet.getNextValueAsTaggedObject();
461: valueToBe = packet.getNextValueAsValue();
462: }
463:
464: /**
465: * @return Returns the fieldID.
466: */
467: public long getFieldID() {
468: return fieldID;
469: }
470:
471: /**
472: * @return Returns the object.
473: */
474: public TaggedObject getObject() {
475: return object;
476: }
477:
478: /**
479: * @return Returns the refTypeTag.
480: */
481: public byte getRefTypeTag() {
482: return refTypeTag;
483: }
484:
485: /**
486: * @return Returns the typeID.
487: */
488: public long getTypeID() {
489: return typeID;
490: }
491:
492: /**
493: * @return Returns the valueToBe.
494: */
495: public Value getValueToBe() {
496: return valueToBe;
497: }
498: };
499:
500: /**
501: * The class implements JDWP VM_DEATH event.
502: */
503: public static final class Event_VM_DEATH extends ParsedEvent {
504: /**
505: * A constructor.
506: * @param suspendPolicy
507: * @param packet
508: */
509: private Event_VM_DEATH(byte suspendPolicy, Packet packet) {
510: super (suspendPolicy, packet,
511: JDWPConstants.EventKind.VM_DEATH);
512: }
513: };
514:
515: /**
516: * Returns array of ParsedEvent extracted from given EventPacket.
517: *
518: * @param packet
519: * EventPacket to parse events
520: * @return array of extracted ParsedEvents
521: */
522: public static ParsedEvent[] parseEventPacket(Packet packet) {
523:
524: Packet packetCopy = new Packet(packet.toBytesArray());
525:
526: // Suspend Policy field
527: byte suspendPolicy = packetCopy.getNextValueAsByte();
528:
529: // Number of events
530: int eventCount = packetCopy.getNextValueAsInt();
531:
532: ParsedEvent[] events = new ParsedEvent[eventCount];
533:
534: // For all events in packet
535: for (int i = 0; i < eventCount; i++) {
536: byte eventKind = packetCopy.getNextValueAsByte();
537: switch (eventKind) {
538: case JDWPConstants.EventKind.VM_START: {
539: events[i] = new Event_VM_START(suspendPolicy,
540: packetCopy);
541: break;
542: }
543: case JDWPConstants.EventKind.SINGLE_STEP: {
544: events[i] = new Event_SINGLE_STEP(suspendPolicy,
545: packetCopy);
546: break;
547: }
548: case JDWPConstants.EventKind.BREAKPOINT: {
549: events[i] = new Event_BREAKPOINT(suspendPolicy,
550: packetCopy);
551: break;
552: }
553: case JDWPConstants.EventKind.METHOD_ENTRY: {
554: events[i] = new Event_METHOD_ENTRY(suspendPolicy,
555: packetCopy);
556: break;
557: }
558: case JDWPConstants.EventKind.METHOD_EXIT: {
559: events[i] = new Event_METHOD_EXIT(suspendPolicy,
560: packetCopy);
561: break;
562: }
563: case JDWPConstants.EventKind.EXCEPTION: {
564: events[i] = new Event_EXCEPTION(suspendPolicy,
565: packetCopy);
566: break;
567: }
568: case JDWPConstants.EventKind.THREAD_START: {
569: events[i] = new Event_THREAD_START(suspendPolicy,
570: packetCopy);
571: break;
572: }
573: case JDWPConstants.EventKind.THREAD_DEATH: {
574: events[i] = new Event_THREAD_DEATH(suspendPolicy,
575: packetCopy);
576: break;
577: }
578: case JDWPConstants.EventKind.CLASS_PREPARE: {
579: events[i] = new Event_CLASS_PREPARE(suspendPolicy,
580: packetCopy);
581: break;
582: }
583: case JDWPConstants.EventKind.CLASS_UNLOAD: {
584: events[i] = new Event_CLASS_UNLOAD(suspendPolicy,
585: packetCopy);
586: break;
587: }
588: case JDWPConstants.EventKind.FIELD_ACCESS: {
589: events[i] = new Event_FIELD_ACCESS(suspendPolicy,
590: packetCopy);
591: break;
592: }
593: case JDWPConstants.EventKind.FIELD_MODIFICATION: {
594: events[i] = new Event_FIELD_MODIFICATION(suspendPolicy,
595: packetCopy);
596: break;
597: }
598: case JDWPConstants.EventKind.VM_DEATH: {
599: events[i] = new Event_VM_DEATH(suspendPolicy,
600: packetCopy);
601: break;
602: }
603: default: {
604: throw new TestErrorException(
605: "Unexpected kind of event: " + eventKind);
606: }
607: }
608: }
609: return events;
610: }
611:
612: }
|