Java Code Examples for org.eclipse.jdt.core.IJavaProject#findPackageFragmentRoots()

The following examples show how to use org.eclipse.jdt.core.IJavaProject#findPackageFragmentRoots() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Storage2UriMapperJdtImplTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testResourceInExternalFolder() throws Exception {
	IFolder externalFolder = createExternalFolder("externalFolder");
	IJavaProject project = createJavaProject("foo");
	
	IFile file = externalFolder.getFile("a.indexed");
	file.create(new StringInputStream("content"), true, monitor());
	IFile file2 = externalFolder.getFile("b.nonindexed");
	file2.create(new StringInputStream("content"), true, monitor());
	IClasspathEntry classpathEntry = addExternalFolderToClasspath(project, externalFolder);
	IPackageFragmentRoot packageFragmentRoot = project.findPackageFragmentRoots(classpathEntry)[0];
	Assert.assertTrue(packageFragmentRoot.getPath().toFile().setLastModified(456L));
	Storage2UriMapperJavaImpl impl = getStorage2UriMapper();
	
	IPackageFragmentRoot root = project.getPackageFragmentRoot(externalFolder);
	Map<URI, IStorage> rootData = impl.getAllEntries(root);
	assertEquals(rootData.toString(), 1, rootData.size());
	assertEquals("platform:/resource/.org.eclipse.jdt.core.external.folders/externalFolder/a.indexed", rootData.keySet().iterator().next().toString());
	IFile file3 = externalFolder.getFile("c.indexed");
	file3.create(new StringInputStream("content"), true, monitor());
	rootData = impl.getAllEntries(root);
	assertEquals(rootData.toString(), 2, rootData.size());
}
 
Example 2
Source File: JavaElementUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param root the package fragment root
 * @return array of projects that have the specified root on their classpath
 * @throws JavaModelException if getting the raw classpath or all Java projects fails
 */
public static IJavaProject[] getReferencingProjects(IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry cpe= root.getRawClasspathEntry();
	if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
		cpe= root.getResolvedClasspathEntry();
	}
	IJavaProject[] allJavaProjects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
	List<IJavaProject> result= new ArrayList<>(allJavaProjects.length);
	for (int i= 0; i < allJavaProjects.length; i++) {
		IJavaProject project= allJavaProjects[i];
		IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(cpe);
		if (roots.length > 0) {
			result.add(project);
		}
	}
	return result.toArray(new IJavaProject[result.size()]);
}
 
Example 3
Source File: BusinessObjectModelRepositoryStore.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
protected IRegion regionWithBDM(final IJavaProject javaProject) throws JavaModelException {
    final IRegion newRegion = JavaCore.newRegion();
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    if (rawClasspath != null) {
        final IClasspathEntry repositoryDependenciesClasspathEntry = find(asIterable(rawClasspath),
                repositoryDependenciesEntry(), null);
        final IPackageFragmentRoot[] fragmentRoots = javaProject
                .findPackageFragmentRoots(repositoryDependenciesClasspathEntry);
        if (fragmentRoots != null) {
            final IPackageFragmentRoot packageFragmentRoot = find(asIterable(fragmentRoots),
                    withElementName(BDM_CLIENT_POJO_JAR_NAME), null);
            if (packageFragmentRoot != null) {
                newRegion.add(packageFragmentRoot);
            }
        }
    }
    return newRegion;
}
 
Example 4
Source File: ClasspathUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return the raw classpath entry on the project's classpath that contributes
 * the given type to the project.
 *
 * @param javaProject The java project
 * @param fullyQualifiedName The fully-qualified type name
 * @return The raw classpath entry that contributes the type to the project,
 *         or <code>null</code> if no such classpath entry can be found.
 * @throws JavaModelException
 */
public static IClasspathEntry findRawClasspathEntryFor(
    IJavaProject javaProject, String fullyQualifiedName)
    throws JavaModelException {
  IType type = javaProject.findType(fullyQualifiedName);
  if (type != null) {
    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

    JavaProject jProject = (JavaProject) javaProject;

    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry rawClasspathEntry : rawClasspath) {
      IClasspathEntry[] resolvedClasspath = jProject.resolveClasspath(new IClasspathEntry[] {rawClasspathEntry});

      // was this - which is no longer, internal api refactor
      //IPackageFragmentRoot[] computePackageFragmentRoots = jProject.computePackageFragmentRoots(resolvedClasspath, true, null);

      // now this - from IPackage
      List<IPackageFragmentRoot> fragmentRoots = new ArrayList<IPackageFragmentRoot>();
      for (IClasspathEntry classPathEntry : resolvedClasspath) {
        IPackageFragmentRoot[] foundRoots = javaProject.findPackageFragmentRoots(classPathEntry);
        fragmentRoots.addAll(Arrays.asList(foundRoots));
      }

      IPackageFragmentRoot[] computePackageFragmentRoots = new IPackageFragmentRoot[fragmentRoots.size()];
      fragmentRoots.toArray(computePackageFragmentRoots);

      if (Arrays.asList(computePackageFragmentRoots).contains(
          packageFragmentRoot)) {
        return rawClasspathEntry;
      }
    }

    return packageFragmentRoot.getRawClasspathEntry();
  }

  return null;
}
 
Example 5
Source File: JavaElementUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param root the package fragment root
 * @return array of projects that have the specified root on their classpath
 * @throws JavaModelException if getting the raw classpath or all Java projects fails
 */
public static IJavaProject[] getReferencingProjects(IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry cpe= root.getRawClasspathEntry();
	if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
		cpe= root.getResolvedClasspathEntry();
	IJavaProject[] allJavaProjects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
	List<IJavaProject> result= new ArrayList<IJavaProject>(allJavaProjects.length);
	for (int i= 0; i < allJavaProjects.length; i++) {
		IJavaProject project= allJavaProjects[i];
		IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(cpe);
		if (roots.length > 0)
			result.add(project);
	}
	return result.toArray(new IJavaProject[result.size()]);
}
 
Example 6
Source File: ClassPathContainer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static boolean contains(IJavaProject project, IClasspathEntry entry, IPackageFragmentRoot root) {
	IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(entry);
	for (int i= 0; i < roots.length; i++) {
		if (roots[i].equals(root))
			return true;
	}
	return false;
}
 
Example 7
Source File: ProjectsManager.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private String getWorkspaceInfo() {
	StringBuilder b = new StringBuilder();
	b.append("Projects:\n");
	for (IProject project : getWorkspaceRoot().getProjects()) {
		b.append(project.getName()).append(": ").append(project.getLocation().toOSString()).append('\n');
		if (ProjectUtils.isJavaProject(project)) {
			IJavaProject javaProject = JavaCore.create(project);
			try {
				b.append("  resolved classpath:\n");
				IClasspathEntry[] cpEntries = javaProject.getRawClasspath();
				for (IClasspathEntry cpe : cpEntries) {
					b.append("  ").append(cpe.getPath().toString()).append('\n');
					if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
						IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(cpe);
						for (IPackageFragmentRoot root : roots) {
							b.append("    ").append(root.getPath().toString()).append('\n');
						}
					}
				}
			} catch (CoreException e) {
				// ignore
			}
		} else {
			b.append("  non-Java project\n");
		}
	}
	b.append("Java Runtimes:\n");
	IVMInstall defaultVMInstall = JavaRuntime.getDefaultVMInstall();
	b.append("  default: ");
	if (defaultVMInstall != null) {
		b.append(defaultVMInstall.getInstallLocation().toString());
	} else {
		b.append("-");
	}
	IExecutionEnvironmentsManager eem = JavaRuntime.getExecutionEnvironmentsManager();
	for (IExecutionEnvironment ee : eem.getExecutionEnvironments()) {
		IVMInstall[] vms = ee.getCompatibleVMs();
		b.append("  ").append(ee.getDescription()).append(": ");
		if (vms.length > 0) {
			b.append(vms[0].getInstallLocation().toString());
		} else {
			b.append("-");
		}
		b.append("\n");
	}
	return b.toString();
}
 
Example 8
Source File: CPListElement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static CPListElement create(Object parent, IClasspathEntry curr, boolean newElement, IJavaProject project) {
	IPath path= curr.getPath();
	IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();

	// get the resource
	IResource res= null;
	boolean isMissing= false;
	IPath linkTarget= null;

	switch (curr.getEntryKind()) {
		case IClasspathEntry.CPE_CONTAINER:
			try {
				isMissing= project != null && (JavaCore.getClasspathContainer(path, project) == null);
			} catch (JavaModelException e) {
				isMissing= true;
			}
			break;
		case IClasspathEntry.CPE_VARIABLE:
			IPath resolvedPath= JavaCore.getResolvedVariablePath(path);
			isMissing=  root.findMember(resolvedPath) == null && !resolvedPath.toFile().exists();
			break;
		case IClasspathEntry.CPE_LIBRARY:
			res= root.findMember(path);
			if (res == null) {
				if (!ArchiveFileFilter.isArchivePath(path, true)) {
					if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()
							&& root.getProject(path.segment(0)).exists()) {
						res= root.getFolder(path);
					}
				}

				IPath rawPath= path;
				if (project != null) {
					IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(curr);
					if (roots.length == 1)
						rawPath= roots[0].getPath();
				}
				isMissing= !rawPath.toFile().exists(); // look for external JARs and folders
			} else if (res.isLinked()) {
				linkTarget= res.getLocation();
			}
			break;
		case IClasspathEntry.CPE_SOURCE:
			path= path.removeTrailingSeparator();
			res= root.findMember(path);
			if (res == null) {
				if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()) {
					res= root.getFolder(path);
				}
				isMissing= true;
			} else if (res.isLinked()) {
				linkTarget= res.getLocation();
			}
			break;
		case IClasspathEntry.CPE_PROJECT:
			res= root.findMember(path);
			isMissing= (res == null);
			break;
	}
	CPListElement elem= new CPListElement(parent, project, curr.getEntryKind(), path, newElement, res, linkTarget);
	elem.setExported(curr.isExported());
	elem.setAttribute(SOURCEATTACHMENT, curr.getSourceAttachmentPath());
	elem.setAttribute(OUTPUT, curr.getOutputLocation());
	elem.setAttribute(EXCLUSION, curr.getExclusionPatterns());
	elem.setAttribute(INCLUSION, curr.getInclusionPatterns());
	elem.setAttribute(ACCESSRULES, curr.getAccessRules());
	elem.setAttribute(COMBINE_ACCESSRULES, new Boolean(curr.combineAccessRules()));

	IClasspathAttribute[] extraAttributes= curr.getExtraAttributes();
	for (int i= 0; i < extraAttributes.length; i++) {
		IClasspathAttribute attrib= extraAttributes[i];
		CPListElementAttribute attribElem= elem.findAttributeElement(attrib.getName());
		if (attribElem == null) {
			elem.createAttributeElement(attrib.getName(), attrib.getValue(), false);
		} else {
			attribElem.setValue(attrib.getValue());
		}
	}

	elem.setIsMissing(isMissing);
	return elem;
}