KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.model.IBreakpoint;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
28 import org.eclipse.jdt.debug.core.IJavaClassPrepareBreakpoint;
29 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint;
30 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
31 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint;
32 import org.eclipse.jdt.debug.core.IJavaWatchpoint;
33 import org.eclipse.jdt.debug.core.JDIDebugModel;
34 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
35 import org.eclipse.ltk.core.refactoring.Change;
36 import org.eclipse.ltk.core.refactoring.NullChange;
37 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
38
39
40 /**
41  * Abtract change to update a breakpoint when a IType is moved or renamed.
42  */

43 public abstract class JavaBreakpointTypeChange extends Change {
44     
45     public static final int TYPE_RENAME= 1;
46     public static final int TYPE_MOVE= 2;
47     public static final int PROJECT_RENAME= 3;
48     public static final int PACKAGE_RENAME= 4;
49     public static final int PACKAGE_MOVE= 5;
50     
51     private IJavaBreakpoint fBreakpoint;
52     private Object JavaDoc fChangedElement;
53     private Object JavaDoc fArgument;
54     private int fChangeType;
55     private IType fDeclaringType;
56     private boolean fIsEnable;
57     private Map JavaDoc fAttributes;
58     private int fHitCount;
59     
60     /**
61      * Create changes for each breakpoint which needs to be updated for this IType rename.
62      */

63     public static Change createChangesForTypeRename(IType type, String JavaDoc newName) throws CoreException {
64         return createChangesForTypeChange(type, newName, TYPE_RENAME);
65     }
66     
67     /**
68      * Create changes for each breakpoint which needs to be updated for this IType move.
69      */

70     public static Change createChangesForTypeMove(IType type, Object JavaDoc destination) throws CoreException {
71         return createChangesForTypeChange(type, destination, TYPE_MOVE);
72     }
73
74     /**
75      * Create a change for each breakpoint which needs to be updated for this IJavaProject rename.
76      */

77     public static Change createChangesForProjectRename(IJavaProject project, String JavaDoc newName) throws CoreException {
78         List JavaDoc changes= new ArrayList JavaDoc();
79         IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(JDIDebugModel.getPluginIdentifier());
80         for (int i= 0; i < breakpoints.length; i++) {
81             IBreakpoint breakpoint= breakpoints[i];
82             if (breakpoint instanceof IJavaBreakpoint) {
83                 IJavaBreakpoint javaBreakpoint= (IJavaBreakpoint) breakpoint;
84                 IType breakpointType= BreakpointUtils.getType(javaBreakpoint);
85                 if (breakpointType != null && project.equals(breakpointType.getJavaProject())) {
86                     changes.add(createChange(javaBreakpoint, null, newName, PROJECT_RENAME));
87                 }
88             }
89         }
90         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.JavaBreakpointTypeChange_0); //$NON-NLS-1$
91
}
92     
93     /**
94      * Create a change for each breakpoint which needs to be updated for this IPackageFragment rename.
95      */

96     public static Change createChangesForPackageRename(IPackageFragment packageFragment, String JavaDoc newName) throws CoreException {
97         return createChangesForPackageChange(packageFragment, newName, PACKAGE_RENAME);
98     }
99
100     /**
101      * Create a change for each breakponit which needs to be updated for this IPackageFragment move.
102      */

103     public static Change createChangesForPackageMove(IPackageFragment packageFragment, IPackageFragmentRoot destination) throws CoreException {
104         return createChangesForPackageChange(packageFragment, destination, PACKAGE_MOVE);
105     }
106     
107     /**
108      * Create changes for each breakpoint which need to be updated for this IType change.
109      */

110     private static Change createChangesForTypeChange(IType changedType, Object JavaDoc argument, int changeType) throws CoreException {
111         List JavaDoc changes= new ArrayList JavaDoc();
112
113         IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(JDIDebugModel.getPluginIdentifier());
114         String JavaDoc typeName= changedType.getFullyQualifiedName();
115         for (int i= 0; i < breakpoints.length; i++) {
116             // for each breakpoint
117
IBreakpoint breakpoint= breakpoints[i];
118             if (breakpoint instanceof IJavaBreakpoint) {
119                 IJavaBreakpoint javaBreakpoint= (IJavaBreakpoint) breakpoint;
120                 IType breakpointType= BreakpointUtils.getType(javaBreakpoint);
121                 // check the name of the type where the breakpoint is installed
122
if (breakpointType != null && javaBreakpoint.getTypeName().startsWith(typeName)) {
123                     // if it matcheds, check the type
124
if (changedType.equals(breakpointType)) {
125                         changes.add(createChange(javaBreakpoint, changedType, argument, changeType));
126                     } else {
127                         // if it's not the type, check the inner types
128
Change change= createChangesForOuterTypeChange(javaBreakpoint, changedType, changedType, argument, changeType);
129                         if (change != null) {
130                             changes.add(change);
131                         }
132                     }
133                 }
134             }
135         }
136                 
137         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.JavaBreakpointTypeChange_0); //$NON-NLS-1$
138
}
139     
140     private static Change createChangesForOuterTypeChange(IJavaBreakpoint javaBreakpoint, IType type, IType changedType, Object JavaDoc argument, int changeType) throws CoreException {
141         IType[] innerTypes= type.getTypes();
142         String JavaDoc breakpointTypeName= javaBreakpoint.getTypeName();
143         IType breakpointType= BreakpointUtils.getType(javaBreakpoint);
144         for (int i= 0; i < innerTypes.length; i++) {
145             IType innerType= innerTypes[i];
146             // check the name of the type where the breakpoint is installed
147
if (breakpointTypeName.startsWith(innerType.getFullyQualifiedName())) {
148                 // if it matcheds, check the type
149
if (innerType.equals(breakpointType)) {
150                     return createChange(javaBreakpoint, changedType, argument, changeType);
151                 }
152                 // if it's not the type, check the inner types
153
return createChangesForOuterTypeChange(javaBreakpoint, innerType, changedType, argument, changeType);
154             }
155             
156         }
157         return null;
158     }
159
160     /**
161      * Create a change for each breakpoint which needs to be updated for this IPackageFragment change.
162      */

163     private static Change createChangesForPackageChange(IPackageFragment packageFragment, Object JavaDoc argument, int changeType) throws CoreException {
164         List JavaDoc changes= new ArrayList JavaDoc();
165         IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(JDIDebugModel.getPluginIdentifier());
166         for (int i= 0; i < breakpoints.length; i++) {
167             IBreakpoint breakpoint= breakpoints[i];
168             if (breakpoint instanceof IJavaBreakpoint) {
169                 IJavaBreakpoint javaBreakpoint= (IJavaBreakpoint) breakpoint;
170                 IType breakpointType= BreakpointUtils.getType(javaBreakpoint);
171                 if (breakpointType != null && packageFragment.equals(breakpointType.getPackageFragment())) {
172                     changes.add(createChange(javaBreakpoint, packageFragment, argument, changeType));
173                 }
174             }
175         }
176         return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.JavaBreakpointTypeChange_0); //$NON-NLS-1$
177
}
178     
179     /**
180      * Create a change according to type of the breakpoint.
181      */

182     private static Change createChange(IJavaBreakpoint javaBreakpoint, Object JavaDoc changedElement, Object JavaDoc argument, int changeType) throws CoreException {
183         if (javaBreakpoint instanceof IJavaClassPrepareBreakpoint) {
184             return new JavaClassPrepareBreakpointTypeChange((IJavaClassPrepareBreakpoint) javaBreakpoint, changedElement, argument, changeType);
185         } else if (javaBreakpoint instanceof IJavaExceptionBreakpoint) {
186             return new JavaExceptionBreakpointTypeChange((IJavaExceptionBreakpoint) javaBreakpoint, changedElement, argument, changeType);
187         } else if (javaBreakpoint instanceof IJavaMethodBreakpoint) {
188             return new JavaMethodBreakpointTypeChange((IJavaMethodBreakpoint) javaBreakpoint, changedElement, argument, changeType);
189         } else if (javaBreakpoint instanceof IJavaWatchpoint) {
190             return new JavaWatchpointTypeChange((IJavaWatchpoint) javaBreakpoint, changedElement, argument, changeType);
191         } else if (javaBreakpoint instanceof IJavaLineBreakpoint) {
192             return new JavaLineBreakpointTypeChange((IJavaLineBreakpoint) javaBreakpoint, changedElement, argument, changeType);
193         } else {
194             return null;
195         }
196     }
197
198     /**
199      * JavaBreakpointTypeChange constructor.
200      */

201     protected JavaBreakpointTypeChange(IJavaBreakpoint breakpoint, Object JavaDoc changedElement, Object JavaDoc argument, int changeType) throws CoreException {
202         fBreakpoint= breakpoint;
203         fChangedElement= changedElement;
204         fArgument= argument;
205         fChangeType= changeType;
206         fDeclaringType= BreakpointUtils.getType(breakpoint);
207         fAttributes= breakpoint.getMarker().getAttributes();
208         fIsEnable= breakpoint.isEnabled();
209         fHitCount= breakpoint.getHitCount();
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
214      */

215     public void initializeValidationData(IProgressMonitor pm) {
216     }
217
218     /* (non-Javadoc)
219      * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
220      */

221     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
222         RefactoringStatus status= new RefactoringStatus();
223         if (!fBreakpoint.isRegistered()) {
224             status.addFatalError(getErrorMessageNoMoreExists());
225         }
226         return status;
227     }
228
229     /* (non-Javadoc)
230      * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
231      */

232     public Change perform(IProgressMonitor pm) throws CoreException {
233         switch (fChangeType) {
234             case TYPE_RENAME:
235                 return performTypeRename();
236             case TYPE_MOVE:
237                 return performTypeMove();
238             case PROJECT_RENAME:
239                 return performProjectRename();
240             case PACKAGE_RENAME:
241                 return performPackageRename();
242             case PACKAGE_MOVE:
243                 return performPackageMove();
244         }
245         return null;
246     }
247     
248     private Change performTypeRename() throws CoreException {
249         // Get the new type and the new 'changed' type then call the code specific to this type
250
// of breakpoint.
251
IType changedType= getChangedType();
252         String JavaDoc oldChangedTypeName= changedType.getFullyQualifiedName('.');
253         String JavaDoc newChangedTypeName;
254         IType parent= changedType.getDeclaringType();
255         if (parent == null) {
256             newChangedTypeName= changedType.getPackageFragment().getElementName() + '.' + getNewName();
257         } else {
258             newChangedTypeName= parent.getFullyQualifiedName('.') + '.' + getNewName();
259         }
260         
261         IType newChangedType;
262         IType newType;
263         IJavaProject project= fDeclaringType.getJavaProject();
264         if (changedType.equals(fDeclaringType)) {
265             newType= project.findType(newChangedTypeName);
266             newChangedType= newType;
267         } else {
268             String JavaDoc typeNameSuffix= fDeclaringType.getFullyQualifiedName('.').substring(oldChangedTypeName.length());
269             String JavaDoc newTypeName= newChangedTypeName + typeNameSuffix;
270             newType= project.findType(newTypeName);
271             newChangedType= project.findType(newChangedTypeName);
272         }
273         
274         /*return*/ performChange(newType, newChangedType, changedType.getElementName(), TYPE_RENAME);
275         return new NullChange();
276     }
277     
278     private Change performTypeMove() throws CoreException {
279         // Get the new type and the new 'changed' type then call the code specific to this type
280
// of breakpoint.
281
IType changedType= getChangedType();
282         Object JavaDoc destination= getDestination();
283         String JavaDoc newChangedTypeName;
284         IJavaProject project;
285         if (destination instanceof IPackageFragment) {
286             IPackageFragment packageDestination= (IPackageFragment) destination;
287             project= packageDestination.getJavaProject();
288             if (packageDestination.isDefaultPackage()) {
289                 newChangedTypeName= changedType.getElementName();
290             } else {
291                 newChangedTypeName= ((IPackageFragment)destination).getElementName() + '.' + changedType.getElementName();
292             }
293         } else {
294             IType type = (IType)destination;
295             newChangedTypeName= (type).getFullyQualifiedName('.') + '.' + changedType.getElementName();
296             project= type.getJavaProject();
297         }
298         
299         IType newChangedType;
300         IType newType;
301         if (changedType == fDeclaringType) {
302             newType= project.findType(newChangedTypeName);
303             newChangedType= newType;
304         } else {
305             String JavaDoc oldChangedTypeName= changedType.getFullyQualifiedName('.');
306             String JavaDoc typeNameSuffix= fDeclaringType.getFullyQualifiedName('.').substring(oldChangedTypeName.length());
307             String JavaDoc newTypeName= newChangedTypeName + typeNameSuffix;
308             newType= project.findType(newTypeName);
309             newChangedType= project.findType(newChangedTypeName);
310         }
311         
312         Object JavaDoc oldDestination= changedType.getDeclaringType();
313         if (oldDestination == null) {
314             oldDestination= changedType.getPackageFragment();
315         }
316         
317         /*return*/ performChange(newType, newChangedType, oldDestination, TYPE_MOVE);
318         return new NullChange();
319     }
320     
321     private Change performProjectRename() throws CoreException {
322         // Get the new type, then call the code specific to this type of breakpoint.
323
IJavaProject project= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(getNewName()));
324         IType newType= project.findType(fDeclaringType.getFullyQualifiedName('.'));
325         /*return*/ performChange(newType, null, fDeclaringType.getJavaProject().getElementName(), PROJECT_RENAME);
326         return new NullChange();
327     }
328     
329     private Change performPackageRename() throws CoreException {
330         // Get the new type and the new package fragment, then call the code specific
331
// to this type of breakpoint.
332
IPackageFragment changedPackage= getChangePackage();
333         IJavaProject project= fDeclaringType.getJavaProject();
334         String JavaDoc newTypeName= getNewName() + fDeclaringType.getFullyQualifiedName('.').substring(changedPackage.getElementName().length());
335         IType newType= project.findType(newTypeName);
336         /*return*/ performChange(newType, newType.getPackageFragment(), changedPackage.getElementName(), PACKAGE_RENAME);
337         return new NullChange();
338     }
339     
340     private Change performPackageMove() throws CoreException {
341         IPackageFragmentRoot destination= getPackageRootDestination();
342         IPackageFragment changedPackage= getChangePackage();
343         IJavaProject project= destination.getJavaProject();
344         IType newType= project.findType(fDeclaringType.getFullyQualifiedName('.'));
345         /*return*/ performChange(newType, newType.getPackageFragment(), changedPackage.getParent(), PROJECT_RENAME);
346         return new NullChange();
347     }
348
349     /* (non-Javadoc)
350      * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
351      */

352     public Object JavaDoc getModifiedElement() {
353         return getBreakpoint();
354     }
355
356     /**
357      * Return the breakpoint modified in this change.
358      */

359     public IJavaBreakpoint getBreakpoint() {
360         return fBreakpoint;
361     }
362     
363     /**
364      * Return the new name of the changed type for a IType, IJavaProject
365      * or IPackageFragment rename change.
366      */

367     public String JavaDoc getNewName() {
368         if (fChangeType == TYPE_RENAME || fChangeType == PROJECT_RENAME || fChangeType == PACKAGE_RENAME) {
369             return (String JavaDoc)fArgument;
370         }
371         return null;
372     }
373     
374     /**
375      * Return the destination for a IType move change.
376      */

377     private Object JavaDoc getDestination() {
378         if (fChangeType == TYPE_MOVE) {
379             return fArgument;
380         }
381         return null;
382     }
383     
384     /**
385      * Return the destination for a IPackageFragment move change.
386      */

387     private IPackageFragmentRoot getPackageRootDestination() {
388         if (fChangeType == PACKAGE_MOVE) {
389             return (IPackageFragmentRoot)fArgument;
390         }
391         return null;
392     }
393
394     /**
395      * Return the original declaring type of the breakpoint.
396      */

397     public IType getDeclaringType() {
398         return fDeclaringType;
399     }
400
401     /**
402      * Return the type modified.
403      */

404     public IType getChangedType() {
405         if (fChangeType == TYPE_RENAME || fChangeType == TYPE_MOVE) {
406             return (IType) fChangedElement;
407         }
408         return null;
409     }
410
411     /**
412      * Return the package modified.
413      */

414     public IPackageFragment getChangePackage() {
415         if (fChangeType == PACKAGE_RENAME || fChangeType == PACKAGE_MOVE) {
416             return (IPackageFragment) fChangedElement;
417         }
418         return null;
419     }
420     
421     /**
422      * Return the enable state of the breakpoint.
423      */

424     public boolean getEnable() {
425         return fIsEnable;
426     }
427     
428     /**
429      * Return the attributes map of the breakpoint.
430      */

431     public Map JavaDoc getAttributes() {
432         return fAttributes;
433     }
434     
435     /**
436      * Return the hit count of the breakpoint.
437      */

438     public int getHitCount() {
439         return fHitCount;
440     }
441     
442     /**
443      * Return the message to use if the breakpoint no more exists (used in #isValid()).
444      */

445     public abstract String JavaDoc getErrorMessageNoMoreExists();
446     
447     /**
448      * Perform the real modifications.
449      * @return the undo change.
450      */

451     public abstract Change performChange(IType newType, Object JavaDoc undoChangedElement, Object JavaDoc undoArgument, int changeType) throws CoreException;
452
453 }
454
Popular Tags