Removed
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2005, 2008 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.pde.internal.ui.wizards.plugin; |
12 |
|
13 |
import java.util.*; |
14 |
import org.eclipse.core.resources.*; |
15 |
import org.eclipse.core.runtime.CoreException; |
16 |
import org.eclipse.core.runtime.IPath; |
17 |
import org.eclipse.jdt.core.*; |
18 |
import org.eclipse.jdt.launching.JavaRuntime; |
19 |
import org.eclipse.jdt.launching.environments.IExecutionEnvironment; |
20 |
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; |
21 |
import org.eclipse.osgi.service.resolver.BundleDescription; |
22 |
import org.eclipse.pde.core.build.*; |
23 |
import org.eclipse.pde.core.plugin.IPluginLibrary; |
24 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
25 |
import org.eclipse.pde.internal.core.*; |
26 |
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; |
27 |
import org.eclipse.pde.internal.core.util.CoreUtility; |
28 |
import org.eclipse.team.core.RepositoryProvider; |
29 |
|
30 |
public class ClasspathComputer { |
31 |
|
32 |
private static Hashtable fSeverityTable = null; |
33 |
private static final int SEVERITY_ERROR = 3; |
34 |
private static final int SEVERITY_WARNING = 2; |
35 |
private static final int SEVERITY_IGNORE = 1; |
36 |
|
37 |
public static void setClasspath(IProject project, IPluginModelBase model) throws CoreException { |
38 |
IClasspathEntry[] entries = getClasspath(project, model, false); |
39 |
JavaCore.create(project).setRawClasspath(entries, null); |
40 |
} |
41 |
|
42 |
public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear) throws CoreException { |
43 |
IJavaProject javaProject = JavaCore.create(project); |
44 |
ArrayList result = new ArrayList(); |
45 |
IBuild build = getBuild(project); |
46 |
|
47 |
// add JRE and set compliance options |
48 |
String ee = getExecutionEnvironment(model.getBundleDescription()); |
49 |
result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.JRE_CONTAINER_PATH)); |
50 |
setComplianceOptions(JavaCore.create(project), ExecutionEnvironmentAnalyzer.getCompliance(ee)); |
51 |
|
52 |
// add pde container |
53 |
result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.REQUIRED_PLUGINS_CONTAINER_PATH)); |
54 |
|
55 |
// add own libraries/source |
56 |
addSourceAndLibraries(project, model, build, clear, result); |
57 |
|
58 |
IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]); |
59 |
IJavaModelStatus validation = JavaConventions.validateClasspath(javaProject, entries, javaProject.getOutputLocation()); |
60 |
if (!validation.isOK()) { |
61 |
PDECore.logErrorMessage(validation.getMessage()); |
62 |
throw new CoreException(validation); |
63 |
} |
64 |
return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]); |
65 |
} |
66 |
|
67 |
public static void addSourceAndLibraries(IProject project, IPluginModelBase model, IBuild build, boolean clear, ArrayList result) throws CoreException { |
68 |
|
69 |
HashSet paths = new HashSet(); |
70 |
|
71 |
// keep existing source folders |
72 |
if (!clear) { |
73 |
IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath(); |
74 |
for (int i = 0; i < entries.length; i++) { |
75 |
IClasspathEntry entry = entries[i]; |
76 |
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { |
77 |
if (paths.add(entry.getPath())) |
78 |
result.add(entry); |
79 |
} |
80 |
} |
81 |
} |
82 |
|
83 |
IClasspathAttribute[] attrs = getClasspathAttributes(project, model); |
84 |
IPluginLibrary[] libraries = model.getPluginBase().getLibraries(); |
85 |
for (int i = 0; i < libraries.length; i++) { |
86 |
IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$ |
87 |
if (buildEntry != null) { |
88 |
addSourceFolder(buildEntry, project, paths, result); |
89 |
} else { |
90 |
if (libraries[i].getName().equals(".")) //$NON-NLS-1$ |
91 |
addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result); |
92 |
else |
93 |
addLibraryEntry(project, libraries[i], attrs, result); |
94 |
} |
95 |
} |
96 |
if (libraries.length == 0) { |
97 |
if (build != null) { |
98 |
IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$ |
99 |
if (buildEntry != null) { |
100 |
addSourceFolder(buildEntry, project, paths, result); |
101 |
} |
102 |
} else if (ClasspathUtilCore.hasBundleStructure(model)) { |
103 |
addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result); |
104 |
} |
105 |
} |
106 |
} |
107 |
|
108 |
private static IClasspathAttribute[] getClasspathAttributes(IProject project, IPluginModelBase model) { |
109 |
IClasspathAttribute[] attributes = new IClasspathAttribute[0]; |
110 |
if (!RepositoryProvider.isShared(project)) { |
111 |
JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager(); |
112 |
String javadoc = manager.getJavadocLocation(model); |
113 |
if (javadoc != null) { |
114 |
attributes = new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadoc)}; |
115 |
} |
116 |
} |
117 |
return attributes; |
118 |
} |
119 |
|
120 |
private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet paths, ArrayList result) throws CoreException { |
121 |
String[] folders = buildEntry.getTokens(); |
122 |
for (int j = 0; j < folders.length; j++) { |
123 |
String folder = folders[j]; |
124 |
IPath path = project.getFullPath().append(folder); |
125 |
if (paths.add(path)) { |
126 |
if (project.findMember(folder) == null) { |
127 |
CoreUtility.createFolder(project.getFolder(folder)); |
128 |
} else { |
129 |
IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(path.toString()); |
130 |
if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) { |
131 |
result.add(root.getRawClasspathEntry()); |
132 |
continue; |
133 |
} |
134 |
} |
135 |
result.add(JavaCore.newSourceEntry(path)); |
136 |
} |
137 |
} |
138 |
} |
139 |
|
140 |
protected static IBuild getBuild(IProject project) throws CoreException { |
141 |
IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$ |
142 |
IBuildModel buildModel = null; |
143 |
if (buildFile.exists()) { |
144 |
buildModel = new WorkspaceBuildModel(buildFile); |
145 |
buildModel.load(); |
146 |
} |
147 |
return (buildModel != null) ? buildModel.getBuild() : null; |
148 |
} |
149 |
|
150 |
private static void addLibraryEntry(IProject project, IPluginLibrary library, IClasspathAttribute[] attrs, ArrayList result) throws JavaModelException { |
151 |
String name = ClasspathUtilCore.expandLibraryName(library.getName()); |
152 |
IResource jarFile = project.findMember(name); |
153 |
if (jarFile == null) |
154 |
return; |
155 |
|
156 |
IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(jarFile); |
157 |
if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) { |
158 |
IClasspathEntry oldEntry = root.getRawClasspathEntry(); |
159 |
if (oldEntry.getSourceAttachmentPath() != null && !result.contains(oldEntry)) { |
160 |
result.add(oldEntry); |
161 |
return; |
162 |
} |
163 |
} |
164 |
|
165 |
IClasspathEntry entry = createClasspathEntry(project, jarFile, name, attrs, library.isExported()); |
166 |
if (!result.contains(entry)) |
167 |
result.add(entry); |
168 |
} |
169 |
|
170 |
private static void addJARdPlugin(IProject project, String filename, IClasspathAttribute[] attrs, ArrayList result) { |
171 |
String name = ClasspathUtilCore.expandLibraryName(filename); |
172 |
IResource jarFile = project.findMember(name); |
173 |
if (jarFile != null) { |
174 |
IClasspathEntry entry = createClasspathEntry(project, jarFile, filename, attrs, true); |
175 |
if (!result.contains(entry)) |
176 |
result.add(entry); |
177 |
} |
178 |
} |
179 |
|
180 |
private static IClasspathEntry createClasspathEntry(IProject project, IResource library, String fileName, IClasspathAttribute[] attrs, boolean isExported) { |
181 |
String sourceZipName = ClasspathUtilCore.getSourceZipName(fileName); |
182 |
IResource resource = project.findMember(sourceZipName); |
183 |
// if zip file does not exist, see if a directory with the source does. This in necessary how we import source for individual source bundles. |
184 |
if (resource == null && sourceZipName.endsWith(".zip")) { //$NON-NLS-1$ |
185 |
resource = project.findMember(sourceZipName.substring(0, sourceZipName.length() - 4)); |
186 |
if (resource == null) |
187 |
// if we can't find the the source for a library, then try to find the common source location set up to share source from one jar to all libraries. |
188 |
// see PluginImportOperation.linkSourceArchives |
189 |
resource = project.getFile(project.getName() + "src.zip"); //$NON-NLS-1$ |
190 |
} |
191 |
IPath srcAttachment = resource != null ? resource.getFullPath() : library.getFullPath(); |
192 |
return JavaCore.newLibraryEntry(library.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, isExported); |
193 |
} |
194 |
|
195 |
private static String getExecutionEnvironment(BundleDescription bundleDescription) { |
196 |
if (bundleDescription != null) { |
197 |
String[] envs = bundleDescription.getExecutionEnvironments(); |
198 |
if (envs.length > 0) |
199 |
return envs[0]; |
200 |
} |
201 |
return null; |
202 |
} |
203 |
|
204 |
public static void setComplianceOptions(IJavaProject project, String compliance) { |
205 |
Map map = project.getOptions(false); |
206 |
if (compliance == null) { |
207 |
if (map.size() > 0) { |
208 |
map.remove(JavaCore.COMPILER_COMPLIANCE); |
209 |
map.remove(JavaCore.COMPILER_SOURCE); |
210 |
map.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM); |
211 |
map.remove(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER); |
212 |
map.remove(JavaCore.COMPILER_PB_ENUM_IDENTIFIER); |
213 |
} else { |
214 |
return; |
215 |
} |
216 |
} else if (JavaCore.VERSION_1_6.equals(compliance)) { |
217 |
map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6); |
218 |
map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); |
219 |
map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6); |
220 |
map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR); |
221 |
map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR); |
222 |
} else if (JavaCore.VERSION_1_5.equals(compliance)) { |
223 |
map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); |
224 |
map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); |
225 |
map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); |
226 |
map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR); |
227 |
map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR); |
228 |
} else if (JavaCore.VERSION_1_4.equals(compliance)) { |
229 |
map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4); |
230 |
map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3); |
231 |
map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2); |
232 |
updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.WARNING); |
233 |
updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.WARNING); |
234 |
} else if (JavaCore.VERSION_1_3.equals(compliance)) { |
235 |
map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_3); |
236 |
map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3); |
237 |
map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_1); |
238 |
updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.IGNORE); |
239 |
updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.IGNORE); |
240 |
} |
241 |
project.setOptions(map); |
242 |
} |
243 |
|
244 |
private static void updateSeverityComplianceOption(Map map, String key, String value) { |
245 |
Integer current_value = null; |
246 |
Integer new_value = null; |
247 |
String current_string_value = null; |
248 |
int current_int_value = 0; |
249 |
int new_int_value = 0; |
250 |
// Initialize the severity table (only once) |
251 |
if (fSeverityTable == null) { |
252 |
fSeverityTable = new Hashtable(SEVERITY_ERROR); |
253 |
fSeverityTable.put(JavaCore.IGNORE, new Integer(SEVERITY_IGNORE)); |
254 |
fSeverityTable.put(JavaCore.WARNING, new Integer(SEVERITY_WARNING)); |
255 |
fSeverityTable.put(JavaCore.ERROR, new Integer(SEVERITY_ERROR)); |
256 |
} |
257 |
// Get the current severity |
258 |
current_string_value = (String) map.get(key); |
259 |
if (current_string_value != null) { |
260 |
current_value = (Integer) fSeverityTable.get(current_string_value); |
261 |
if (current_value != null) { |
262 |
current_int_value = current_value.intValue(); |
263 |
} |
264 |
} |
265 |
// Get the new severity |
266 |
new_value = (Integer) fSeverityTable.get(value); |
267 |
if (new_value != null) { |
268 |
new_int_value = new_value.intValue(); |
269 |
} |
270 |
// If the current severity is not higher than the new severity, replace it |
271 |
if (new_int_value > current_int_value) { |
272 |
map.put(key, value); |
273 |
} |
274 |
} |
275 |
|
276 |
/** |
277 |
* Returns a new classpath container entry for the given execution environment. If the given java project |
278 |
* has an existing JRE/EE classpath entry, the access rules, extra attributes and isExported settings of |
279 |
* the existing entry will be added to the new execution entry. |
280 |
* |
281 |
* @param javaProject project to check for existing classpath entries |
282 |
* @param ee id of the execution environment to create an entry for |
283 |
* @param path id of the container to create an entry for |
284 |
* |
285 |
* @return new classpath container entry |
286 |
* @throws CoreException if there is a problem accessing the classpath entries of the project |
287 |
*/ |
288 |
public static IClasspathEntry createEntryUsingPreviousEntry(IJavaProject javaProject, String ee, IPath path) throws CoreException { |
289 |
IClasspathEntry[] entries = javaProject.getRawClasspath(); |
290 |
for (int i = 0; i < entries.length; i++) { |
291 |
if (entries[i].getPath().equals(path)) { |
292 |
if (path.equals(PDECore.JRE_CONTAINER_PATH)) |
293 |
return JavaCore.newContainerEntry(getEEPath(ee), entries[i].getAccessRules(), entries[i].getExtraAttributes(), entries[i].isExported()); |
294 |
|
295 |
return JavaCore.newContainerEntry(path, entries[i].getAccessRules(), entries[i].getExtraAttributes(), entries[i].isExported()); |
296 |
} |
297 |
} |
298 |
|
299 |
if (path.equals(PDECore.JRE_CONTAINER_PATH)) |
300 |
return createJREEntry(ee); |
301 |
|
302 |
return JavaCore.newContainerEntry(path); |
303 |
} |
304 |
|
305 |
/** |
306 |
* Returns a classpath container entry for the given execution environment. |
307 |
* @param ee id of the execution environment |
308 |
* @return classpath container entry |
309 |
*/ |
310 |
public static IClasspathEntry createJREEntry(String ee) { |
311 |
return JavaCore.newContainerEntry(getEEPath(ee)); |
312 |
} |
313 |
|
314 |
/** |
315 |
* Returns the JRE container path for the execution environment with the given id. |
316 |
* @param ee execution environment id |
317 |
* @return JRE container path for the execution environment |
318 |
*/ |
319 |
private static IPath getEEPath(String ee) { |
320 |
IPath path = null; |
321 |
if (ee != null) { |
322 |
IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); |
323 |
IExecutionEnvironment env = manager.getEnvironment(ee); |
324 |
if (env != null) |
325 |
path = JavaRuntime.newJREContainerPath(env); |
326 |
} |
327 |
if (path == null) { |
328 |
path = JavaRuntime.newDefaultJREContainerPath(); |
329 |
} |
330 |
return path; |
331 |
} |
332 |
|
333 |
/** |
334 |
* @return a new classpath container entry for a required plugin container |
335 |
*/ |
336 |
public static IClasspathEntry createContainerEntry() { |
337 |
return JavaCore.newContainerEntry(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH); |
338 |
} |
339 |
|
340 |
} |