001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.util;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.ListIterator;
016:
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.jdt.core.IJavaElement;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.core.IPackageFragment;
025: import org.eclipse.jdt.core.IPackageFragmentRoot;
026: import org.eclipse.jdt.core.IType;
027: import org.eclipse.jdt.core.JavaCore;
028: import org.eclipse.jdt.core.JavaModelException;
029: import org.eclipse.jdt.core.search.IJavaSearchScope;
030: import org.eclipse.jdt.core.search.SearchEngine;
031: import org.eclipse.jdt.launching.JavaRuntime;
032: import org.eclipse.osgi.service.resolver.ExportPackageDescription;
033: import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
034: import org.eclipse.pde.core.plugin.IPluginLibrary;
035: import org.eclipse.pde.core.plugin.IPluginModelBase;
036: import org.eclipse.pde.core.plugin.PluginRegistry;
037: import org.eclipse.pde.internal.core.ClasspathUtilCore;
038: import org.eclipse.pde.internal.core.PDECore;
039: import org.eclipse.pde.internal.core.SearchablePluginsManager;
040:
041: public class PDEJavaHelper {
042:
043: /*static class Requestor extends TypeNameRequestor {
044: int count = 0;
045:
046: public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName,
047: char[][] enclosingTypeNames, String path) {
048: count += 1;
049: }
050:
051: public boolean hasMatches() {
052: return count > 0;
053: }
054: }*/
055:
056: public static boolean isOnClasspath(String fullyQualifiedName,
057: IJavaProject project) {
058: if (fullyQualifiedName.indexOf('$') != -1)
059: fullyQualifiedName = fullyQualifiedName.replace('$', '.');
060: try {
061: IType type = project.findType(fullyQualifiedName);
062: return type != null && type.exists();
063: } catch (JavaModelException e) {
064: }
065: return false;
066: /*try {
067: Requestor requestor = new Requestor();
068: new SearchEngine().searchAllTypeNames(
069: fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf('.')).toCharArray(),
070: SearchPattern.R_EXACT_MATCH|SearchPattern.R_CASE_SENSITIVE,
071: fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf('.') + 1).toCharArray(),
072: SearchPattern.R_EXACT_MATCH|SearchPattern.R_CASE_SENSITIVE,
073: IJavaSearchConstants.TYPE,
074: SearchEngine.createJavaSearchScope(new IJavaElement[] {project}),
075: requestor,
076: IJavaSearchConstants.FORCE_IMMEDIATE_SEARCH,
077: new NullProgressMonitor());
078: return requestor.hasMatches();
079: } catch (JavaModelException e) {
080: }
081: return false;*/
082: }
083:
084: public static IJavaSearchScope getSearchScope(IJavaProject project) {
085: return SearchEngine
086: .createJavaSearchScope(getNonJRERoots(project));
087: }
088:
089: public static IJavaSearchScope getSearchScope(IProject project) {
090: return getSearchScope(JavaCore.create(project));
091: }
092:
093: public static IPackageFragmentRoot[] getNonJRERoots(
094: IJavaProject project) {
095: ArrayList result = new ArrayList();
096: try {
097: IPackageFragmentRoot[] roots = project
098: .getAllPackageFragmentRoots();
099: for (int i = 0; i < roots.length; i++) {
100: if (!isJRELibrary(roots[i])) {
101: result.add(roots[i]);
102: }
103: }
104: } catch (JavaModelException e) {
105: }
106: return (IPackageFragmentRoot[]) result
107: .toArray(new IPackageFragmentRoot[result.size()]);
108: }
109:
110: public static boolean isJRELibrary(IPackageFragmentRoot root) {
111: try {
112: IPath path = root.getRawClasspathEntry().getPath();
113: if (path.equals(new Path(JavaRuntime.JRE_CONTAINER))
114: || path
115: .equals(new Path(
116: JavaRuntime.JRELIB_VARIABLE))) {
117: return true;
118: }
119: } catch (JavaModelException e) {
120: }
121: return false;
122: }
123:
124: /**
125: * @param packageName - the name of the package
126: * @param pluginID - the id of the containing plug-in - can be null if <code>project</code> is not null
127: * @param project - if null will search for an external package fragment, otherwise will search in project
128: * @return
129: */
130: public static IPackageFragment getPackageFragment(
131: String packageName, String pluginID, IProject project) {
132: if (project == null)
133: return getExternalPackageFragment(packageName, pluginID);
134:
135: IJavaProject jp = JavaCore.create(project);
136: if (jp != null)
137: try {
138: IPackageFragmentRoot[] roots = jp
139: .getAllPackageFragmentRoots();
140: for (int i = 0; i < roots.length; i++) {
141: IPackageFragment frag = roots[i]
142: .getPackageFragment(packageName);
143: if (frag.exists()) {
144: return frag;
145: }
146: }
147: } catch (JavaModelException e) {
148: }
149: return null;
150: }
151:
152: private static IPackageFragment getExternalPackageFragment(
153: String packageName, String pluginID) {
154: if (pluginID == null)
155: return null;
156: IPluginModelBase base = null;
157: try {
158: IPluginModelBase plugin = PluginRegistry
159: .findModel(pluginID);
160: if (plugin == null)
161: return null;
162: ImportPackageSpecification[] packages = plugin
163: .getBundleDescription().getImportPackages();
164: for (int i = 0; i < packages.length; i++)
165: if (packages[i].getName().equals(packageName)) {
166: ExportPackageDescription desc = (ExportPackageDescription) packages[i]
167: .getSupplier();
168: if (desc != null)
169: base = PluginRegistry.findModel(desc
170: .getExporter().getSymbolicName());
171: break;
172: }
173: if (base == null)
174: return null;
175: IResource res = base.getUnderlyingResource();
176: if (res != null) {
177: IJavaProject jp = JavaCore.create(res.getProject());
178: if (jp != null)
179: try {
180: IPackageFragmentRoot[] roots = jp
181: .getAllPackageFragmentRoots();
182: for (int i = 0; i < roots.length; i++) {
183: IPackageFragment frag = roots[i]
184: .getPackageFragment(packageName);
185: if (frag.exists())
186: return frag;
187: }
188: } catch (JavaModelException e) {
189: }
190: }
191: IProject proj = PDECore
192: .getWorkspace()
193: .getRoot()
194: .getProject(
195: SearchablePluginsManager.PROXY_PROJECT_NAME);
196: if (proj == null)
197: return searchWorkspaceForPackage(packageName, base);
198: IJavaProject jp = JavaCore.create(proj);
199: IPath path = new Path(base.getInstallLocation());
200: // if model is in jar form
201: if (!path.toFile().isDirectory()) {
202: IPackageFragmentRoot root = jp
203: .findPackageFragmentRoot(path);
204: if (root != null) {
205: IPackageFragment frag = root
206: .getPackageFragment(packageName);
207: if (frag.exists())
208: return frag;
209: }
210: // else model is in folder form, try to find model's libraries on filesystem
211: } else {
212: IPluginLibrary[] libs = base.getPluginBase()
213: .getLibraries();
214: for (int i = 0; i < libs.length; i++) {
215: if (IPluginLibrary.RESOURCE.equals(libs[i]
216: .getType()))
217: continue;
218: String libName = ClasspathUtilCore
219: .expandLibraryName(libs[i].getName());
220: IPackageFragmentRoot root = jp
221: .findPackageFragmentRoot(path
222: .append(libName));
223: if (root != null) {
224: IPackageFragment frag = root
225: .getPackageFragment(packageName);
226: if (frag.exists())
227: return frag;
228: }
229: }
230: }
231: } catch (JavaModelException e) {
232: }
233: return searchWorkspaceForPackage(packageName, base);
234: }
235:
236: private static IPackageFragment searchWorkspaceForPackage(
237: String packageName, IPluginModelBase base) {
238: IPluginLibrary[] libs = base.getPluginBase().getLibraries();
239: ArrayList libPaths = new ArrayList();
240: IPath path = new Path(base.getInstallLocation());
241: if (libs.length == 0) {
242: libPaths.add(path);
243: }
244: for (int i = 0; i < libs.length; i++) {
245: if (IPluginLibrary.RESOURCE.equals(libs[i].getType()))
246: continue;
247: String libName = ClasspathUtilCore
248: .expandLibraryName(libs[i].getName());
249: libPaths.add(path.append(libName));
250: }
251: IProject[] projects = PDECore.getWorkspace().getRoot()
252: .getProjects();
253: for (int i = 0; i < projects.length; i++) {
254: try {
255: if (!projects[i].hasNature(JavaCore.NATURE_ID)
256: || !projects[i].isOpen())
257: continue;
258: IJavaProject jp = JavaCore.create(projects[i]);
259: ListIterator li = libPaths.listIterator();
260: while (li.hasNext()) {
261: IPackageFragmentRoot root = jp
262: .findPackageFragmentRoot((IPath) li.next());
263: if (root != null) {
264: IPackageFragment frag = root
265: .getPackageFragment(packageName);
266: if (frag.exists())
267: return frag;
268: }
269: }
270: } catch (CoreException e) {
271: }
272: }
273: return null;
274: }
275:
276: /**
277: * @param jProject
278: * @param existingPackages
279: * @param allowJava
280: * @return
281: */
282: public static IPackageFragment[] getPackageFragments(
283: IJavaProject jProject, Collection existingPackages,
284: boolean allowJava) {
285: HashMap map = getPackageFragmentsHash(jProject,
286: existingPackages, allowJava);
287: return (IPackageFragment[]) map.values().toArray(
288: new IPackageFragment[map.size()]);
289: }
290:
291: /**
292: * @param jProject
293: * @param existingPackages
294: * @param allowJava
295: * @return
296: */
297: public static HashMap getPackageFragmentsHash(
298: IJavaProject jProject, Collection existingPackages,
299: boolean allowJava) {
300: HashMap map = new HashMap();
301: try {
302: IPackageFragmentRoot[] roots = getRoots(jProject);
303: for (int i = 0; i < roots.length; i++) {
304: IJavaElement[] children = roots[i].getChildren();
305: for (int j = 0; j < children.length; j++) {
306: IPackageFragment fragment = (IPackageFragment) children[j];
307: String name = fragment.getElementName();
308: if (name.length() == 0)
309: name = "."; //$NON-NLS-1$
310: if ((fragment.hasChildren() || fragment
311: .getNonJavaResources().length > 0)
312: && !existingPackages.contains(name)) {
313: if (!name.equals("java") || !name.startsWith("java.") || allowJava) //$NON-NLS-1$ //$NON-NLS-2$
314: map
315: .put(fragment.getElementName(),
316: fragment);
317: }
318: }
319: }
320: } catch (JavaModelException e) {
321: }
322: return map;
323: }
324:
325: private static IPackageFragmentRoot[] getRoots(IJavaProject jProject) {
326: ArrayList result = new ArrayList();
327: try {
328: IPackageFragmentRoot[] roots = jProject
329: .getPackageFragmentRoots();
330: for (int i = 0; i < roots.length; i++) {
331: if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE
332: || jProject.getProject().equals(
333: roots[i].getCorrespondingResource())
334: || (roots[i].isArchive() && !roots[i]
335: .isExternal())) {
336: result.add(roots[i]);
337: }
338: }
339: } catch (JavaModelException e) {
340: }
341: return (IPackageFragmentRoot[]) result
342: .toArray(new IPackageFragmentRoot[result.size()]);
343: }
344:
345: /**
346: * @param project
347: * @return
348: */
349: public static String getJavaSourceLevel(IProject project) {
350: return getJavaLevel(project, JavaCore.COMPILER_SOURCE);
351: }
352:
353: /**
354: * @param project
355: * @return
356: */
357: public static String getJavaComplianceLevel(IProject project) {
358: return getJavaLevel(project, JavaCore.COMPILER_COMPLIANCE);
359: }
360:
361: /**
362: * Precedence order from high to low: (1) Project specific option;
363: * (2) General preference option; (3) Default option; (4) Java 1.3
364: * @param project
365: * @param optionName
366: * @return
367: */
368: public static String getJavaLevel(IProject project,
369: String optionName) {
370: // Returns the corresponding java project
371: // No need to check for null, will return null
372: IJavaProject javaProject = JavaCore.create(project);
373: String value = null;
374: // Preferred to use the project
375: if ((javaProject != null) && javaProject.exists()) {
376: // Get the project specific option if one exists. Rolls up to the
377: // general preference option if no project specific option exists.
378: value = javaProject.getOption(optionName, true);
379: if (value != null) {
380: return value;
381: }
382: }
383: // Get the general preference option
384: value = JavaCore.getJavaCore().getPluginPreferences()
385: .getString(optionName);
386: if (value != null) {
387: return value;
388: }
389: // Get the default option
390: value = JavaCore.getOption(optionName);
391: if (value != null) {
392: return value;
393: }
394: // Return the default
395: return JavaCore.VERSION_1_3;
396: }
397:
398: }
|