KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > breakpoints > ClassBasedBreakpoint


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.breakpoints;
21
22 import com.sun.jdi.ReferenceType;
23 import com.sun.jdi.VMDisconnectedException;
24 import com.sun.jdi.event.ClassPrepareEvent;
25 import com.sun.jdi.event.ClassUnloadEvent;
26 import com.sun.jdi.event.Event;
27 import com.sun.jdi.request.ClassPrepareRequest;
28 import com.sun.jdi.request.ClassUnloadRequest;
29 import com.sun.jdi.VirtualMachine;
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import org.netbeans.api.debugger.jpda.ClassLoadUnloadBreakpoint;
37
38 import org.netbeans.api.debugger.jpda.JPDABreakpoint;
39 import org.netbeans.api.debugger.Session;
40 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
41 import org.netbeans.spi.debugger.jpda.SourcePathProvider;
42 import org.openide.util.WeakListeners;
43
44 /**
45 * Implementation of breakpoint on method.
46 *
47 * @author Jan Jancura
48 */

49 public abstract class ClassBasedBreakpoint extends BreakpointImpl {
50     
51     private String JavaDoc sourceRoot;
52     private final Object JavaDoc SOURCE_ROOT_LOCK = new Object JavaDoc();
53     private SourceRootsChangedListener srChListener;
54     
55     private static Logger JavaDoc logger = Logger.getLogger("org.netbeans.modules.debugger.jpda.breakpoints"); // NOI18N
56

57     public ClassBasedBreakpoint (
58         JPDABreakpoint breakpoint,
59         JPDADebuggerImpl debugger,
60         Session session
61     ) {
62         super (breakpoint, null, debugger, session);
63     }
64     
65     public ClassBasedBreakpoint (
66         JPDABreakpoint breakpoint,
67         BreakpointsReader reader,
68         JPDADebuggerImpl debugger,
69         Session session
70     ) {
71         super (breakpoint, reader, debugger, session);
72     }
73     
74     protected final void setSourceRoot(String JavaDoc sourceRoot) {
75         synchronized (SOURCE_ROOT_LOCK) {
76             this.sourceRoot = sourceRoot;
77             if (sourceRoot != null && srChListener == null) {
78                 srChListener = new SourceRootsChangedListener();
79                 getDebugger().getEngineContext().addPropertyChangeListener(
80                         WeakListeners.propertyChange(srChListener,
81                                                      getDebugger().getEngineContext()));
82             } else if (sourceRoot == null) {
83                 srChListener = null; // release the listener
84
}
85         }
86     }
87     
88     protected final String JavaDoc getSourceRoot() {
89         synchronized (SOURCE_ROOT_LOCK) {
90             return sourceRoot;
91         }
92     }
93     
94     protected boolean isEnabled() {
95         synchronized (SOURCE_ROOT_LOCK) {
96             String JavaDoc sourceRoot = getSourceRoot();
97             if (sourceRoot == null) {
98                 return true;
99             }
100             String JavaDoc[] sourceRoots = getDebugger().getEngineContext().getSourceRoots();
101             for (int i = 0; i < sourceRoots.length; i++) {
102                 if (sourceRoot.equals(sourceRoots[i])) {
103                     return true;
104                 }
105             }
106             if (logger.isLoggable(Level.FINE)) {
107                 logger.fine("Breakpoint "+getBreakpoint()+
108                             " NOT submitted because it's source root "+sourceRoot+
109                             " is not contained in debugger's source roots: "+
110                             java.util.Arrays.asList(sourceRoots));
111             }
112             return false;
113         }
114     }
115     
116     protected void setClassRequests (
117         String JavaDoc[] classFilters,
118         String JavaDoc[] classExclusionFilters,
119         int breakpointType
120     ) {
121         try {
122             if ((breakpointType & ClassLoadUnloadBreakpoint.TYPE_CLASS_LOADED) != 0
123             ) {
124                 int i, k = classFilters.length;
125                 for (i = 0; i < k; i++) {
126                     ClassPrepareRequest cpr = getEventRequestManager ().
127                         createClassPrepareRequest ();
128                     cpr.addClassFilter (classFilters [i]);
129                     logger.fine("Set class load request: " + classFilters [i]);
130                     addEventRequest (cpr);
131                 }
132                 k = classExclusionFilters.length;
133                 for (i = 0; i < k; i++) {
134                     ClassPrepareRequest cpr = getEventRequestManager ().
135                         createClassPrepareRequest ();
136                     cpr.addClassExclusionFilter (classExclusionFilters [i]);
137                     logger.fine("Set class load exclusion request: " + classExclusionFilters [i]);
138                     addEventRequest (cpr);
139                 }
140             }
141             if ((breakpointType & ClassLoadUnloadBreakpoint.TYPE_CLASS_UNLOADED) != 0
142             ) {
143                 int i, k = classFilters.length;
144                 for (i = 0; i < k; i++) {
145                     ClassUnloadRequest cur = getEventRequestManager ().
146                         createClassUnloadRequest ();
147                     cur.addClassFilter (classFilters [i]);
148                     logger.fine("Set class unload request: " + classFilters [i]);
149                     addEventRequest (cur);
150                 }
151                 k = classExclusionFilters.length;
152                 for (i = 0; i < k; i++) {
153                     ClassUnloadRequest cur = getEventRequestManager ().
154                         createClassUnloadRequest ();
155                     cur.addClassExclusionFilter (classExclusionFilters [i]);
156                     logger.fine("Set class unload exclusion request: " + classExclusionFilters [i]);
157                     addEventRequest (cur);
158                 }
159             }
160         } catch (VMDisconnectedException e) {
161         }
162     }
163     
164     protected boolean checkLoadedClasses (
165         String JavaDoc className
166     ) {
167         VirtualMachine vm = getVirtualMachine ();
168         if (vm == null) return false;
169         boolean all = className.startsWith("*") || className.endsWith("*"); // NOI18N
170
logger.fine("Check loaded classes: " + className + ", will load all classes: " + all); // NOI18N
171
boolean matched = false;
172         try {
173             Iterator JavaDoc i = null;
174             if (all) {
175                 i = vm.allClasses ().iterator ();
176             } else {
177                 i = vm.classesByName (className).iterator ();
178             }
179             while (i.hasNext ()) {
180                 ReferenceType referenceType = (ReferenceType) i.next ();
181 // if (verbose)
182
// System.out.println("B cls: " + referenceType);
183
if (i != null) {
184                     String JavaDoc name = referenceType.name ();
185                     if (match (name, className)) {
186                         logger.fine(" Class loaded: " + referenceType);
187                         classLoaded (referenceType);
188                         matched = true;
189                     }
190                 }
191             }
192         } catch (VMDisconnectedException e) {
193         }
194         return matched;
195     }
196
197     public boolean exec (Event event) {
198         if (event instanceof ClassPrepareEvent) {
199             logger.fine(" Class loaded: " + ((ClassPrepareEvent) event).referenceType ());
200             classLoaded (((ClassPrepareEvent) event).referenceType ());
201         } else if (event instanceof ClassUnloadEvent) {
202             logger.fine(" Class unloaded: " + ((ClassPrepareEvent) event).referenceType ());
203             classUnloaded (((ClassUnloadEvent) event).className ());
204         }
205         return true;
206     }
207     
208     protected void classLoaded (ReferenceType referenceType) {}
209     protected void classUnloaded (String JavaDoc className) {}
210     
211     
212     private class SourceRootsChangedListener implements PropertyChangeListener JavaDoc {
213         
214         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
215             if (SourcePathProvider.PROP_SOURCE_ROOTS.equals(evt.getPropertyName())) {
216                 update();
217             }
218         }
219         
220     }
221 }
222
223
Popular Tags