KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > jdb > MethodBreakpoint


1 /*
2  * MethodBreakpoint.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: MethodBreakpoint.java,v 1.4 2003/05/25 01:25:14 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

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 JavaDoc;
31 import java.util.List JavaDoc;
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 JavaDoc methodName;
44
45     public MethodBreakpoint(Jdb jdb, String JavaDoc className, String JavaDoc methodName)
46     {
47         super(jdb);
48         this.className = className;
49         this.methodName = methodName;
50     }
51
52     public String JavaDoc getMethodName()
53     {
54         return methodName;
55     }
56
57     public EventRequest resolveEventRequest(ReferenceType refType) throws Exception JavaDoc
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(); // Abstract or native method.
65
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     // BUG! Overloads are not handled correctly.
75
private Method findMatchingMethod(ReferenceType refType)
76     {
77         Iterator JavaDoc 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 JavaDoc tags = buf.getTags(true);
101         if (tags == null)
102             return;
103         String JavaDoc lookFor = refType.name();
104         // Remove package prefix.
105
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         // Look for first line of actual code.
129
for (Line ln = begin.next(); ln != end && ln != null; ln = ln.next()) {
130             String JavaDoc text = ln.getText().trim();
131             if (!text.startsWith("//")) {
132                 if (text.indexOf('=') >= 0 || text.indexOf('(') >= 0) {
133                     // Line looks like code.
134
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 JavaDoc 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 JavaDoc 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 JavaDoc toXml()
183     {
184         int indent = 4;
185         final String JavaDoc 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