KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)EventRequestSpec.java 1.20 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.request.ExceptionRequest;
40 import com.sun.jdi.request.ClassPrepareRequest;
41 import com.sun.jdi.event.ClassPrepareEvent;
42 import java.util.List JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 abstract class EventRequestSpec {
47
48     final ReferenceTypeSpec refSpec;
49
50     int suspendPolicy = EventRequest.SUSPEND_ALL;
51
52     EventRequest resolved = null;
53     ClassPrepareRequest prepareRequest = null;
54
55     EventRequestSpec(ReferenceTypeSpec refSpec) {
56         this.refSpec = refSpec;
57     }
58
59     /**
60      * The 'refType' is known to match, return the EventRequest.
61      */

62     abstract EventRequest resolveEventRequest(ReferenceType refType)
63                                            throws Exception JavaDoc;
64
65     /**
66      * @return If this EventRequestSpec matches the 'refType'
67      * return the cooresponding EventRequest. Otherwise
68      * return null.
69      */

70     synchronized EventRequest resolve(ClassPrepareEvent event) throws Exception JavaDoc {
71         if ((resolved == null) &&
72             (prepareRequest != null) &&
73             prepareRequest.equals(event.request())) {
74
75             resolved = resolveEventRequest(event.referenceType());
76             prepareRequest.disable();
77             Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
78             prepareRequest = null;
79
80             if (refSpec instanceof PatternReferenceTypeSpec) {
81                 PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
82                 if (! prs.isUnique()){
83                     /*
84                      * Class pattern event requests are never
85                      * considered "resolved", since future class loads
86                      * might also match.
87                      * Create and enable a new ClassPrepareRequest to
88                      * keep trying to resolve.
89                      */

90                     resolved = null;
91                     prepareRequest = refSpec.createPrepareRequest();
92                     prepareRequest.enable();
93                 }
94             }
95         }
96         return resolved;
97     }
98
99     synchronized void remove() {
100         if (isResolved()) {
101             Env.vm().eventRequestManager().deleteEventRequest(resolved());
102         }
103         if (refSpec instanceof PatternReferenceTypeSpec) {
104             PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
105             if (! prs.isUnique()){
106                 /*
107                  * This is a class pattern. Track down and delete
108                  * all EventRequests matching this spec.
109                  * Note: Class patterns apply only to ExceptionRequests,
110                  * so that is all we need to examine.
111                  */

112                 ArrayList JavaDoc deleteList = new ArrayList JavaDoc();
113                 Iterator JavaDoc iter =
114                     Env.vm().eventRequestManager().exceptionRequests().iterator();
115                 while (iter.hasNext()) {
116                     ExceptionRequest er = (ExceptionRequest)iter.next();
117                     if (prs.matches(er.exception())) {
118                         deleteList.add (er);
119                     }
120                 }
121                 Env.vm().eventRequestManager().deleteEventRequests(deleteList);
122             }
123         }
124     }
125     
126     private EventRequest resolveAgainstPreparedClasses() throws Exception JavaDoc {
127         Iterator JavaDoc iter = Env.vm().allClasses().iterator();
128         while (iter.hasNext()) {
129             ReferenceType refType = (ReferenceType)iter.next();
130             if (refType.isPrepared() && refSpec.matches(refType)) {
131                 resolved = resolveEventRequest(refType);
132             }
133         }
134         return resolved;
135     }
136
137     synchronized EventRequest resolveEagerly() throws Exception JavaDoc {
138         try {
139             if (resolved == null) {
140                 /*
141                  * Not resolved. Schedule a prepare request so we
142                  * can resolve later.
143                  */

144                 prepareRequest = refSpec.createPrepareRequest();
145                 prepareRequest.enable();
146     
147                 // Try to resolve in case the class is already loaded.
148
resolveAgainstPreparedClasses();
149                 if (resolved != null) {
150                     prepareRequest.disable();
151                     Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
152                     prepareRequest = null;
153                 }
154             }
155             if (refSpec instanceof PatternReferenceTypeSpec) {
156                 PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
157                 if (! prs.isUnique()){
158                     /*
159                      * Class pattern event requests are never
160                      * considered "resolved", since future class loads
161                      * might also match. Create a new
162                      * ClassPrepareRequest if necessary and keep
163                      * trying to resolve.
164                      */

165                     resolved = null;
166                     if (prepareRequest == null) {
167                         prepareRequest = refSpec.createPrepareRequest();
168                         prepareRequest.enable();
169                     }
170                 }
171             }
172         } catch (VMNotConnectedException e) {
173             // Do nothing. Another resolve will be attempted when the
174
// VM is started.
175
}
176         return resolved;
177     }
178
179     /**
180      * @return the eventRequest this spec has been resolved to,
181      * null if so far unresolved.
182      */

183     EventRequest resolved() {
184         return resolved;
185     }
186
187     /**
188      * @return true if this spec has been resolved.
189      */

190     boolean isResolved() {
191         return resolved != null;
192     }
193
194     protected boolean isJavaIdentifier(String JavaDoc s) {
195         if (s.length() == 0) {
196             return false;
197         }
198
199         int cp = s.codePointAt(0);
200         if (! Character.isJavaIdentifierStart(cp)) {
201             return false;
202         }
203
204         for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
205             cp = s.codePointAt(i);
206             if (! Character.isJavaIdentifierPart(cp)) {
207                 return false;
208             }
209         }
210
211         return true;
212     }
213
214     String JavaDoc errorMessageFor(Exception JavaDoc e) {
215         if (e instanceof IllegalArgumentException JavaDoc) {
216             return (MessageOutput.format("Invalid command syntax"));
217         } else if (e instanceof RuntimeException JavaDoc) {
218             // A runtime exception that we were not expecting
219
throw (RuntimeException JavaDoc)e;
220         } else {
221             return (MessageOutput.format("Internal error; unable to set",
222                                          this.refSpec.toString()));
223         }
224     }
225 }
226
227
228
Popular Tags