1 11 package org.eclipse.jdt.internal.launching; 12 13 import org.eclipse.core.resources.IProject; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.ResourcesPlugin; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.debug.core.ILaunchConfiguration; 18 import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate; 19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 20 import org.eclipse.jdt.core.IJavaProject; 21 import org.eclipse.jdt.core.IType; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 24 25 32 public class JavaMigrationDelegate implements ILaunchConfigurationMigrationDelegate { 33 34 37 protected static final String EMPTY_STRING = ""; 39 42 public JavaMigrationDelegate() {} 43 44 47 public boolean isCandidate(ILaunchConfiguration candidate) throws CoreException { 48 String pName = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING); 49 if(pName.equals(EMPTY_STRING)) { 50 return false; 51 } 52 if (!isAvailable(pName)) { 53 return false; 54 } 55 IResource[] mapped = candidate.getMappedResources(); 56 IResource target = getResource(candidate); 57 if (target == null) { 58 return mapped != null; 59 } else { 60 if (mapped == null) { 61 return true; 62 } else { 63 if (mapped.length != 1) { 64 return true; 65 } else { 66 return !target.equals(mapped[0]); 67 } 68 } 69 } 70 } 71 72 78 private boolean isAvailable(String projectName) { 79 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); 80 return project.exists() && project.isOpen(); 81 } 82 83 94 public static IResource getResource(ILaunchConfiguration candidate) throws CoreException { 95 IResource resource = null; 96 String pname = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING); 97 if(!EMPTY_STRING.equals(pname)) { 98 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(pname); 99 String tname = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, EMPTY_STRING); 100 if(!EMPTY_STRING.equals(tname)) { 101 if(project != null && project.exists() && project.isOpen()) { 102 IJavaProject jproject = JavaCore.create(project); 103 if(jproject != null && jproject.exists()) { 104 tname = tname.replace('$', '.'); 105 IType type = jproject.findType(tname); 106 if(type != null) { 107 resource = type.getUnderlyingResource(); 108 if(resource == null) { 109 resource = (IResource) type.getAdapter(IResource.class); 110 } 111 } 112 } 113 } 114 } else { 115 return project; 116 } 117 if (resource == null) { 118 resource = project; 119 } 120 } 121 return resource; 122 } 123 124 127 public void migrate(ILaunchConfiguration candidate) throws CoreException { 128 ILaunchConfigurationWorkingCopy wc = candidate.getWorkingCopy(); 129 updateResourceMapping(wc); 130 wc.doSave(); 131 } 132 133 139 public static void updateResourceMapping(ILaunchConfigurationWorkingCopy wc) throws CoreException { 140 IResource resource = getResource(wc); 141 IResource[] resources = null; 142 if (resource != null) { 143 resources = new IResource[]{resource}; 144 } 145 wc.setMappedResources(resources); 146 } 147 148 } 149 | Popular Tags |