1 11 package org.eclipse.jdt.internal.debug.core.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 43 public abstract class BreakpointChange extends Change { 44 45 private IJavaBreakpoint fBreakpoint; 46 private String fTypeName; 47 private int fHitCount; 48 private IJavaObject[] fInstanceFilters; 49 private int fSuspendPolicy; 50 private IJavaThread[] fThreadFilters; 51 private boolean fEnabled; 52 53 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 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 90 protected IJavaBreakpoint getOriginalBreakpoint() { 91 return fBreakpoint; 92 } 93 94 100 protected String getOriginalBreakpointTypeName() { 101 return fTypeName; 102 } 103 104 107 public void initializeValidationData(IProgressMonitor pm) { 108 } 110 111 114 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException { 115 return new RefactoringStatus(); 116 } 117 118 121 public Object getModifiedElement() { 122 return fBreakpoint; 123 } 124 125 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 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 160 protected int getLineNumber() { 161 return -1; 162 } 163 164 168 protected int getHitCount() { 169 return fHitCount; 170 } 171 172 178 public static IType getType(IJavaElement parent, String 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 200 public static IJavaElement findElement(IJavaElement parent, IJavaElement element) { 201 List children = getPath(element); 202 List 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 256 private static List getPath(IJavaElement element) { 257 ArrayList children = new ArrayList (); 258 while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) { 259 children.add(element); 260 element = element.getParent(); 261 } 262 return children; 263 } 264 265 271 protected String getBreakpointLabel(IBreakpoint breakpoint) { 272 return JDIDebugUIPlugin.getDefault().getModelPresentation().getText(breakpoint); 273 } 274 } 275 | Popular Tags |