1 11 package org.eclipse.jdt.internal.debug.core.refactoring; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.ILaunchConfiguration; 21 import org.eclipse.jdt.core.IJavaElement; 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.internal.debug.core.JDIDebugPlugin; 27 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 28 import org.eclipse.ltk.core.refactoring.Change; 29 import org.eclipse.ltk.core.refactoring.CompositeChange; 30 31 35 public class JDTDebugRefactoringUtil { 36 37 40 public static Change createChangeFromList(List changes, String changeLabel) { 41 int nbChanges= changes.size(); 42 if (nbChanges == 0) { 43 return null; 44 } 45 else if (nbChanges == 1) { 46 return (Change) changes.get(0); 47 } 48 else { 49 return new CompositeChange(changeLabel, (Change[])changes.toArray(new Change[changes.size()])); 50 } 51 } 52 53 60 protected static String computeNewContainerName(IJavaProject javaProject, ILaunchConfiguration launchConfiguration) { 61 IPath currentLocation = launchConfiguration.getLocation(); 62 IPath projectLocation = javaProject.getProject().getLocation(); 63 if (projectLocation.isPrefixOf(currentLocation)) { 64 String projectFile = new File (projectLocation.toOSString()).getAbsolutePath(); 65 String configDir = new File (currentLocation.toOSString()).getParent(); 66 return new String (configDir.substring(projectFile.length())); 67 } 68 return null; 69 } 70 71 83 protected static Change createChangesForOuterTypeChange(ILaunchConfiguration config, IType type, String newfqname, String pname) throws CoreException { 84 IType[] innerTypes = type.getTypes(); 85 String mtname = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 86 for (int i= 0; i < innerTypes.length; i++) { 87 String newTypeName = newfqname + '$' + innerTypes[i].getElementName(); 88 if (innerTypes[i].getFullyQualifiedName().equals(mtname)) { 90 return new LaunchConfigurationProjectMainTypeChange(config, newTypeName, pname); 91 } 92 return createChangesForOuterTypeChange(config, innerTypes[i], newTypeName, pname); 94 } 95 return null; 96 } 97 98 106 public static Change createChangesForPackageMove(IPackageFragment pfragment, IPackageFragmentRoot destination) throws CoreException { 107 List changes = new ArrayList (); 108 ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(pfragment.getJavaProject().getElementName()); 109 String mtname = null; 110 for (int i= 0; i < configs.length; i++) { 111 mtname = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 112 if(mtname != null) { 113 if(mtname.lastIndexOf(pfragment.getElementName()) > -1) { 114 changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], null, destination.getJavaProject().getElementName())); 115 } 116 } 117 } 118 return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7); 119 } 120 121 129 public static Change createChangesForPackageRename(IPackageFragment pfragment, String newname) throws CoreException { 130 List changes = new ArrayList (); 131 ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(pfragment.getJavaProject().getElementName()); 132 String mtname; 133 for (int i= 0; i < configs.length; i++) { 134 mtname = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 135 if(mtname != null) { 136 String pkname = ""; int index = mtname.lastIndexOf('.'); 138 if(index > 0) { 139 pkname = mtname.substring(0, index); 140 } 141 if (pfragment.getElementName().equals(pkname)) { 142 String ntname = newname + '.' + mtname.substring(index + 1); 143 changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], ntname, null)); 144 } 145 } 146 else { 147 changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], null, null)); 148 } 149 } 150 return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7); 151 } 152 153 161 public static Change createChangesForProjectRename(IJavaProject project, String newname) throws CoreException { 162 List changes = new ArrayList (); 163 ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(project.getElementName()); 164 LaunchConfigurationProjectMainTypeChange change = null; 165 for (int i= 0; i < configs.length; i++) { 166 change = new LaunchConfigurationProjectMainTypeChange(configs[i], null, newname); 167 String newcname = computeNewContainerName(project, configs[i]); 168 if (newcname != null) { 169 change.setNewContainerName(newcname); 170 } 171 changes.add(change); 172 } 173 return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7); 174 } 175 176 185 protected static Change createChangesForTypeChange(IType type, String newfqname, String pname) throws CoreException { 186 List changes = new ArrayList (); 187 String typename = type.getFullyQualifiedName(); 188 ILaunchConfiguration[] configs = getJavaTypeLaunchConfigurations(type.getJavaProject().getElementName()); 189 String mtname; 190 for (int i= 0; i < configs.length; i++) { 191 mtname = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 192 if (typename.equals(mtname)) { 193 changes.add(new LaunchConfigurationProjectMainTypeChange(configs[i], newfqname, pname)); 194 } 195 else { 196 Change change = createChangesForOuterTypeChange(configs[i], type, newfqname, pname); 197 if (change != null) { 198 changes.add(change); 199 } 200 } 201 } 202 return JDTDebugRefactoringUtil.createChangeFromList(changes, RefactoringMessages.LaunchConfigurationProjectMainTypeChange_7); 203 } 204 205 213 public static Change createChangesForTypeMove(IType type, IJavaElement destination) throws CoreException { 214 IJavaProject pdestination = destination.getJavaProject(); 215 String newpname = null; 216 if (!type.getJavaProject().equals(pdestination)) { 217 newpname = pdestination.getElementName(); 218 } 219 String newfqname = type.getElementName(); 220 if (destination instanceof IType) { 221 newfqname = ((IType)destination).getFullyQualifiedName() + '$' + type.getElementName(); 222 } 223 else if (destination instanceof IPackageFragment) { 224 if (!((IPackageFragment) destination).isDefaultPackage()) { 225 newfqname = destination.getElementName() + '.' + type.getElementName(); 226 } 227 } 228 return createChangesForTypeChange(type, newfqname, newpname); 229 } 230 231 239 public static Change createChangesForTypeRename(IType type, String newname) throws CoreException { 240 IType dtype = type.getDeclaringType(); 241 String newfqname = newname; 242 if (dtype == null) { 243 IPackageFragment packageFragment = type.getPackageFragment(); 244 if (!packageFragment.isDefaultPackage()) { 245 newfqname = packageFragment.getElementName() + '.' + newname; 246 } 247 } 248 else { 249 newfqname = dtype.getFullyQualifiedName() + '$' + newname; 250 } 251 return createChangesForTypeChange(type, newfqname, null); 252 } 253 254 260 protected static ILaunchConfiguration[] getJavaTypeLaunchConfigurations(String pname) { 261 try { 262 ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(); 263 ArrayList list = new ArrayList (); 264 String attrib; 265 for(int i = 0; i < configs.length; i++) { 266 attrib = configs[i].getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )null); 267 if(attrib != null) { 268 if(attrib.equals(pname)) { 269 list.add(configs[i]); 270 } 271 } 272 } 273 return (ILaunchConfiguration[])list.toArray(new ILaunchConfiguration[list.size()]); 274 } 275 catch(CoreException e) {JDIDebugPlugin.log(e);} 276 return new ILaunchConfiguration[0]; 277 } 278 279 } 280 | Popular Tags |