Java Code Examples for org.eclipse.jdt.core.IClasspathEntry#getReferencingEntry()

The following examples show how to use org.eclipse.jdt.core.IClasspathEntry#getReferencingEntry() . 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: 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 2
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
	try {
		IClasspathEntry rawEntry= root.getRawClasspathEntry();
		if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
			IClasspathEntry entry= JavaModelUtil.getClasspathEntry(root);
			if (entry.getReferencingEntry() != null) {
				return false; // not the variable entry itself, but a referenced entry
			}
			IPath path= rawEntry.getPath().makeRelative();

			if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
				int segements= path.segmentCount();
				if (segements > 0) {
					fBuilder.append(path.segment(segements - 1));
					if (segements > 1) {
						fBuilder.append(JavaElementLabels.CONCAT_STRING);
						fBuilder.append(path.removeLastSegments(1).toOSString());
					}
				} else {
					fBuilder.append(path.toString());
				}
			} else {
				fBuilder.append(path.toString());
			}
			fBuilder.append(JavaElementLabels.CONCAT_STRING);
			if (root.isExternal()) {
				fBuilder.append(root.getPath().toOSString());
			} else {
				fBuilder.append(root.getPath().makeRelative().toString());
			}

			return true;
		}
	} catch (JavaModelException e) {
		// problems with class path, ignore (bug 202792)
		return false;
	}
	return false;
}
 
Example 3
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void appendExternalArchiveLabel(IPackageFragmentRoot root, long flags) {
	IPath path;
	IClasspathEntry classpathEntry= null;
	try {
		classpathEntry= JavaModelUtil.getClasspathEntry(root);
		IPath rawPath= classpathEntry.getPath();
		if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER && !rawPath.isAbsolute()) {
			path= rawPath;
		} else {
			path= root.getPath();
		}
	} catch (JavaModelException e) {
		path= root.getPath();
	}
	if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
		int segments= path.segmentCount();
		if (segments > 0) {
			fBuilder.append(path.segment(segments - 1));
			if (segments > 1 || path.getDevice() != null) {
				fBuilder.append(JavaElementLabels.CONCAT_STRING);
				fBuilder.append(path.removeLastSegments(1).toOSString());
			}
			if (classpathEntry != null) {
				IClasspathEntry referencingEntry= classpathEntry.getReferencingEntry();
				if (referencingEntry != null) {
					fBuilder.append(NLS.bind(" (from {0} of {1})", new Object[] { Name.CLASS_PATH.toString(), referencingEntry.getPath().lastSegment() }));
				}
			}
		} else {
			fBuilder.append(path.toOSString());
		}
	} else {
		fBuilder.append(path.toOSString());
	}
}
 
Example 4
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void appendExternalArchiveLabel(IPackageFragmentRoot root, long flags) {
	IPath path;
	IClasspathEntry classpathEntry= null;
	try {
		classpathEntry= JavaModelUtil.getClasspathEntry(root);
		IPath rawPath= classpathEntry.getPath();
		if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER && !rawPath.isAbsolute())
			path= rawPath;
		else
			path= root.getPath();
	} catch (JavaModelException e) {
		path= root.getPath();
	}
	if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
		int segements= path.segmentCount();
		if (segements > 0) {
			fBuffer.append(path.segment(segements - 1));
			int offset= fBuffer.length();
			if (segements > 1 || path.getDevice() != null) {
				fBuffer.append(JavaElementLabels.CONCAT_STRING);
				fBuffer.append(path.removeLastSegments(1).toOSString());
			}
			if (classpathEntry != null) {
				IClasspathEntry referencingEntry= classpathEntry.getReferencingEntry();
				if (referencingEntry != null) {
					fBuffer.append(Messages.format(JavaUIMessages.JavaElementLabels_onClassPathOf, new Object[] { Name.CLASS_PATH.toString(), referencingEntry.getPath().lastSegment() }));
				}
			}
			if (getFlag(flags, JavaElementLabels.COLORIZE)) {
				fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
			}
		} else {
			fBuffer.append(path.toOSString());
		}
	} else {
		fBuffer.append(path.toOSString());
	}
}
 
Example 5
Source File: RemoveFromBuildpathAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canHandle(IStructuredSelection elements) {
	if (elements.size() == 0)
		return false;

	try {
		for (Iterator<?> iter= elements.iterator(); iter.hasNext();) {
			Object element= iter.next();

			if (element instanceof IJavaProject) {
				IJavaProject project= (IJavaProject)element;
				if (!ClasspathModifier.isSourceFolder(project))
					return false;

			} else if (element instanceof IPackageFragmentRoot) {
				IClasspathEntry entry= JavaModelUtil.getClasspathEntry((IPackageFragmentRoot) element);
				if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
					return false;
				if (entry.getReferencingEntry() != null)
					return false;
			} else if (element instanceof ClassPathContainer) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	} catch (JavaModelException e) {
	}
	return false;
}