1 11 package org.eclipse.jdt.internal.launching; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 24 import org.eclipse.debug.core.sourcelookup.containers.DirectorySourceContainer; 25 import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer; 26 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer; 27 import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer; 28 import org.eclipse.jdt.core.IJavaModel; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.IPackageFragmentRoot; 31 import org.eclipse.jdt.core.JavaCore; 32 import org.eclipse.jdt.core.JavaModelException; 33 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 34 import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer; 35 import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer; 36 37 43 public class JavaSourceLookupUtil { 44 45 52 public static ISourceContainer[] translate(IRuntimeClasspathEntry[] entries) { 53 List containers = new ArrayList (entries.length); 54 for (int i = 0; i < entries.length; i++) { 55 IRuntimeClasspathEntry entry = entries[i]; 56 switch (entry.getType()) { 57 case IRuntimeClasspathEntry.ARCHIVE: 58 IPackageFragmentRoot root = getPackageFragmentRoot(entry); 59 if (root == null) { 60 String path = entry.getSourceAttachmentLocation(); 61 ISourceContainer container = null; 62 if (path == null) { 63 path = entry.getLocation(); 65 } 66 if (path != null) { 67 File file = new File (path); 69 if (file.isDirectory()) { 70 IResource resource = entry.getResource(); 71 if (resource instanceof IContainer) { 72 container = new FolderSourceContainer((IContainer)resource, false); 73 } else { 74 container = new DirectorySourceContainer(file, false); 75 } 76 } else { 77 container = new ExternalArchiveSourceContainer(path, true); 78 } 79 if (!containers.contains(container)) { 80 containers.add(container); 81 } 82 } 83 } else { 84 ISourceContainer container = new PackageFragmentRootSourceContainer(root); 85 if (!containers.contains(container)) { 86 containers.add(container); 87 } 88 } 89 break; 90 case IRuntimeClasspathEntry.PROJECT: 91 IResource resource = entry.getResource(); 92 if (resource != null && resource.getType() == IResource.PROJECT) { 93 IJavaProject javaProject = JavaCore.create((IProject)resource); 94 ISourceContainer container = null; 95 if (javaProject.exists()) { 96 container = new JavaProjectSourceContainer(javaProject); 97 } else if (resource.exists()) { 98 container = new ProjectSourceContainer((IProject)resource, false); 99 } 100 if (container != null && !containers.contains(container)) { 101 containers.add(container); 102 } 103 } 104 break; 105 default: 106 break; 108 } 109 } 110 return (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]); 111 } 112 113 128 private static boolean isSourceAttachmentEqual(IPackageFragmentRoot root, IRuntimeClasspathEntry entry) throws JavaModelException { 129 IPath entryPath = entry.getSourceAttachmentPath(); 130 if (entryPath == null) { 131 return true; 132 } 133 IPath rootPath = root.getSourceAttachmentPath(); 134 if (rootPath == null) { 135 return false; 137 } 138 return rootPath.equals(entryPath); 139 140 } 141 142 150 private static IPackageFragmentRoot getPackageFragmentRoot(IRuntimeClasspathEntry entry) { 151 IResource resource = entry.getResource(); 152 if (resource == null) { 153 IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 157 try { 158 IJavaProject[] jps = model.getJavaProjects(); 159 for (int i = 0; i < jps.length; i++) { 160 IJavaProject jp = jps[i]; 161 IProject p = jp.getProject(); 162 if (p.isOpen()) { 163 IPackageFragmentRoot[] allRoots = jp.getPackageFragmentRoots(); 164 for (int j = 0; j < allRoots.length; j++) { 165 IPackageFragmentRoot root = allRoots[j]; 166 if (root.isExternal() && root.getPath().equals(new Path(entry.getLocation()))) { 167 if (isSourceAttachmentEqual(root, entry)) { 168 return root; 170 } 171 } 172 } 173 } 174 } 175 } catch (JavaModelException e) { 176 LaunchingPlugin.log(e); 177 } 178 } else { 179 IProject project = resource.getProject(); 181 IJavaProject jp = JavaCore.create(project); 182 try { 183 if (project.isOpen() && jp.exists()) { 184 IPackageFragmentRoot root = jp.getPackageFragmentRoot(resource); 185 IPackageFragmentRoot[] allRoots = jp.getPackageFragmentRoots(); 186 for (int j = 0; j < allRoots.length; j++) { 187 if (allRoots[j].equals(root)) { 188 if (isSourceAttachmentEqual(root, entry)) { 190 return root; 192 } 193 } 194 } 195 196 } 197 IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 200 IJavaProject[] jps = model.getJavaProjects(); 201 for (int i = 0; i < jps.length; i++) { 202 IJavaProject jp1 = jps[i]; 203 IProject p = jp1.getProject(); 204 if (p.isOpen()) { 205 IPackageFragmentRoot[] allRoots = jp1.getPackageFragmentRoots(); 206 for (int j = 0; j < allRoots.length; j++) { 207 IPackageFragmentRoot root = allRoots[j]; 208 if (!root.isExternal() && root.getPath().equals(entry.getPath())) { 209 if (isSourceAttachmentEqual(root, entry)) { 210 return root; 212 } 213 } 214 } 215 } 216 } 217 } catch (JavaModelException e) { 218 LaunchingPlugin.log(e); 219 } 220 } 221 return null; 222 } 223 } 224 | Popular Tags |