1 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 ; 31 import java.beans.PropertyChangeListener ; 32 33 import java.util.Iterator ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 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 49 public abstract class ClassBasedBreakpoint extends BreakpointImpl { 50 51 private String sourceRoot; 52 private final Object SOURCE_ROOT_LOCK = new Object (); 53 private SourceRootsChangedListener srChListener; 54 55 private static Logger logger = Logger.getLogger("org.netbeans.modules.debugger.jpda.breakpoints"); 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 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; } 85 } 86 } 87 88 protected final String getSourceRoot() { 89 synchronized (SOURCE_ROOT_LOCK) { 90 return sourceRoot; 91 } 92 } 93 94 protected boolean isEnabled() { 95 synchronized (SOURCE_ROOT_LOCK) { 96 String sourceRoot = getSourceRoot(); 97 if (sourceRoot == null) { 98 return true; 99 } 100 String [] 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 [] classFilters, 118 String [] 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 className 166 ) { 167 VirtualMachine vm = getVirtualMachine (); 168 if (vm == null) return false; 169 boolean all = className.startsWith("*") || className.endsWith("*"); logger.fine("Check loaded classes: " + className + ", will load all classes: " + all); boolean matched = false; 172 try { 173 Iterator 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 (i != null) { 184 String 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 className) {} 210 211 212 private class SourceRootsChangedListener implements PropertyChangeListener { 213 214 public void propertyChange(PropertyChangeEvent evt) { 215 if (SourcePathProvider.PROP_SOURCE_ROOTS.equals(evt.getPropertyName())) { 216 update(); 217 } 218 } 219 220 } 221 } 222 223 | Popular Tags |