KickJava   Java API By Example, From Geeks To Geeks.

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


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.Location;
23 import com.sun.jdi.Method;
24 import com.sun.jdi.ReferenceType;
25 import com.sun.jdi.VMDisconnectedException;
26 import com.sun.jdi.Value;
27 import com.sun.jdi.VirtualMachine;
28 import com.sun.jdi.event.BreakpointEvent;
29 import com.sun.jdi.event.Event;
30 import com.sun.jdi.event.LocatableEvent;
31 import com.sun.jdi.event.MethodEntryEvent;
32 import com.sun.jdi.event.MethodExitEvent;
33 import com.sun.jdi.request.BreakpointRequest;
34 import com.sun.jdi.request.MethodEntryRequest;
35 import com.sun.jdi.request.MethodExitRequest;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Set JavaDoc;
41 import org.netbeans.api.debugger.Breakpoint.VALIDITY;
42 import org.netbeans.api.debugger.Session;
43 import org.netbeans.api.debugger.jpda.ClassLoadUnloadBreakpoint;
44 import org.netbeans.api.debugger.jpda.MethodBreakpoint;
45 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
46 import org.openide.util.NbBundle;
47
48 /**
49 * Implementation of method breakpoint.
50 *
51 * @author Jan Jancura
52 */

53 public class MethodBreakpointImpl extends ClassBasedBreakpoint {
54     
55     private static final boolean IS_JDK_16 = !System.getProperty("java.version").startsWith("1.5"); // NOI18N
56

57     
58     private MethodBreakpoint breakpoint;
59     
60     
61     public MethodBreakpointImpl (MethodBreakpoint breakpoint, JPDADebuggerImpl debugger, Session session) {
62         super (breakpoint, debugger, session);
63         this.breakpoint = breakpoint;
64         set ();
65     }
66     
67     public static boolean canGetMethodReturnValues(VirtualMachine vm) {
68         if (!IS_JDK_16) return false;
69         boolean canGetMethodReturnValues = false;
70         java.lang.reflect.Method JavaDoc m = null;
71         try {
72             m = vm.getClass().getMethod("canGetMethodReturnValues", new Class JavaDoc[] {});
73         } catch (Exception JavaDoc ex) {
74         }
75         if (m != null) {
76             try {
77                 m.setAccessible(true);
78                 Object JavaDoc ret = m.invoke(vm, new Object JavaDoc[] {});
79                 canGetMethodReturnValues = Boolean.TRUE.equals(ret);
80             } catch (Exception JavaDoc ex) {
81             }
82         }
83         return canGetMethodReturnValues;
84     }
85     
86     protected void setRequests () {
87         setClassRequests (
88             breakpoint.getClassFilters (),
89             breakpoint.getClassExclusionFilters (),
90             ClassLoadUnloadBreakpoint.TYPE_CLASS_LOADED
91         );
92         checkLoadedClasses (breakpoint.getClassFilters () [0]);
93     }
94
95     public boolean exec (Event event) {
96         if (event instanceof BreakpointEvent)
97             return perform (
98                 breakpoint.getCondition (),
99                 ((BreakpointEvent) event).thread (),
100                 ((LocatableEvent) event).location ().declaringType (),
101                 null
102             );
103         if (event instanceof MethodEntryEvent) {
104             String JavaDoc methodName = ((MethodEntryEvent) event).method().name();
105             Set JavaDoc methodNames = (Set JavaDoc) event.request().getProperty("methodNames");
106             if (methodNames == null || methodNames.contains(methodName)) {
107                 ReferenceType refType = null;
108                 if (((LocatableEvent) event).location() != null) {
109                     refType = ((LocatableEvent) event).location().declaringType();
110                 }
111                 return perform (
112                     breakpoint.getCondition (),
113                     ((MethodEntryEvent) event).thread (),
114                     refType,
115                     null
116                 );
117             }
118         }
119         if (event instanceof MethodExitEvent) {
120             String JavaDoc methodName = ((MethodExitEvent) event).method().name();
121             Set JavaDoc methodNames = (Set JavaDoc) event.request().getProperty("methodNames");
122             if (methodNames == null || methodNames.contains(methodName)) {
123                 ReferenceType refType = null;
124                 if (((LocatableEvent) event).location() != null) {
125                     refType = ((LocatableEvent) event).location().declaringType();
126                 }
127                 Value returnValue = null;
128                 /* JDK 1.6.0 code */
129                 if (IS_JDK_16) { // Retrieval of the return value
130
VirtualMachine vm = event.virtualMachine();
131                     // vm.canGetMethodReturnValues();
132
if (canGetMethodReturnValues(vm)) {
133                         //Value returnValue = ((MethodExitEvent) event).returnValue();
134
java.lang.reflect.Method JavaDoc m = null;
135                         try {
136                             m = event.getClass().getDeclaredMethod("returnValue", new Class JavaDoc[] {});
137                         } catch (Exception JavaDoc ex) {
138                             m = null;
139                         }
140                         if (m != null) {
141                             try {
142                                 m.setAccessible(true);
143                                 Object JavaDoc ret = m.invoke(event, new Object JavaDoc[] {});
144                                 returnValue = (Value) ret;
145                             } catch (Exception JavaDoc ex) {
146                             }
147                         }
148                     }
149                 }
150                 return perform (
151                     breakpoint.getCondition (),
152                     ((MethodExitEvent) event).thread (),
153                     refType,
154                     returnValue
155                 );
156             }
157         }
158         return super.exec (event);
159     }
160     
161     protected void classLoaded (ReferenceType referenceType) {
162         Iterator JavaDoc methods = referenceType.methods ().iterator ();
163         MethodEntryRequest entryReq = null;
164         MethodExitRequest exitReq = null;
165         Set JavaDoc<String JavaDoc> entryMethodNames = null;
166         Set JavaDoc<String JavaDoc> exitMethodNames = null;
167         boolean locationEntry = false;
168         String JavaDoc methodName = breakpoint.getMethodName();
169         while (methods.hasNext ()) {
170             Method method = (Method) methods.next ();
171             if (methodName.equals("") || match (method.name (), methodName)) {
172                 
173                 if ((breakpoint.getBreakpointType() & breakpoint.TYPE_METHOD_ENTRY) != 0) {
174                     if (method.location () != null && !method.isNative()) {
175                         Location location = method.location ();
176                         try {
177                             BreakpointRequest br = getEventRequestManager ().
178                                 createBreakpointRequest (location);
179                             addEventRequest (br);
180                         } catch (VMDisconnectedException e) {
181                         }
182                         locationEntry = true;
183                     } else {
184                         if (entryReq == null) {
185                             entryReq = getEventRequestManager().
186                                     createMethodEntryRequest();
187                             entryReq.addClassFilter(referenceType);
188                             entryMethodNames = new HashSet JavaDoc<String JavaDoc>();
189                             entryReq.putProperty("methodNames", entryMethodNames);
190                         }
191                         entryMethodNames.add(method.name ());
192                     }
193                 }
194                 if ((breakpoint.getBreakpointType() & breakpoint.TYPE_METHOD_EXIT) != 0) {
195                     if (exitReq == null) {
196                         exitReq = getEventRequestManager().
197                                 createMethodExitRequest();
198                         exitReq.addClassFilter(referenceType);
199                         exitMethodNames = new HashSet JavaDoc<String JavaDoc>();
200                         exitReq.putProperty("methodNames", exitMethodNames);
201                     }
202                     exitMethodNames.add(method.name());
203                 }
204             }
205         }
206         if (entryReq != null) {
207             addEventRequest(entryReq);
208         }
209         if (exitReq != null) {
210             addEventRequest(exitReq);
211         }
212         if (locationEntry || entryReq != null || exitReq != null) {
213             setValidity(VALIDITY.VALID, null);
214         } else {
215             setValidity(VALIDITY.INVALID,
216                     NbBundle.getMessage(MethodBreakpointImpl.class, "MSG_NoMethod", referenceType.name(), methodName));
217         }
218     }
219 }
220
221
Popular Tags