KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > bdi > LineBreakpointSpec


1 /*
2  * @(#)LineBreakpointSpec.java 1.9 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.bdi;
36
37 import com.sun.jdi.*;
38 import com.sun.jdi.request.*;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Iterator JavaDoc;
43
44 public class LineBreakpointSpec extends BreakpointSpec {
45     int lineNumber;
46
47     LineBreakpointSpec(EventRequestSpecList specs,
48                        ReferenceTypeSpec refSpec, int lineNumber) {
49         super(specs, refSpec);
50         this.lineNumber = lineNumber;
51     }
52
53     /**
54      * The 'refType' is known to match.
55      */

56     void resolve(ReferenceType refType) throws InvalidTypeException,
57                                              LineNotFoundException {
58         if (!(refType instanceof ClassType)) {
59             throw new InvalidTypeException();
60         }
61         Location location = location((ClassType)refType);
62         setRequest(refType.virtualMachine().eventRequestManager()
63                    .createBreakpointRequest(location));
64     }
65
66     private Location location(ClassType clazz) throws
67                                             LineNotFoundException {
68         Location location = null;
69         try {
70             List JavaDoc locs = clazz.locationsOfLine(lineNumber());
71             if (locs.size() == 0) {
72                 throw new LineNotFoundException();
73             }
74             // TODO handle multiple locations
75
location = (Location)locs.get(0);
76             if (location.method() == null) {
77                 throw new LineNotFoundException();
78             }
79         } catch (AbsentInformationException e) {
80             /*
81              * TO DO: throw something more specific, or allow
82              * AbsentInfo exception to pass through.
83              */

84             throw new LineNotFoundException();
85         }
86         return location;
87     }
88
89     public int lineNumber() {
90         return lineNumber;
91     }
92
93     public int hashCode() {
94         return refSpec.hashCode() + lineNumber;
95     }
96
97     public boolean equals(Object JavaDoc obj) {
98         if (obj instanceof LineBreakpointSpec) {
99             LineBreakpointSpec breakpoint = (LineBreakpointSpec)obj;
100
101             return refSpec.equals(breakpoint.refSpec) &&
102                    (lineNumber == breakpoint.lineNumber);
103         } else {
104             return false;
105         }
106     }
107
108     public String JavaDoc errorMessageFor(Exception JavaDoc e) {
109         if (e instanceof LineNotFoundException) {
110             return ("No code at line " + lineNumber() + " in " + refSpec);
111         } else if (e instanceof InvalidTypeException) {
112             return ("Breakpoints can be located only in classes. " +
113                         refSpec + " is an interface or array");
114         } else {
115             return super.errorMessageFor( e);
116         }
117     }
118
119     public String JavaDoc toString() {
120         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("breakpoint ");
121         buffer.append(refSpec.toString());
122         buffer.append(':');
123         buffer.append(lineNumber);
124         buffer.append(" (");
125         buffer.append(getStatusString());
126         buffer.append(')');
127         return buffer.toString();
128     }
129 }
130
Popular Tags