KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.OperationCanceledException;
19 import org.eclipse.debug.core.model.IBreakpoint;
20 import org.eclipse.jdt.core.IClassFile;
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IImportContainer;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IMember;
25 import org.eclipse.jdt.core.IMethod;
26 import org.eclipse.jdt.core.ISourceRange;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
30 import org.eclipse.jdt.debug.core.IJavaObject;
31 import org.eclipse.jdt.debug.core.IJavaThread;
32 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
33 import org.eclipse.jface.text.BadLocationException;
34 import org.eclipse.jface.text.Document;
35 import org.eclipse.ltk.core.refactoring.Change;
36 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
37
38 /**
39  * An abstract change for breakpoint type refactoring changes
40  * @since 3.2
41  *
42  */

43 public abstract class BreakpointChange extends Change {
44     
45     private IJavaBreakpoint fBreakpoint;
46     private String JavaDoc fTypeName;
47     private int fHitCount;
48     private IJavaObject[] fInstanceFilters;
49     private int fSuspendPolicy;
50     private IJavaThread[] fThreadFilters;
51     private boolean fEnabled;
52
53     /**
54      * Constructor
55      * @param breakpoint
56      * @throws CoreException
57      */

58     public BreakpointChange(IJavaBreakpoint breakpoint) throws CoreException {
59         fBreakpoint = breakpoint;
60         fTypeName = breakpoint.getTypeName();
61         fHitCount = breakpoint.getHitCount();
62         fInstanceFilters = breakpoint.getInstanceFilters();
63         fSuspendPolicy = breakpoint.getSuspendPolicy();
64         fThreadFilters = breakpoint.getThreadFilters();
65         fEnabled = breakpoint.isEnabled();
66     }
67     
68     /**
69      * Applies the original attributes to the new breakpoint
70      *
71      * @param breakpoint the new breakpoint
72      * @throws CoreException
73      */

74     protected void apply(IJavaBreakpoint breakpoint) throws CoreException {
75         breakpoint.setHitCount(fHitCount);
76         for (int i = 0; i < fInstanceFilters.length; i++) {
77             breakpoint.addInstanceFilter(fInstanceFilters[i]);
78         }
79         breakpoint.setSuspendPolicy(fSuspendPolicy);
80         for (int i = 0; i < fThreadFilters.length; i++) {
81             breakpoint.setThreadFilter(fThreadFilters[i]);
82         }
83         breakpoint.setEnabled(fEnabled);
84     }
85     
86     /**
87      * Returns the original breakpoints prior to the change
88      * @return the original breakpoint prior to the change
89      */

90     protected IJavaBreakpoint getOriginalBreakpoint() {
91         return fBreakpoint;
92     }
93     
94     /**
95      * Returns the original name of the type the associated breakpoint was set on.
96      * This can be different than the type being changed.
97      *
98      * @return
99      */

100     protected String JavaDoc getOriginalBreakpointTypeName() {
101         return fTypeName;
102     }
103     
104     /* (non-Javadoc)
105      * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
106      */

107     public void initializeValidationData(IProgressMonitor pm) {
108         // do nothing
109
}
110     
111     /* (non-Javadoc)
112      * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
113      */

114     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
115         return new RefactoringStatus();
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
120      */

121     public Object JavaDoc getModifiedElement() {
122         return fBreakpoint;
123     }
124     
125     /**
126      * Returns an array of ints representing the new line number, char start and char end
127      * of the member.
128      *
129      * @param member
130      * @return array of 3 ints or <code>null</code>
131      */

132     protected int[] getNewLineNumberAndRange(IMember member) throws CoreException {
133         ISourceRange nameRange = member.getNameRange();
134         int offset = nameRange.getOffset();
135         int lineNumber = getNewLineNumber(member, offset);
136         return new int[]{lineNumber, offset, offset + nameRange.getLength()};
137     }
138     
139     /**
140      * Returns the new line number of the from the source of the specified member's compilation unit
141      * @param member the member to query
142      * @param offset the offset
143      * @return the new line number
144      * @throws JavaModelException
145      */

146     private int getNewLineNumber(IMember member, int offset) throws JavaModelException {
147         int lineNumber = getLineNumber();
148         Document document = new Document(member.getCompilationUnit().getSource());
149         try {
150             lineNumber = document.getLineOfOffset(offset);
151         } catch (BadLocationException e) {
152         }
153         return lineNumber;
154     }
155     
156     /**
157      * Return the line number for the breakpoint
158      * @return the line number for the breakpoint
159      */

160     protected int getLineNumber() {
161         return -1;
162     }
163     
164     /**
165      * Returns the hit count for the breakpoint
166      * @return the hit count for the breakpoint
167      */

168     protected int getHitCount() {
169         return fHitCount;
170     }
171     
172     /**
173      * Returns the <code>IType</code> within the specified parent type given by simpleName
174      * @param parent
175      * @param simpleName
176      * @return the <code>IType</code> within the specified parent type given by simpleName
177      */

178     public static IType getType(IJavaElement parent, String JavaDoc simpleName) {
179         switch (parent.getElementType()) {
180             case IJavaElement.COMPILATION_UNIT:
181                 return ((ICompilationUnit)parent).getType(simpleName);
182             case IJavaElement.TYPE:
183                 return ((IType)parent).getType(simpleName);
184             case IJavaElement.FIELD:
185             case IJavaElement.INITIALIZER:
186             case IJavaElement.METHOD:
187                 return ((IMember)parent).getType(simpleName, -1);
188         }
189         return null;
190     }
191     
192     /**
193      * Returns the <code>IJavaElement</code> contained within the specified parent one, or the parent one
194      * by default
195      * @param parent
196      * @param element
197      * @return the <code>IJavaElement</code> contained within the specified parent one, or the parent one
198      * by default
199      */

200     public static IJavaElement findElement(IJavaElement parent, IJavaElement element) {
201         List JavaDoc children = getPath(element);
202         List JavaDoc path = getPath(parent);
203         IJavaElement currentElement = parent;
204         for (int i = children.size() - path.size() - 1; i >= 0; i--) {
205             IJavaElement child = (IJavaElement)children.get(i);
206             switch (child.getElementType()) {
207                 case IJavaElement.PACKAGE_DECLARATION:
208                     currentElement = ((ICompilationUnit)currentElement).getPackageDeclaration(child.getElementName());
209                     break;
210                 case IJavaElement.IMPORT_CONTAINER:
211                     currentElement = ((ICompilationUnit)currentElement).getImportContainer();
212                     break;
213                 case IJavaElement.IMPORT_DECLARATION:
214                     currentElement = ((IImportContainer)currentElement).getImport(child.getElementName());
215                     break;
216                 case IJavaElement.TYPE:
217                     switch (currentElement.getElementType()) {
218                         case IJavaElement.COMPILATION_UNIT:
219                             currentElement = ((ICompilationUnit)currentElement).getType(child.getElementName());
220                             break;
221                         case IJavaElement.CLASS_FILE:
222                             currentElement = ((IClassFile)currentElement).getType();
223                             break;
224                         case IJavaElement.TYPE:
225                             currentElement = ((IType)currentElement).getType(child.getElementName());
226                             break;
227                         case IJavaElement.FIELD:
228                         case IJavaElement.INITIALIZER:
229                         case IJavaElement.METHOD:
230                             currentElement = ((IMember)currentElement).getType(child.getElementName(), ((IMember)child).getOccurrenceCount());
231                             break;
232                     }
233                     break;
234                 case IJavaElement.INITIALIZER:
235                     currentElement = ((IType)currentElement).getInitializer(((IMember)child).getOccurrenceCount());
236                     break;
237                 case IJavaElement.FIELD:
238                     currentElement = ((IType)currentElement).getField(child.getElementName());
239                     break;
240                 case IJavaElement.METHOD:
241                     currentElement = ((IType)currentElement).getMethod(child.getElementName(), ((IMethod)child).getParameterTypes());
242                     break;
243             }
244             
245         }
246         return currentElement;
247     }
248     
249     /**
250      * Returns the path of the given element up to but not including its compilation unit,
251      * in bottom up order.
252      *
253      * @param element
254      * @return element's path
255      */

256     private static List JavaDoc getPath(IJavaElement element) {
257         ArrayList JavaDoc children = new ArrayList JavaDoc();
258         while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
259             children.add(element);
260             element = element.getParent();
261         }
262         return children;
263     }
264     
265     /**
266      * Returns a label for the given breakpoint generated from the JDI model presentation.
267      *
268      * @param breakpoint a breakpoint
269      * @return standard label for the breakpoint
270      */

271     protected String JavaDoc getBreakpointLabel(IBreakpoint breakpoint) {
272         return JDIDebugUIPlugin.getDefault().getModelPresentation().getText(breakpoint);
273     }
274 }
275
Popular Tags