List of usage examples for org.eclipse.jdt.internal.core JavaProject getClasspathEntryFor
@Override
public IClasspathEntry getClasspathEntryFor(IPath path) throws JavaModelException
From source file:com.codenvy.ide.ext.java.server.internal.core.JavaProjectElementInfo.java
License:Open Source License
/** * Compute the non-java resources contained in this java project. *//*w ww.j a v a2s . c o m*/ private Object[] computeNonJavaResources(JavaProject project) { // determine if src == project and/or if bin == project IPath projectPath = project.getProject().getFullPath(); boolean srcIsProject = false; boolean binIsProject = false; char[][] inclusionPatterns = null; char[][] exclusionPatterns = null; IPath projectOutput = null; boolean isClasspathResolved = true; try { IClasspathEntry entry = project.getClasspathEntryFor(projectPath); if (entry != null) { srcIsProject = true; inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars(); exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars(); } projectOutput = project.getOutputLocation(); binIsProject = projectPath.equals(projectOutput); } catch (JavaModelException e) { isClasspathResolved = false; } Object[] resources = new IResource[5]; int resourcesCounter = 0; try { IResource[] members = ((IContainer) project.getResource()).members(); int length = members.length; if (length > 0) { String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); IClasspathEntry[] classpath = project.getResolvedClasspath(); for (int i = 0; i < length; i++) { IResource res = members[i]; switch (res.getType()) { case IResource.FILE: IPath resFullPath = res.getFullPath(); String resName = res.getName(); // ignore a jar file on the classpath if (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath, res.getLocation()/* see https://bugs.eclipse .org/bugs/show_bug.cgi?id=244406 */, classpath, projectOutput)) { break; } // ignore .java file if src == project if (srcIsProject && Util.isValidCompilationUnitName(resName, sourceLevel, complianceLevel) && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)) { break; } // ignore .class file if bin == project if (binIsProject && Util.isValidClassFileName(resName, sourceLevel, complianceLevel)) { break; } // else add non java resource if (resources.length == resourcesCounter) { // resize System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0, resourcesCounter); } resources[resourcesCounter++] = res; break; case IResource.FOLDER: resFullPath = res.getFullPath(); // ignore non-excluded folders on the classpath or that correspond to an output location if ((srcIsProject && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns) && Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel)) || (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath, res.getLocation(), classpath, projectOutput))) { break; } // else add non java resource if (resources.length == resourcesCounter) { // resize System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0, resourcesCounter); } resources[resourcesCounter++] = res; } } } if (resources.length != resourcesCounter) { System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter]), 0, resourcesCounter); } } catch (CoreException e) { resources = NO_NON_JAVA_RESOURCES; resourcesCounter = 0; } return resources; }
From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java
License:Open Source License
/** * Returns the package fragment whose path matches the given * (absolute) path, or <code>null</code> if none exist. The domain of * the search is bounded by the classpath of the <code>IJavaProject</code> * this <code>NameLookup</code> was obtained from. * The path can be:/*from www. ja v a2 s. c o m*/ * - internal to the workbench: "/Project/src" * - external to the workbench: "c:/jdk/classes.zip/java/lang" */ public IPackageFragment findPackageFragment(IPath path) { if (!path.isAbsolute()) { throw new IllegalArgumentException(Messages.path_mustBeAbsolute); } /* * TODO (jerome) this code should rather use the package fragment map to find the candidate package, then * check if the respective enclosing root maps to the one on this given IPath. */ IResource possibleFragment = ResourcesPlugin.getWorkspace().getRoot().findMember(path); if (possibleFragment == null) { //external jar for (int i = 0; i < this.packageFragmentRoots.length; i++) { IPackageFragmentRoot root = this.packageFragmentRoots[i]; if (!root.isExternal()) { continue; } IPath rootPath = root.getPath(); if (rootPath.isPrefixOf(path)) { String name = path.toOSString(); // + 1 is for the File.separatorChar name = name.substring(rootPath.toOSString().length() + 1, name.length()); name = name.replace(File.separatorChar, '.'); IJavaElement[] list = null; try { list = root.getChildren(); } catch (JavaModelException npe) { continue; // the package fragment root is not present; } int elementCount = list.length; for (int j = 0; j < elementCount; j++) { IPackageFragment packageFragment = (IPackageFragment) list[j]; if (nameMatches(name, packageFragment, false)) { return packageFragment; } } } } } else { IJavaElement fromFactory = JavaCore.create(possibleFragment); if (fromFactory == null) { return null; } switch (fromFactory.getElementType()) { case IJavaElement.PACKAGE_FRAGMENT: return (IPackageFragment) fromFactory; case IJavaElement.JAVA_PROJECT: // default package in a default root JavaProject project = (JavaProject) fromFactory; try { IClasspathEntry entry = project.getClasspathEntryFor(path); if (entry != null) { IPackageFragmentRoot root = project.getPackageFragmentRoot(project.getResource()); Object defaultPkgRoot = this.packageFragments.get(CharOperation.NO_STRINGS); if (defaultPkgRoot == null) { return null; } if (defaultPkgRoot instanceof PackageFragmentRoot && defaultPkgRoot.equals(root)) return ((PackageFragmentRoot) root).getPackageFragment(CharOperation.NO_STRINGS); else { IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) defaultPkgRoot; for (int i = 0; i < roots.length; i++) { if (roots[i].equals(root)) { return ((PackageFragmentRoot) root) .getPackageFragment(CharOperation.NO_STRINGS); } } } } } catch (JavaModelException e) { return null; } return null; case IJavaElement.PACKAGE_FRAGMENT_ROOT: return ((PackageFragmentRoot) fromFactory).getPackageFragment(CharOperation.NO_STRINGS); } } return null; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.HierarchyScope.java
License:Open Source License
private IPath[] computeProjectsAndJars(IType type) throws JavaModelException { HashSet set = new HashSet(); IPackageFragmentRoot root = (IPackageFragmentRoot) type.getPackageFragment().getParent(); if (root.isArchive()) { // add the root set.add(root.getPath());/*from w w w .jav a2s .c o m*/ // add all projects that reference this archive and their dependents IPath rootPath = root.getPath(); IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel(); IJavaProject[] projects = model.getJavaProjects(); HashSet visited = new HashSet(); for (int i = 0; i < projects.length; i++) { JavaProject project = (JavaProject) projects[i]; IClasspathEntry entry = project.getClasspathEntryFor(rootPath); if (entry != null) { // add the project and its binary pkg fragment roots IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); set.add(project.getPath()); for (int k = 0; k < roots.length; k++) { IPackageFragmentRoot pkgFragmentRoot = roots[k]; if (pkgFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) { set.add(pkgFragmentRoot.getPath()); } } // add the dependent projects computeDependents(project, set, visited); } } } else { // add all the project's pkg fragment roots IJavaProject project = (IJavaProject) root.getParent(); IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot pkgFragmentRoot = roots[i]; if (pkgFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) { set.add(pkgFragmentRoot.getPath()); } else { set.add(pkgFragmentRoot.getParent().getPath()); } } // add the dependent projects computeDependents(project, set, new HashSet()); } IPath[] result = new IPath[set.size()]; set.toArray(result); return result; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.IndexSelector.java
License:Open Source License
/** * Returns whether elements of the given project or jar can see the given focus (an IJavaProject or * a JarPackageFragmentRot) either because the focus is part of the project or the jar, or because it is * accessible throught the project's classpath *///from ww w . j av a 2s . c o m public static boolean canSeeFocus(SearchPattern pattern, IPath projectOrJarPath) { try { IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel(); IJavaProject project = getJavaProject(projectOrJarPath, model); IJavaElement[] focuses = getFocusedElementsAndTypes(pattern, project, null); if (focuses.length == 0) return false; if (project != null) { return canSeeFocus(focuses, (JavaProject) project, null); } // projectOrJarPath is a jar // it can see the focus only if it is on the classpath of a project that can see the focus IJavaProject[] allProjects = model.getJavaProjects(); for (int i = 0, length = allProjects.length; i < length; i++) { JavaProject otherProject = (JavaProject) allProjects[i]; IClasspathEntry entry = otherProject.getClasspathEntryFor(projectOrJarPath); if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { if (canSeeFocus(focuses, otherProject, null)) { return true; } } } return false; } catch (JavaModelException e) { return false; } }
From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Creates and returns a handle for the given JAR file, its project being the given project. * The Java model associated with the JAR's project may be * created as a side effect./*from w ww . jav a 2 s .c om*/ * Returns <code>null</code> if unable to create a JAR package fragment root. * (for example, if the JAR file represents a non-Java resource) */ public static IPackageFragmentRoot createJarPackageFragmentRootFrom(File file, JavaProject project) { if (file == null) { return null; } // if (project == null) { // project = JavaCore.create(file.getProject()); // } // Create a jar package fragment root only if on the classpath IPath resourcePath = new Path(file.getPath()); try { IClasspathEntry entry = project.getClasspathEntryFor(resourcePath); if (entry != null) { return project.getPackageFragmentRoot(file); } } catch (JavaModelException e) { // project doesn't exist: return null } return null; }
From source file:org.eclipse.che.jdt.internal.core.search.IndexSelector.java
License:Open Source License
/** * Returns whether elements of the given project or jar can see the given focus (an IJavaProject or * a JarPackageFragmentRot) either because the focus is part of the project or the jar, or because it is * accessible throught the project's classpath *///from ww w.jav a2 s. c o m public static int canSeeFocus(SearchPattern pattern, IPath projectOrJarPath) { try { IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel(); IJavaProject project = getJavaProject(projectOrJarPath, model); IJavaElement[] focuses = getFocusedElementsAndTypes(pattern, project, null); if (focuses.length == 0) return PROJECT_CAN_NOT_SEE_FOCUS; if (project != null) { return canSeeFocus(focuses, (JavaProject) project, null); } // projectOrJarPath is a jar // it can see the focus only if it is on the classpath of a project that can see the focus int result = PROJECT_CAN_NOT_SEE_FOCUS; IJavaProject[] allProjects = model.getJavaProjects(); for (int i = 0, length = allProjects.length; i < length; i++) { JavaProject otherProject = (JavaProject) allProjects[i]; IClasspathEntry entry = otherProject.getClasspathEntryFor(projectOrJarPath); if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { int canSeeFocus = canSeeFocus(focuses, otherProject, null); if (canSeeFocus == PROJECT_CAN_SEE_FOCUS) return PROJECT_CAN_SEE_FOCUS; if (canSeeFocus == PROJECT_SOURCE_CAN_NOT_SEE_FOCUS) result = PROJECT_SOURCE_CAN_NOT_SEE_FOCUS; } } return result; } catch (JavaModelException e) { return PROJECT_CAN_NOT_SEE_FOCUS; } }