Java Code Examples for org.eclipse.jdt.core.IPackageFragmentRoot#getResolvedClasspathEntry()

The following examples show how to use org.eclipse.jdt.core.IPackageFragmentRoot#getResolvedClasspathEntry() . 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: 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 2
Source File: SourceJarLocations.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static File getSourceJarPath(IJavaElement element) throws JavaModelException {
	IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);

	if (root == null) {
		return null;
	}

	IClasspathEntry entry = root.getResolvedClasspathEntry();
	IPath sourceAttachment = entry.getSourceAttachmentPath();

	if (sourceAttachment == null) {
		return null; //No source jar could be found
	}

	return sourceAttachment.toFile();
}
 
Example 3
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the classpath entry of the given package fragment root. This is the raw entry, except
 * if the root is a referenced library, in which case it's the resolved entry.
 * 
 * @param root a package fragment root
 * @return the corresponding classpath entry
 * @throws JavaModelException if accessing the entry failed
 * @since 3.6
 */
public static IClasspathEntry getClasspathEntry(IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry rawEntry= root.getRawClasspathEntry();
	int rawEntryKind= rawEntry.getEntryKind();
	switch (rawEntryKind) {
		case IClasspathEntry.CPE_LIBRARY:
		case IClasspathEntry.CPE_VARIABLE:
		case IClasspathEntry.CPE_CONTAINER: // should not happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
			if (root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
				IClasspathEntry resolvedEntry= root.getResolvedClasspathEntry();
				if (resolvedEntry.getReferencingEntry() != null)
					return resolvedEntry;
				else
					return rawEntry;
			}
	}
	return rawEntry;
}
 
Example 4
Source File: JavaDocLocations.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
	if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
		return getProjectJavadocLocation((IJavaProject) element);
	}

	IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
	if (root == null) {
		return null;
	}

	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		IClasspathEntry entry = root.getResolvedClasspathEntry();
		URL javadocLocation = getLibraryJavadocLocation(entry);
		if (javadocLocation != null) {
			return getLibraryJavadocLocation(entry);
		}
		entry = root.getRawClasspathEntry();
		switch (entry.getEntryKind()) {
			case IClasspathEntry.CPE_LIBRARY:
			case IClasspathEntry.CPE_VARIABLE:
				return getLibraryJavadocLocation(entry);
			default:
				return null;
		}
	} else {
		return getProjectJavadocLocation(root.getJavaProject());
	}
}
 
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: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
	if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
		return getProjectJavadocLocation((IJavaProject) element);
	}

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
	if (root == null) {
		return null;
	}

	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		IClasspathEntry entry= root.getResolvedClasspathEntry();
		URL javadocLocation= getLibraryJavadocLocation(entry);
		if (javadocLocation != null) {
			return getLibraryJavadocLocation(entry);
		}
		entry= root.getRawClasspathEntry();
		switch (entry.getEntryKind()) {
			case IClasspathEntry.CPE_LIBRARY:
			case IClasspathEntry.CPE_VARIABLE:
				return getLibraryJavadocLocation(entry);
			default:
				return null;
		}
	} else {
		return getProjectJavadocLocation(root.getJavaProject());
	}
}