01: package net.sf.mockcreator.plugin;
02:
03: import java.io.File;
04: import java.util.ArrayList;
05: import java.util.List;
06:
07: import org.eclipse.core.runtime.IPath;
08: import org.eclipse.jdt.core.IClasspathEntry;
09: import org.eclipse.jdt.core.IJavaModel;
10: import org.eclipse.jdt.core.IJavaProject;
11: import org.eclipse.jdt.core.JavaModelException;
12:
13: public class CollectProjectClassPath {
14:
15: public static String[] collect(IJavaProject ijp)
16: throws JavaModelException {
17: List cp = new ArrayList();
18:
19: IClasspathEntry[] icpe = ijp.getResolvedClasspath(true);
20:
21: IPath projectLocation = ijp.getProject().getLocation();
22: IPath projectOutputDir = ijp.getOutputLocation();
23: addSegmentedPath(cp, getAbsolutePath(projectLocation,
24: projectOutputDir));
25:
26: for (int i = 0; i < icpe.length; ++i) {
27: int kind = icpe[i].getEntryKind();
28:
29: if (kind == IClasspathEntry.CPE_PROJECT) {
30: IClasspathEntry pcpe = icpe[i];
31:
32: IJavaModel javaModel = ijp.getJavaModel();
33: String referencedProjectName = pcpe.getPath()
34: .lastSegment();
35: IJavaProject referencedProject = javaModel
36: .getJavaProject(referencedProjectName);
37:
38: IPath ol = ijp.getOutputLocation();
39: if (projectOutputDir != null)
40: projectOutputDir = projectOutputDir
41: .removeFirstSegments(1);
42: IPath loc = referencedProject.getProject()
43: .getLocation();
44: addSegmentedPath(cp, loc.append(projectOutputDir));
45:
46: } else {
47: addSegmentedPath(cp, getAbsolutePath(projectLocation,
48: icpe[i].getPath()));
49: }
50: }
51:
52: String[] rc = new String[cp.size()];
53: for (int i = 0; i < cp.size(); ++i)
54: rc[i] = (String) cp.get(i);
55: return rc;
56: }
57:
58: private static IPath getAbsolutePath(IPath projectLocation,
59: IPath entry) {
60: if (projectLocation.lastSegment().equals(entry.segment(0))) {
61: // relative
62: return projectLocation.append(entry.removeFirstSegments(1));
63: }
64:
65: return entry;
66: }
67:
68: private static void addSegmentedPath(List cp, IPath path) {
69: if (path == null)
70: return;
71:
72: StringBuffer sb = new StringBuffer();
73: String[] sgs = path.segments();
74:
75: if (path.getDevice() != null) {
76: sb.append(path.getDevice()).append(File.separatorChar);
77: }
78:
79: for (int i = 0; i < sgs.length; ++i) {
80: sb.append(sgs[i]);
81: if (i != sgs.length - 1)
82: sb.append(File.separatorChar);
83: }
84:
85: cp.add(sb.toString());
86: }
87: }
|