KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > tty > EventRequestSpecList


1 /*
2  * @(#)EventRequestSpecList.java 1.21 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.tty;
36
37 import com.sun.jdi.*;
38 import com.sun.jdi.request.EventRequest;
39 import com.sun.jdi.event.ClassPrepareEvent;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46
47 class EventRequestSpecList {
48
49     private static final int statusResolved = 1;
50     private static final int statusUnresolved = 2;
51     private static final int statusError = 3;
52     
53     // all specs
54
private List JavaDoc eventRequestSpecs = Collections.synchronizedList(
55                                                   new ArrayList JavaDoc());
56
57     EventRequestSpecList() {
58     }
59
60     /**
61      * Resolve all deferred eventRequests waiting for 'refType'.
62      * @return true if it completes successfully, false on error.
63      */

64     boolean resolve(ClassPrepareEvent event) {
65         boolean failure = false;
66         synchronized(eventRequestSpecs) {
67             Iterator JavaDoc iter = eventRequestSpecs.iterator();
68             while (iter.hasNext()) {
69                 EventRequestSpec spec = (EventRequestSpec)iter.next();
70                 if (!spec.isResolved()) {
71                     try {
72                         EventRequest eventRequest = spec.resolve(event);
73                         if (eventRequest != null) {
74                             MessageOutput.println("Set deferred", spec.toString());
75                         }
76                     } catch (Exception JavaDoc e) {
77                         MessageOutput.println("Unable to set deferred",
78                                               new Object JavaDoc [] {spec.toString(),
79                                                              spec.errorMessageFor(e)});
80                         failure = true;
81                     }
82                 }
83             }
84         }
85         return !failure;
86     }
87
88     void resolveAll() {
89         Iterator JavaDoc iter = eventRequestSpecs.iterator();
90         while (iter.hasNext()) {
91             EventRequestSpec spec = (EventRequestSpec)iter.next();
92             try {
93                 EventRequest eventRequest = spec.resolveEagerly();
94                 if (eventRequest != null) {
95                     MessageOutput.println("Set deferred", spec.toString());
96                 }
97             } catch (Exception JavaDoc e) {
98             }
99         }
100     }
101
102     boolean addEagerlyResolve(EventRequestSpec spec) {
103         try {
104             eventRequestSpecs.add(spec);
105             EventRequest eventRequest = spec.resolveEagerly();
106             if (eventRequest != null) {
107                 MessageOutput.println("Set", spec.toString());
108             }
109             return true;
110         } catch (Exception JavaDoc exc) {
111             MessageOutput.println("Unable to set",
112                                   new Object JavaDoc [] {spec.toString(),
113                                                  spec.errorMessageFor(exc)});
114             return false;
115         }
116     }
117
118     EventRequestSpec createBreakpoint(String JavaDoc classPattern,
119                                  int line) throws ClassNotFoundException JavaDoc {
120         ReferenceTypeSpec refSpec =
121             new PatternReferenceTypeSpec(classPattern);
122         return new BreakpointSpec(refSpec, line);
123     }
124         
125     EventRequestSpec createBreakpoint(String JavaDoc classPattern,
126                                  String JavaDoc methodId,
127                                  List JavaDoc methodArgs)
128                                 throws MalformedMemberNameException,
129                                        ClassNotFoundException JavaDoc {
130         ReferenceTypeSpec refSpec =
131             new PatternReferenceTypeSpec(classPattern);
132         return new BreakpointSpec(refSpec, methodId, methodArgs);
133     }
134         
135     EventRequestSpec createExceptionCatch(String JavaDoc classPattern,
136                                           boolean notifyCaught,
137                                           boolean notifyUncaught)
138                                             throws ClassNotFoundException JavaDoc {
139         ReferenceTypeSpec refSpec =
140             new PatternReferenceTypeSpec(classPattern);
141         return new ExceptionSpec(refSpec, notifyCaught, notifyUncaught);
142     }
143         
144     EventRequestSpec createAccessWatchpoint(String JavaDoc classPattern,
145                                        String JavaDoc fieldId)
146                                       throws MalformedMemberNameException,
147                                              ClassNotFoundException JavaDoc {
148         ReferenceTypeSpec refSpec =
149             new PatternReferenceTypeSpec(classPattern);
150         return new AccessWatchpointSpec(refSpec, fieldId);
151     }
152         
153     EventRequestSpec createModificationWatchpoint(String JavaDoc classPattern,
154                                        String JavaDoc fieldId)
155                                       throws MalformedMemberNameException,
156                                              ClassNotFoundException JavaDoc {
157         ReferenceTypeSpec refSpec =
158             new PatternReferenceTypeSpec(classPattern);
159         return new ModificationWatchpointSpec(refSpec, fieldId);
160     }
161
162     boolean delete(EventRequestSpec proto) {
163         synchronized (eventRequestSpecs) {
164             int inx = eventRequestSpecs.indexOf(proto);
165             if (inx != -1) {
166                 EventRequestSpec spec = (EventRequestSpec)eventRequestSpecs.get(inx);
167                 spec.remove();
168                 eventRequestSpecs.remove(inx);
169                 return true;
170             } else {
171                 return false;
172             }
173         }
174     }
175
176     List JavaDoc eventRequestSpecs() {
177        // We need to make a copy to avoid synchronization problems
178
synchronized (eventRequestSpecs) {
179             return new ArrayList JavaDoc(eventRequestSpecs);
180         }
181     }
182 }
183
Popular Tags