1 11 package org.eclipse.pde.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 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.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.jdt.core.IAccessRule; 23 import org.eclipse.jdt.core.IClasspathAttribute; 24 import org.eclipse.jdt.core.IClasspathEntry; 25 import org.eclipse.jdt.core.IJavaModelStatus; 26 import org.eclipse.jdt.core.IJavaProject; 27 import org.eclipse.jdt.core.JavaConventions; 28 import org.eclipse.jdt.core.JavaCore; 29 import org.eclipse.pde.core.build.IBuild; 30 import org.eclipse.pde.core.build.IBuildEntry; 31 import org.eclipse.pde.core.build.IBuildModel; 32 import org.eclipse.pde.core.plugin.IPluginLibrary; 33 import org.eclipse.pde.core.plugin.IPluginModelBase; 34 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; 35 import org.eclipse.pde.internal.core.util.CoreUtility; 36 import org.eclipse.team.core.RepositoryProvider; 37 38 public class ClasspathComputer { 39 40 public static void setClasspath(IProject project, IPluginModelBase model) throws CoreException { 41 IClasspathEntry[] entries = getClasspath(project, model, false); 42 JavaCore.create(project).setRawClasspath(entries, null); 43 } 44 45 public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear) throws CoreException { 46 47 ArrayList result = new ArrayList (); 48 49 addSourceAndLibraries(project, model, clear, result); 51 52 result.add(ClasspathUtilCore.createJREEntry()); 54 55 result.add(ClasspathUtilCore.createContainerEntry()); 57 58 IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]); 59 IJavaProject javaProject = JavaCore.create(project); 60 IJavaModelStatus validation = 61 JavaConventions.validateClasspath( 62 javaProject, 63 entries, 64 javaProject.getOutputLocation()); 65 if (!validation.isOK()) { 66 PDECore.logErrorMessage(validation.getMessage()); 67 throw new CoreException(validation); 68 } 69 return (IClasspathEntry[])result.toArray(new IClasspathEntry[result.size()]); 70 } 71 72 private static void addSourceAndLibraries(IProject project, IPluginModelBase model, boolean clear, 73 ArrayList result) throws CoreException { 74 75 HashSet paths = new HashSet (); 76 77 if (!clear) { 79 IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath(); 80 for (int i = 0; i < entries.length; i++) { 81 IClasspathEntry entry = entries[i]; 82 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 1) { 83 if (paths.add(entry.getPath())) 84 result.add(entry); 85 } 86 } 87 } 88 89 IBuild build = getBuild(project); 90 IClasspathAttribute[] attrs = getClasspathAttributes(project, model); 91 IPluginLibrary[] libraries = model.getPluginBase().getLibraries(); 92 for (int i = 0; i < libraries.length; i++) { 93 IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); if (buildEntry != null) { 95 addSourceFolder(buildEntry, project, paths, result); 96 } else { 97 if (libraries[i].getName().equals(".")) addJARdPlugin(project, getFilename(model), attrs, result); 99 else 100 addLibraryEntry(project, libraries[i], libraries[i].isExported(), attrs, result); 101 } 102 } 103 if (libraries.length == 0) { 104 if (build != null) { 105 IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); if (buildEntry != null) { 107 addSourceFolder(buildEntry, project, paths, result); 108 } 109 } else if (ClasspathUtilCore.isBundle(model)) { 110 addJARdPlugin(project, getFilename(model), attrs, result); 111 } 112 } 113 } 114 115 private static IClasspathAttribute[] getClasspathAttributes(IProject project, IPluginModelBase model) { 116 IClasspathAttribute[] attributes = new IClasspathAttribute[0]; 117 if (!RepositoryProvider.isShared(project)) { 118 JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager(); 119 String javadoc = manager.getJavadocLocation(model); 120 if (javadoc != null) { 121 attributes = new IClasspathAttribute[] 122 {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadoc)}; 123 } 124 } 125 return attributes; 126 } 127 128 private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet paths, ArrayList result) throws CoreException { 129 String [] folders = buildEntry.getTokens(); 130 for (int j = 0; j < folders.length; j++) { 131 String folder = folders[j]; 132 IPath path = project.getFullPath().append(folder); 133 if (paths.add(path)) { 134 if (project.findMember(folder) == null) 135 CoreUtility.createFolder(project.getFolder(folder)); 136 result.add(JavaCore.newSourceEntry(path)); 137 } 138 } 139 } 140 141 public static String getFilename(IPluginModelBase model) { 142 StringBuffer buffer = new StringBuffer (); 143 String id = model.getPluginBase().getId(); 144 if (id != null) 145 buffer.append(id); 146 buffer.append("_"); String version = model.getPluginBase().getVersion(); 148 if (version != null) 149 buffer.append(version); 150 buffer.append(".jar"); return buffer.toString(); 152 } 153 154 protected static IBuild getBuild(IProject project) throws CoreException { 155 IFile buildFile = project.getFile("build.properties"); IBuildModel buildModel = null; 157 if (buildFile.exists()) { 158 buildModel = new WorkspaceBuildModel(buildFile); 159 buildModel.load(); 160 } 161 return (buildModel != null) ? buildModel.getBuild() : null; 162 } 163 164 private static void addLibraryEntry(IProject project, IPluginLibrary library, boolean exported, IClasspathAttribute[] attrs, ArrayList result) { 165 String name = ClasspathUtilCore.expandLibraryName(library.getName()); 166 IResource jarFile = project.findMember(name); 167 if (jarFile != null) { 168 IResource resource = project.findMember(getSourceZipName(name)); 169 if (resource == null) 170 resource = project.findMember(new Path(getSourceZipName(name)).lastSegment()); 171 IPath srcAttachment = resource != null ? resource.getFullPath() : null; 172 IClasspathEntry entry = JavaCore.newLibraryEntry(jarFile.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, exported); 173 if (!result.contains(entry)) 174 result.add(entry); 175 } 176 } 177 178 private static void addJARdPlugin(IProject project, String filename, IClasspathAttribute[] attrs, ArrayList result) { 179 String name = ClasspathUtilCore.expandLibraryName(filename); 180 IResource jarFile = project.findMember(name); 181 if (jarFile != null) { 182 IResource resource = project.findMember(getSourceZipName(name)); 183 IPath srcAttachment = resource != null ? resource.getFullPath() : jarFile.getFullPath(); 184 IClasspathEntry entry = 185 JavaCore.newLibraryEntry(jarFile.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, true); 186 if (!result.contains(entry)) 187 result.add(entry); 188 } 189 } 190 191 public static String getSourceZipName(String libraryName) { 192 int dot = libraryName.lastIndexOf('.'); 193 return (dot != -1) ? libraryName.substring(0, dot) + "src.zip" : libraryName; } 195 196 } 197 | Popular Tags |