History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/tools/example/debug/tty/EventRequestSpec.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/tools/example/debug/tty/EventRequestSpec.java+210−0
@@ -0,0 +1,210 @@
1+/*
2+ * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package com.sun.tools.example.debug.tty;
27+
28+import com.sun.jdi.*;
29+import com.sun.jdi.request.EventRequest;
30+import com.sun.jdi.request.ExceptionRequest;
31+import com.sun.jdi.request.ClassPrepareRequest;
32+import com.sun.jdi.event.ClassPrepareEvent;
33+import java.util.ArrayList;
34+
35+abstract class EventRequestSpec {
36+
37+ final ReferenceTypeSpec refSpec;
38+
39+ int suspendPolicy = EventRequest.SUSPEND_ALL;
40+
41+ EventRequest resolved = null;
42+ ClassPrepareRequest prepareRequest = null;
43+
44+ EventRequestSpec(ReferenceTypeSpec refSpec) {
45+ this.refSpec = refSpec;
46+ }
47+
48+ /**
49+ * The 'refType' is known to match, return the EventRequest.
50+ */
51+ abstract EventRequest resolveEventRequest(ReferenceType refType)
52+ throws Exception;
53+
54+ /**
55+ * @return If this EventRequestSpec matches the 'refType'
56+ * return the cooresponding EventRequest. Otherwise
57+ * return null.
58+ */
59+ synchronized EventRequest resolve(ClassPrepareEvent event) throws Exception {
60+ if ((resolved == null) &&
61+ (prepareRequest != null) &&
62+ prepareRequest.equals(event.request())) {
63+
64+ resolved = resolveEventRequest(event.referenceType());
65+ prepareRequest.disable();
66+ Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
67+ prepareRequest = null;
68+
69+ if (refSpec instanceof PatternReferenceTypeSpec) {
70+ PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
71+ if (! prs.isUnique()){
72+ /*
73+ * Class pattern event requests are never
74+ * considered "resolved", since future class loads
75+ * might also match.
76+ * Create and enable a new ClassPrepareRequest to
77+ * keep trying to resolve.
78+ */
79+ resolved = null;
80+ prepareRequest = refSpec.createPrepareRequest();
81+ prepareRequest.enable();
82+ }
83+ }
84+ }
85+ return resolved;
86+ }
87+
88+ synchronized void remove() {
89+ if (isResolved()) {
90+ Env.vm().eventRequestManager().deleteEventRequest(resolved());
91+ }
92+ if (refSpec instanceof PatternReferenceTypeSpec) {
93+ PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
94+ if (! prs.isUnique()){
95+ /*
96+ * This is a class pattern. Track down and delete
97+ * all EventRequests matching this spec.
98+ * Note: Class patterns apply only to ExceptionRequests,
99+ * so that is all we need to examine.
100+ */
101+ ArrayList<ExceptionRequest> deleteList = new ArrayList<ExceptionRequest>();
102+ for (ExceptionRequest er :
103+ Env.vm().eventRequestManager().exceptionRequests()) {
104+ if (prs.matches(er.exception())) {
105+ deleteList.add (er);
106+ }
107+ }
108+ Env.vm().eventRequestManager().deleteEventRequests(deleteList);
109+ }
110+ }
111+ }
112+
113+ private EventRequest resolveAgainstPreparedClasses() throws Exception {
114+ for (ReferenceType refType : Env.vm().allClasses()) {
115+ if (refType.isPrepared() && refSpec.matches(refType)) {
116+ resolved = resolveEventRequest(refType);
117+ }
118+ }
119+ return resolved;
120+ }
121+
122+ synchronized EventRequest resolveEagerly() throws Exception {
123+ try {
124+ if (resolved == null) {
125+ /*
126+ * Not resolved. Schedule a prepare request so we
127+ * can resolve later.
128+ */
129+ prepareRequest = refSpec.createPrepareRequest();
130+ prepareRequest.enable();
131+
132+ // Try to resolve in case the class is already loaded.
133+ resolveAgainstPreparedClasses();
134+ if (resolved != null) {
135+ prepareRequest.disable();
136+ Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
137+ prepareRequest = null;
138+ }
139+ }
140+ if (refSpec instanceof PatternReferenceTypeSpec) {
141+ PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
142+ if (! prs.isUnique()){
143+ /*
144+ * Class pattern event requests are never
145+ * considered "resolved", since future class loads
146+ * might also match. Create a new
147+ * ClassPrepareRequest if necessary and keep
148+ * trying to resolve.
149+ */
150+ resolved = null;
151+ if (prepareRequest == null) {
152+ prepareRequest = refSpec.createPrepareRequest();
153+ prepareRequest.enable();
154+ }
155+ }
156+ }
157+ } catch (VMNotConnectedException e) {
158+ // Do nothing. Another resolve will be attempted when the
159+ // VM is started.
160+ }
161+ return resolved;
162+ }
163+
164+ /**
165+ * @return the eventRequest this spec has been resolved to,
166+ * null if so far unresolved.
167+ */
168+ EventRequest resolved() {
169+ return resolved;
170+ }
171+
172+ /**
173+ * @return true if this spec has been resolved.
174+ */
175+ boolean isResolved() {
176+ return resolved != null;
177+ }
178+
179+ protected boolean isJavaIdentifier(String s) {
180+ if (s.length() == 0) {
181+ return false;
182+ }
183+
184+ int cp = s.codePointAt(0);
185+ if (! Character.isJavaIdentifierStart(cp)) {
186+ return false;
187+ }
188+
189+ for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
190+ cp = s.codePointAt(i);
191+ if (! Character.isJavaIdentifierPart(cp)) {
192+ return false;
193+ }
194+ }
195+
196+ return true;
197+ }
198+
199+ String errorMessageFor(Exception e) {
200+ if (e instanceof IllegalArgumentException) {
201+ return (MessageOutput.format("Invalid command syntax"));
202+ } else if (e instanceof RuntimeException) {
203+ // A runtime exception that we were not expecting
204+ throw (RuntimeException)e;
205+ } else {
206+ return (MessageOutput.format("Internal error; unable to set",
207+ this.refSpec.toString()));
208+ }
209+ }
210+}