View | Details | Raw Unified | Return to bug 182238
Collapse All | Expand All

(-)src/org/eclipse/pde/internal/ui/launcher/PDESourceLookupQuery.java (-132 / +4 lines)
Lines 11-53 Link Here
11
package org.eclipse.pde.internal.ui.launcher;
11
package org.eclipse.pde.internal.ui.launcher;
12
12
13
import java.io.File;
13
import java.io.File;
14
import java.util.ArrayList;
15
14
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IProject;
18
import org.eclipse.core.resources.IResource;
19
import org.eclipse.core.resources.IWorkspaceRoot;
20
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IPath;
22
import org.eclipse.core.runtime.ISafeRunnable;
16
import org.eclipse.core.runtime.ISafeRunnable;
23
import org.eclipse.core.runtime.Path;
24
import org.eclipse.debug.core.DebugException;
17
import org.eclipse.debug.core.DebugException;
25
import org.eclipse.debug.core.model.IValue;
18
import org.eclipse.debug.core.model.IValue;
26
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
19
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
27
import org.eclipse.debug.core.sourcelookup.containers.ArchiveSourceContainer;
28
import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
29
import org.eclipse.jdt.core.IClasspathEntry;
30
import org.eclipse.jdt.core.IJavaElement;
31
import org.eclipse.jdt.core.IJavaProject;
32
import org.eclipse.jdt.core.IPackageFragmentRoot;
33
import org.eclipse.jdt.core.JavaCore;
34
import org.eclipse.jdt.core.JavaModelException;
35
import org.eclipse.jdt.debug.core.IJavaClassType;
20
import org.eclipse.jdt.debug.core.IJavaClassType;
36
import org.eclipse.jdt.debug.core.IJavaFieldVariable;
21
import org.eclipse.jdt.debug.core.IJavaFieldVariable;
37
import org.eclipse.jdt.debug.core.IJavaObject;
22
import org.eclipse.jdt.debug.core.IJavaObject;
38
import org.eclipse.jdt.debug.core.IJavaReferenceType;
23
import org.eclipse.jdt.debug.core.IJavaReferenceType;
39
import org.eclipse.jdt.debug.core.IJavaStackFrame;
24
import org.eclipse.jdt.debug.core.IJavaStackFrame;
40
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
41
import org.eclipse.jdt.launching.JavaRuntime;
42
import org.eclipse.osgi.service.resolver.BundleDescription;
25
import org.eclipse.osgi.service.resolver.BundleDescription;
43
import org.eclipse.osgi.service.resolver.State;
26
import org.eclipse.osgi.service.resolver.State;
44
import org.eclipse.pde.core.plugin.IPluginModelBase;
27
import org.eclipse.pde.core.plugin.IPluginModelBase;
45
import org.eclipse.pde.core.plugin.ModelEntry;
46
import org.eclipse.pde.core.plugin.PluginRegistry;
47
import org.eclipse.pde.internal.core.PDEClasspathContainer;
48
import org.eclipse.pde.internal.core.PDECore;
28
import org.eclipse.pde.internal.core.PDECore;
49
import org.eclipse.pde.internal.core.TargetPlatformHelper;
29
import org.eclipse.pde.internal.core.TargetPlatformHelper;
50
import org.eclipse.pde.internal.ui.PDEPlugin;
51
30
52
public class PDESourceLookupQuery implements ISafeRunnable {
31
public class PDESourceLookupQuery implements ISafeRunnable {
53
	
32
	
Lines 58-66 Link Here
58
	
37
	
59
	private Object fElement;
38
	private Object fElement;
60
	private Object fResult;
39
	private Object fResult;
40
	private PDESourceLookupDirector fDirector;
61
	
41
	
62
	public PDESourceLookupQuery(Object object) {
42
	public PDESourceLookupQuery(PDESourceLookupDirector director, Object object) {
63
		fElement = object;		
43
		fElement = object;		
44
		fDirector = director;
64
	}
45
	}
65
46
66
	public void handleException(Throwable exception) {
47
	public void handleException(Throwable exception) {
Lines 181-298 Link Here
181
	}
162
	}
182
	
163
	
183
	protected ISourceContainer[] getSourceContainers(String location, String id) throws CoreException {
164
	protected ISourceContainer[] getSourceContainers(String location, String id) throws CoreException {
184
		ArrayList result = new ArrayList();		
165
		return fDirector.getSourceContainers(location, id);
185
		ModelEntry entry = PluginRegistry.findEntry(id);
186
		
187
		boolean match = false;
188
189
		IPluginModelBase[] models = entry.getWorkspaceModels();		
190
		for (int i = 0; i < models.length; i++) {
191
			if (isPerfectMatch(models[i], new Path(location))) {
192
				IResource resource = models[i].getUnderlyingResource();
193
				// if the plug-in matches a workspace model,
194
				// add the project and any libraries not coming via a container
195
				// to the list of source containers, in that order
196
				if (resource != null) {
197
					addProjectSourceContainers(resource.getProject(), result);
198
				}
199
				match = true;
200
				break;
201
			}
202
		}
203
204
		if (!match) {
205
			File file = new File(location);
206
			if (file.isFile()) {
207
				// in case of linked plug-in projects that map to an external JARd plug-in,
208
				// use source container that maps to the library in the linked project.
209
				ISourceContainer container = getArchiveSourceContainer(location);
210
				if (container != null)
211
					return new ISourceContainer[] {container};				
212
			} 
213
			
214
			models = entry.getExternalModels();
215
			for (int i = 0; i < models.length; i++) {
216
				if (isPerfectMatch(models[i], new Path(location))) {
217
					// try all source zips found in the source code locations
218
					IClasspathEntry[] entries = PDEClasspathContainer.getExternalEntries(models[i]);
219
					for (int j = 0; j < entries.length; j++) {
220
						IRuntimeClasspathEntry rte = convertClasspathEntry(entries[j]);
221
						if (rte != null)
222
							result.add(rte);
223
					}
224
					break;
225
				}
226
			}
227
		}
228
		
229
		IRuntimeClasspathEntry[] entries = (IRuntimeClasspathEntry[])
230
			 				result.toArray(new IRuntimeClasspathEntry[result.size()]);
231
		return JavaRuntime.getSourceContainers(entries);
232
	}
233
	
234
	private void addProjectSourceContainers(IProject project, ArrayList result) throws CoreException {
235
		if (project == null || !project.hasNature(JavaCore.NATURE_ID))
236
			return;
237
		
238
		IJavaProject jProject = JavaCore.create(project);
239
		result.add(JavaRuntime.newProjectRuntimeClasspathEntry(jProject));
240
		
241
		IClasspathEntry[] entries = jProject.getRawClasspath();
242
		for (int i = 0; i < entries.length; i++) {
243
			IClasspathEntry entry = entries[i];
244
			if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
245
				IRuntimeClasspathEntry rte = convertClasspathEntry(entry);
246
				if (rte != null)
247
					result.add(rte);
248
			}
249
		}	
250
	}
166
	}
251
	
167
	
252
	private IRuntimeClasspathEntry convertClasspathEntry(IClasspathEntry entry) {
253
		if (entry == null)
254
			return null;
255
		
256
		IPath srcPath = entry.getSourceAttachmentPath();
257
		if (srcPath != null && srcPath.segmentCount() > 0) {
258
			IRuntimeClasspathEntry rte = 
259
				JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath());
260
			rte.setSourceAttachmentPath(srcPath);
261
			rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath());
262
			return rte;
263
		}
264
		return null;
265
	}
266
	
267
	private boolean isPerfectMatch(IPluginModelBase model, IPath path) {
268
		return model == null ? false : path.equals(new Path(model.getInstallLocation()));
269
	}
270
	
271
	private ISourceContainer getArchiveSourceContainer(String location) throws JavaModelException {
272
		IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
273
		IFile[] containers = root.findFilesForLocation(new Path(location));
274
		for (int i = 0; i < containers.length; i++) {
275
			IJavaElement element = JavaCore.create(containers[i]);
276
			if (element instanceof IPackageFragmentRoot) {
277
				IPackageFragmentRoot archive = (IPackageFragmentRoot)element;
278
				IPath path = archive.getSourceAttachmentPath();
279
				if (path == null || path.segmentCount() == 0)
280
					continue;
281
				
282
				IPath rootPath = archive.getSourceAttachmentRootPath();
283
				boolean detectRootPath = rootPath != null && rootPath.segmentCount() > 0;
284
				
285
				IFile archiveFile = root.getFile(path);
286
				if (archiveFile.exists()) 
287
					return new ArchiveSourceContainer(archiveFile, detectRootPath);
288
				
289
				File file = path.toFile();
290
				if (file.exists()) 
291
					return new ExternalArchiveSourceContainer(file.getAbsolutePath(), detectRootPath);		
292
			}
293
		}
294
		return null;
295
	}
296
	/**
168
	/**
297
	 * Generates and returns a source file path based on a qualified type name.
169
	 * Generates and returns a source file path based on a qualified type name.
298
	 * For example, when <code>java.lang.String</code> is provided,
170
	 * For example, when <code>java.lang.String</code> is provided,
(-)src/org/eclipse/pde/internal/ui/launcher/PDESourceLookupDirector.java (-1 / +198 lines)
Lines 10-32 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ui.launcher;
11
package org.eclipse.pde.internal.ui.launcher;
12
12
13
import java.io.File;
14
import java.util.ArrayList;
15
import java.util.HashMap;
13
import java.util.HashSet;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.Map;
14
import java.util.Set;
19
import java.util.Set;
15
20
21
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.resources.IProject;
23
import org.eclipse.core.resources.IResource;
24
import org.eclipse.core.resources.IWorkspaceRoot;
16
import org.eclipse.core.runtime.CoreException;
25
import org.eclipse.core.runtime.CoreException;
26
import org.eclipse.core.runtime.IPath;
27
import org.eclipse.core.runtime.Path;
17
import org.eclipse.core.runtime.SafeRunner;
28
import org.eclipse.core.runtime.SafeRunner;
18
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
29
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
30
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
19
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
31
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
20
import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant;
32
import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant;
33
import org.eclipse.debug.core.sourcelookup.containers.ArchiveSourceContainer;
34
import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
21
import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer;
35
import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer;
22
import org.eclipse.debug.core.sourcelookup.containers.WorkspaceSourceContainer;
36
import org.eclipse.debug.core.sourcelookup.containers.WorkspaceSourceContainer;
23
import org.eclipse.debug.ui.sourcelookup.WorkingSetSourceContainer;
37
import org.eclipse.debug.ui.sourcelookup.WorkingSetSourceContainer;
38
import org.eclipse.jdt.core.IClasspathEntry;
39
import org.eclipse.jdt.core.IJavaElement;
40
import org.eclipse.jdt.core.IJavaProject;
41
import org.eclipse.jdt.core.IPackageFragmentRoot;
42
import org.eclipse.jdt.core.JavaCore;
43
import org.eclipse.jdt.core.JavaModelException;
24
import org.eclipse.jdt.debug.core.IJavaObject;
44
import org.eclipse.jdt.debug.core.IJavaObject;
25
import org.eclipse.jdt.debug.core.IJavaReferenceType;
45
import org.eclipse.jdt.debug.core.IJavaReferenceType;
26
import org.eclipse.jdt.debug.core.IJavaStackFrame;
46
import org.eclipse.jdt.debug.core.IJavaStackFrame;
47
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
48
import org.eclipse.jdt.launching.JavaRuntime;
27
import org.eclipse.jdt.launching.sourcelookup.containers.JavaSourceLookupParticipant;
49
import org.eclipse.jdt.launching.sourcelookup.containers.JavaSourceLookupParticipant;
50
import org.eclipse.pde.core.plugin.IPluginModelBase;
51
import org.eclipse.pde.core.plugin.ModelEntry;
52
import org.eclipse.pde.core.plugin.PluginRegistry;
53
import org.eclipse.pde.internal.core.PDEClasspathContainer;
54
import org.eclipse.pde.internal.ui.PDEPlugin;
28
55
29
public class PDESourceLookupDirector extends AbstractSourceLookupDirector {
56
public class PDESourceLookupDirector extends AbstractSourceLookupDirector {
57
	
58
	/**
59
	 * Cache of source containers by location and id (String & String)
60
	 */
61
	private Map fSourceContainerMap = new HashMap();
62
	
63
	/**
64
	 * Key for source container - combines two strings
65
	 */
66
	private class ContainerKey {
67
		private String fA;
68
		private String fB;
69
		ContainerKey(String a, String b) {
70
			fA = a;
71
			fB = b;
72
		}
73
		public boolean equals(Object obj) {
74
			if (obj instanceof ContainerKey) {
75
				ContainerKey key = (ContainerKey) obj;
76
				return key.fA.equals(fA) && key.fB.equals(fB);
77
			}
78
			return false;
79
		}
80
		public int hashCode() {
81
			return fA.hashCode() + fB.hashCode();
82
		}
83
		
84
	}
30
85
31
	private static Set fFilteredTypes;
86
	private static Set fFilteredTypes;
32
	
87
	
Lines 55-61 Link Here
55
	 * @see org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector#getSourceElement(java.lang.Object)
110
	 * @see org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector#getSourceElement(java.lang.Object)
56
	 */
111
	 */
57
	public Object getSourceElement(Object element) {
112
	public Object getSourceElement(Object element) {
58
		PDESourceLookupQuery query = new PDESourceLookupQuery(element);
113
		PDESourceLookupQuery query = new PDESourceLookupQuery(this, element);
59
		SafeRunner.run(query);
114
		SafeRunner.run(query);
60
		Object result = query.getResult();
115
		Object result = query.getResult();
61
		return result != null ? result : super.getSourceElement(element);
116
		return result != null ? result : super.getSourceElement(element);
Lines 75-78 Link Here
75
		return sourceElements;
130
		return sourceElements;
76
	}
131
	}
77
	
132
	
133
	ISourceContainer[] getSourceContainers(String location, String id) throws CoreException {
134
		
135
		ContainerKey key = new ContainerKey(location, id);
136
		ISourceContainer[] containers = (ISourceContainer[]) fSourceContainerMap.get(key);
137
		if (containers != null) {
138
			return containers;
139
		}
140
		
141
		ArrayList result = new ArrayList();		
142
		ModelEntry entry = PluginRegistry.findEntry(id);
143
		
144
		boolean match = false;
145
146
		IPluginModelBase[] models = entry.getWorkspaceModels();		
147
		for (int i = 0; i < models.length; i++) {
148
			if (isPerfectMatch(models[i], new Path(location))) {
149
				IResource resource = models[i].getUnderlyingResource();
150
				// if the plug-in matches a workspace model,
151
				// add the project and any libraries not coming via a container
152
				// to the list of source containers, in that order
153
				if (resource != null) {
154
					addProjectSourceContainers(resource.getProject(), result);
155
				}
156
				match = true;
157
				break;
158
			}
159
		}
160
161
		if (!match) {
162
			File file = new File(location);
163
			if (file.isFile()) {
164
				// in case of linked plug-in projects that map to an external JARd plug-in,
165
				// use source container that maps to the library in the linked project.
166
				ISourceContainer container = getArchiveSourceContainer(location);
167
				if (container != null) {
168
					containers = new ISourceContainer[] {container};
169
					fSourceContainerMap.put(key, containers);
170
					return containers;
171
				}
172
			} 
173
			
174
			models = entry.getExternalModels();
175
			for (int i = 0; i < models.length; i++) {
176
				if (isPerfectMatch(models[i], new Path(location))) {
177
					// try all source zips found in the source code locations
178
					IClasspathEntry[] entries = PDEClasspathContainer.getExternalEntries(models[i]);
179
					for (int j = 0; j < entries.length; j++) {
180
						IRuntimeClasspathEntry rte = convertClasspathEntry(entries[j]);
181
						if (rte != null)
182
							result.add(rte);
183
					}
184
					break;
185
				}
186
			}
187
		}
188
		
189
		IRuntimeClasspathEntry[] entries = (IRuntimeClasspathEntry[])
190
			 				result.toArray(new IRuntimeClasspathEntry[result.size()]);
191
		containers = JavaRuntime.getSourceContainers(entries);
192
		fSourceContainerMap.put(key, containers);
193
		return containers;
194
	}
195
	
196
	private boolean isPerfectMatch(IPluginModelBase model, IPath path) {
197
		return model == null ? false : path.equals(new Path(model.getInstallLocation()));
198
	}
199
	
200
	private IRuntimeClasspathEntry convertClasspathEntry(IClasspathEntry entry) {
201
		if (entry == null)
202
			return null;
203
		
204
		IPath srcPath = entry.getSourceAttachmentPath();
205
		if (srcPath != null && srcPath.segmentCount() > 0) {
206
			IRuntimeClasspathEntry rte = 
207
				JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath());
208
			rte.setSourceAttachmentPath(srcPath);
209
			rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath());
210
			return rte;
211
		}
212
		return null;
213
	}
214
	
215
	private ISourceContainer getArchiveSourceContainer(String location) throws JavaModelException {
216
		IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
217
		IFile[] containers = root.findFilesForLocation(new Path(location));
218
		for (int i = 0; i < containers.length; i++) {
219
			IJavaElement element = JavaCore.create(containers[i]);
220
			if (element instanceof IPackageFragmentRoot) {
221
				IPackageFragmentRoot archive = (IPackageFragmentRoot)element;
222
				IPath path = archive.getSourceAttachmentPath();
223
				if (path == null || path.segmentCount() == 0)
224
					continue;
225
				
226
				IPath rootPath = archive.getSourceAttachmentRootPath();
227
				boolean detectRootPath = rootPath != null && rootPath.segmentCount() > 0;
228
				
229
				IFile archiveFile = root.getFile(path);
230
				if (archiveFile.exists()) 
231
					return new ArchiveSourceContainer(archiveFile, detectRootPath);
232
				
233
				File file = path.toFile();
234
				if (file.exists()) 
235
					return new ExternalArchiveSourceContainer(file.getAbsolutePath(), detectRootPath);		
236
			}
237
		}
238
		return null;
239
	}
240
	
241
	private void addProjectSourceContainers(IProject project, ArrayList result) throws CoreException {
242
		if (project == null || !project.hasNature(JavaCore.NATURE_ID))
243
			return;
244
		
245
		IJavaProject jProject = JavaCore.create(project);
246
		result.add(JavaRuntime.newProjectRuntimeClasspathEntry(jProject));
247
		
248
		IClasspathEntry[] entries = jProject.getRawClasspath();
249
		for (int i = 0; i < entries.length; i++) {
250
			IClasspathEntry entry = entries[i];
251
			if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
252
				IRuntimeClasspathEntry rte = convertClasspathEntry(entry);
253
				if (rte != null)
254
					result.add(rte);
255
			}
256
		}	
257
	}
258
259
	/* (non-Javadoc)
260
	 * @see org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector#dispose()
261
	 */
262
	public synchronized void dispose() {
263
		Iterator iterator = fSourceContainerMap.values().iterator();
264
		while (iterator.hasNext()) {
265
			ISourceContainer[] containers = (ISourceContainer[]) iterator.next();
266
			for (int i = 0; i < containers.length; i++) {
267
				containers[i].dispose();
268
			}
269
		}
270
		fSourceContainerMap.clear();
271
		super.dispose();
272
	}	
273
	
274
	
78
}
275
}

Return to bug 182238