| 1 21 22 package org.armedbear.j.jdb; 23 24 import com.sun.jdi.InvalidTypeException; 25 import com.sun.jdi.Location; 26 import com.sun.jdi.Method; 27 import com.sun.jdi.ReferenceType; 28 import com.sun.jdi.request.EventRequest; 29 import com.sun.jdi.request.EventRequestManager; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import org.armedbear.j.Buffer; 33 import org.armedbear.j.Editor; 34 import org.armedbear.j.FastStringBuffer; 35 import org.armedbear.j.JavaSource; 36 import org.armedbear.j.Line; 37 import org.armedbear.j.LocalTag; 38 import org.armedbear.j.Log; 39 import org.armedbear.j.Utilities; 40 41 public final class MethodBreakpoint extends ResolvableBreakpoint 42 { 43 private final String methodName; 44 45 public MethodBreakpoint(Jdb jdb, String className, String methodName) 46 { 47 super(jdb); 48 this.className = className; 49 this.methodName = methodName; 50 } 51 52 public String getMethodName() 53 { 54 return methodName; 55 } 56 57 public EventRequest resolveEventRequest(ReferenceType refType) throws Exception  58 { 59 Method method = findMatchingMethod(refType); 60 if (method == null) 61 throw new InvalidTypeException(); 62 Location location = method.location(); 63 if (location == null) 64 throw new InvalidTypeException(); EventRequestManager erm = 66 refType.virtualMachine().eventRequestManager(); 67 EventRequest er = erm.createBreakpointRequest(location); 68 er.setSuspendPolicy(EventRequest.SUSPEND_ALL); 69 er.enable(); 70 setBreakpointInSource(refType, method); 71 return er; 72 } 73 74 private Method findMatchingMethod(ReferenceType refType) 76 { 77 Iterator iter = refType.methods().iterator(); 78 while (iter.hasNext()) { 79 Method method = (Method) iter.next(); 80 if (method.name().equals(methodName)) 81 return method; 82 } 83 return null; 84 } 85 86 private void setBreakpointInSource(ReferenceType refType, Method method) 87 { 88 file = JavaSource.findSource(refType.name(), jdb.getSourcePath()); 89 if (file == null) { 90 Log.debug("setBreakpointInSource findSource returned null"); 91 return; 92 } 93 Buffer buf = Editor.getBuffer(file); 94 if (buf == null) 95 return; 96 if (!buf.initialized()) 97 buf.initialize(); 98 if (!buf.isLoaded()) 99 buf.load(); 100 List tags = buf.getTags(true); 101 if (tags == null) 102 return; 103 String lookFor = refType.name(); 104 int index = lookFor.lastIndexOf('.'); 106 if (index >= 0) 107 lookFor = lookFor.substring(index+1); 108 lookFor += '.'; 109 lookFor += method.name(); 110 Log.debug("lookFor = |" + lookFor + "|"); 111 Line begin = null; 112 Line end = null; 113 for (int i = 0; i < tags.size(); i++) { 114 LocalTag tag = (LocalTag) tags.get(i); 115 if (tag.getName() != null) { 116 if (tag.getName().equals(lookFor)) { 117 begin = tag.getLine(); 118 if (++i < tags.size()) { 119 tag = (LocalTag) tags.get(i); 120 end = tag.getLine(); 121 } 122 break; 123 } 124 } 125 } 126 if (begin == null) 127 return; 128 for (Line ln = begin.next(); ln != end && ln != null; ln = ln.next()) { 130 String text = ln.getText().trim(); 131 if (!text.startsWith("//")) { 132 if (text.indexOf('=') >= 0 || text.indexOf('(') >= 0) { 133 ln.setAnnotation(new BreakpointAnnotation(this)); 135 line = ln; 136 break; 137 } 138 } 139 } 140 } 141 142 public void resolved() 143 { 144 if (file != null) { 145 Buffer buffer = Editor.getBufferList().findBuffer(file); 146 if (buffer != null) 147 buffer.repaint(); 148 } 149 if (line != null) 150 line.setAnnotation(new BreakpointAnnotation(this)); 151 jdb.log("Breakpoint resolved: " + getLocationString()); 152 } 153 154 public String getLocationString() 155 { 156 FastStringBuffer sb = new FastStringBuffer(); 157 if (className != null) { 158 sb.append(className); 159 sb.append('.'); 160 } 161 sb.append(methodName); 162 if (!isResolved()) 163 sb.append(' '); 164 return sb.toString(); 165 } 166 167 public String toString() 168 { 169 FastStringBuffer sb = new FastStringBuffer(); 170 if (className != null) { 171 sb.append(className); 172 sb.append('.'); 173 } 174 sb.append(methodName); 175 if (!isResolved()) { 176 sb.append(' '); 177 sb.append("(deferred)"); 178 } 179 return sb.toString(); 180 } 181 182 public String toXml() 183 { 184 int indent = 4; 185 final String separator = System.getProperty("line.separator"); 186 FastStringBuffer sb = new FastStringBuffer(Utilities.spaces(indent)); 187 sb.append("<breakpoint"); 188 sb.append(separator); 189 if (className != null) { 190 sb.append(Utilities.spaces(indent+2)); 191 sb.append("className=\""); 192 sb.append(className); 193 sb.append('"'); 194 sb.append(separator); 195 } 196 if (methodName != null) { 197 sb.append(Utilities.spaces(indent+2)); 198 sb.append("methodName=\""); 199 sb.append(methodName); 200 sb.append('"'); 201 sb.append(separator); 202 } 203 sb.append(Utilities.spaces(indent)); 204 sb.append("/>"); 205 sb.append(separator); 206 return sb.toString(); 207 } 208 } 209 | Popular Tags |