KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > refactoring > JavaMethodBreakpointMethodNameChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.core.refactoring;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.IBreakpointManager;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.Signature;
24 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint;
25 import org.eclipse.jdt.debug.core.JDIDebugModel;
26 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
27 import org.eclipse.jdt.internal.debug.ui.actions.ToggleBreakpointAdapter;
28 import org.eclipse.ltk.core.refactoring.Change;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30
31 /**
32  * Change object to used to update a java method breakpoint.
33  */

34 public class JavaMethodBreakpointMethodNameChange extends Change {
35     
36     private IJavaMethodBreakpoint fMethodBreakpoint;
37     private String JavaDoc fNewMethodName;
38     private String JavaDoc fOldMethodName;
39     private IType fDeclaringType;
40     
41     /**
42      * Return the change object to used to update the corresponding java method breakpoint.
43      * Return <code>null</code> if there is no breakpoint for this method.
44      */

45     public static Change createChange(IMethod method, String JavaDoc newName) throws CoreException {
46         IType declaringType= method.getDeclaringType();
47         String JavaDoc typeName= declaringType.getFullyQualifiedName();
48         String JavaDoc methodName= method.getElementName();
49         String JavaDoc methodSignature= ToggleBreakpointAdapter.resolveMethodSignature(declaringType, method.getSignature());
50         IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
51         IBreakpoint[] breakpoints= breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
52         for (int i= 0; i < breakpoints.length; i++) {
53             IBreakpoint breakpoint= breakpoints[i];
54             if (breakpoint instanceof IJavaMethodBreakpoint) {
55                 IJavaMethodBreakpoint methodBreakpoint= (IJavaMethodBreakpoint)breakpoint;
56                 if (typeName.equals(methodBreakpoint.getTypeName()) && methodName.equals(methodBreakpoint.getMethodName()) && methodSignature.equals(methodBreakpoint.getMethodSignature())) {
57                     return new JavaMethodBreakpointMethodNameChange(methodBreakpoint, newName);
58                 }
59             }
60         }
61         return null;
62     }
63     
64     protected JavaMethodBreakpointMethodNameChange(IJavaMethodBreakpoint methodBreakpoint, String JavaDoc newName) throws CoreException {
65         fMethodBreakpoint= methodBreakpoint;
66         fNewMethodName= newName;
67         fOldMethodName= fMethodBreakpoint.getMethodName();
68         fDeclaringType= BreakpointUtils.getType(methodBreakpoint);
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#getName()
73      */

74     public String JavaDoc getName() {
75         return MessageFormat.format(RefactoringMessages.JavaMethodBreakpointMethodNameChange_0, new String JavaDoc[] {fDeclaringType.getElementName(), fOldMethodName}); //$NON-NLS-1$
76
}
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
80      */

81     public void initializeValidationData(IProgressMonitor pm) {
82     }
83
84     /* (non-Javadoc)
85      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
86      */

87     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
88         RefactoringStatus status= new RefactoringStatus();
89         if (!fMethodBreakpoint.isRegistered()) {
90             status.addFatalError(MessageFormat.format(RefactoringMessages.JavaMethodBreakpointMethodNameChange_1, new String JavaDoc[] {fDeclaringType.getElementName(), fOldMethodName})); //$NON-NLS-1$
91
}
92         return status;
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
97      */

98     public Change perform(IProgressMonitor pm) throws CoreException {
99         String JavaDoc methodSignature= fMethodBreakpoint.getMethodSignature();
100         String JavaDoc[] parameterTypes= Signature.getParameterTypes(methodSignature);
101         for (int i= 0; i < parameterTypes.length; i++) {
102             parameterTypes[i]= parameterTypes[i].replace('/', '.');
103         }
104         IMethod method= fDeclaringType.getMethod(fNewMethodName, parameterTypes);
105         Map JavaDoc attributes= fMethodBreakpoint.getMarker().getAttributes();
106         BreakpointUtils.addJavaBreakpointAttributes(attributes, method);
107         boolean isEnable= fMethodBreakpoint.isEnabled();
108         IJavaMethodBreakpoint newMethodBreakpoint= JDIDebugModel.createMethodBreakpoint(
109                 fMethodBreakpoint.getMarker().getResource(),
110                 fMethodBreakpoint.getTypeName(),
111                 fNewMethodName,
112                 methodSignature,
113                 fMethodBreakpoint.isEntry(),
114                 fMethodBreakpoint.isExit(),
115                 fMethodBreakpoint.isNativeOnly(),
116                 fMethodBreakpoint.getLineNumber(),
117                 fMethodBreakpoint.getCharStart(),
118                 fMethodBreakpoint.getCharEnd(),
119                 fMethodBreakpoint.getHitCount(),
120                 true,
121                 attributes);
122         newMethodBreakpoint.setEnabled(isEnable);
123         fMethodBreakpoint.delete();
124         return new JavaMethodBreakpointMethodNameChange(newMethodBreakpoint, fOldMethodName);
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#getModifiedElement()
129      */

130     public Object JavaDoc getModifiedElement() {
131         return fMethodBreakpoint;
132     }
133
134 }
135
Popular Tags