KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > StandardClasspathProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.launching;
12
13
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.jdt.core.IJavaProject;
23
24 /**
25  * Default implementation for classpath provider.
26  * <p>
27  * This class may be subclassed.
28  * </p>
29  * @since 2.0
30  */

31 public class StandardClasspathProvider implements IRuntimeClasspathProvider {
32
33     /* (non-Javadoc)
34      * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#computeUnresolvedClasspath(org.eclipse.debug.core.ILaunchConfiguration)
35      */

36     public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException {
37         boolean useDefault = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, true);
38         if (useDefault) {
39             IJavaProject proj = JavaRuntime.getJavaProject(configuration);
40             IRuntimeClasspathEntry jreEntry = JavaRuntime.computeJREEntry(configuration);
41             if (proj == null) {
42                 //no project - use default libraries
43
if (jreEntry == null) {
44                     return new IRuntimeClasspathEntry[0];
45                 }
46                 return new IRuntimeClasspathEntry[]{jreEntry};
47             }
48             IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedRuntimeClasspath(proj);
49             // replace project JRE with config's JRE
50
IRuntimeClasspathEntry projEntry = JavaRuntime.computeJREEntry(proj);
51             if (jreEntry != null && projEntry != null) {
52                 if (!jreEntry.equals(projEntry)) {
53                     for (int i = 0; i < entries.length; i++) {
54                         IRuntimeClasspathEntry entry = entries[i];
55                         if (entry.equals(projEntry)) {
56                             entries[i] = jreEntry;
57                             return entries;
58                         }
59                     }
60                 }
61             }
62             return entries;
63         }
64         // recover persisted classpath
65
return recoverRuntimePath(configuration, IJavaLaunchConfigurationConstants.ATTR_CLASSPATH);
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#resolveClasspath(org.eclipse.jdt.launching.IRuntimeClasspathEntry[], org.eclipse.debug.core.ILaunchConfiguration)
70      */

71     public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException {
72         // use an ordered set to avoid duplicates
73
Set JavaDoc all = new LinkedHashSet JavaDoc(entries.length);
74         for (int i = 0; i < entries.length; i++) {
75             IRuntimeClasspathEntry[] resolved =JavaRuntime.resolveRuntimeClasspathEntry(entries[i], configuration);
76             for (int j = 0; j < resolved.length; j++) {
77                 all.add(resolved[j]);
78             }
79         }
80         return (IRuntimeClasspathEntry[])all.toArray(new IRuntimeClasspathEntry[all.size()]);
81     }
82     
83     /**
84      * Returns a collection of runtime classpath entries that are defined in the
85      * specified attribute of the given launch configuration. When present,
86      * the attribute must contain a list of runtime classpath entry mementos.
87      *
88      * @param configuration launch configuration
89      * @param attribute attribute name containing the list of entries
90      * @return collection of runtime classpath entries that are defined in the
91      * specified attribute of the given launch configuration
92      * @exception CoreException if unable to retrieve the list
93      */

94     protected IRuntimeClasspathEntry[] recoverRuntimePath(ILaunchConfiguration configuration, String JavaDoc attribute) throws CoreException {
95         List JavaDoc entries = configuration.getAttribute(attribute, Collections.EMPTY_LIST);
96         IRuntimeClasspathEntry[] rtes = new IRuntimeClasspathEntry[entries.size()];
97         Iterator JavaDoc iter = entries.iterator();
98         int i = 0;
99         while (iter.hasNext()) {
100             rtes[i] = JavaRuntime.newRuntimeClasspathEntry((String JavaDoc)iter.next());
101             i++;
102         }
103         return rtes;
104     }
105
106 }
107
Popular Tags