Java Code Examples for org.eclipse.jdt.core.ITypeRoot#exists()

The following examples show how to use org.eclipse.jdt.core.ITypeRoot#exists() . 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: JavadocContentAccess.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Gets a reader for an package fragment's Javadoc comment content from the source attachment.
 * The content does contain only the text from the comment without the Javadoc leading star characters.
 * Returns <code>null</code> if the package fragment does not contain a Javadoc comment or if no source is available.
 * @param fragment The package fragment to get the Javadoc of.
 * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
 * does not contain a Javadoc comment or if no source is available
 * @throws JavaModelException is thrown when the package fragment's javadoc can not be accessed
 * @since 3.4
 */
private static Reader internalGetContentReader(IPackageFragment fragment) throws JavaModelException {
	IPackageFragmentRoot root= (IPackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

	//1==> Handle the case when the documentation is present in package-info.java or package-info.class file
	boolean isBinary= root.getKind() == IPackageFragmentRoot.K_BINARY;
	ITypeRoot packageInfo;
	if (isBinary) {
		packageInfo= fragment.getClassFile(PACKAGE_INFO_CLASS);
	} else {
		packageInfo= fragment.getCompilationUnit(PACKAGE_INFO_JAVA);
	}
	if (packageInfo != null && packageInfo.exists()) {
		String source = packageInfo.getSource();
		//the source can be null for some of the class files
		if (source != null) {
			Javadoc javadocNode = getPackageJavadocNode(fragment, source);
			if (javadocNode != null) {
				int start = javadocNode.getStartPosition();
				int length = javadocNode.getLength();
				return new JavaDocCommentReader(source, start, start + length - 1);
			}
		}
	}
	return null;
}
 
Example 2
Source File: OpenAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object getPackageFragmentObjectToOpen(IPackageFragment packageFragment) throws JavaModelException {
	ITypeRoot typeRoot= null;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY)
		typeRoot= (packageFragment).getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	else
		typeRoot= (packageFragment).getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	if (typeRoot.exists())
		return typeRoot;
	
	Object[] nonJavaResources= (packageFragment).getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return file;
			}
		}
	}
	return packageFragment;
}
 
Example 3
Source File: CallHierarchy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static CompilationUnit getCompilationUnitNode(IMember member, boolean resolveBindings) {
	ITypeRoot typeRoot= member.getTypeRoot();
    try {
 	if (typeRoot.exists() && typeRoot.getBuffer() != null) {
ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(typeRoot);
parser.setResolveBindings(resolveBindings);
return (CompilationUnit) parser.createAST(null);
 	}
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    return null;
}
 
Example 4
Source File: ASTProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks whether the given Java element has accessible source.
 *
 * @param je the Java element to test
 * @return <code>true</code> if the element has source
 * @since 3.2
 */
private static boolean hasSource(ITypeRoot je) {
	if (je == null || !je.exists())
		return false;

	try {
		return je.getBuffer() != null;
	} catch (JavaModelException ex) {
		IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.OK, "Error in JDT Core during AST creation", ex);  //$NON-NLS-1$
		JavaPlugin.getDefault().getLog().log(status);
	}
	return false;
}
 
Example 5
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IEditorPart openDeclaration(IJavaElement element) throws PartInitException, JavaModelException {
	if (!(element instanceof IPackageFragment)) {
		return JavaUI.openInEditor(element);
	}
	
	IPackageFragment packageFragment= (IPackageFragment) element;
	ITypeRoot typeRoot;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		typeRoot= packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	} else {
		typeRoot= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	}

	// open the package-info file in editor if one exists
	if (typeRoot.exists())
		return JavaUI.openInEditor(typeRoot);

	// open the package.html file in editor if one exists
	Object[] nonJavaResources= packageFragment.getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return EditorUtility.openInEditor(file, true);
			}
		}
	}

	// select the package in the Package Explorer if there is no associated package Javadoc file
	PackageExplorerPart view= (PackageExplorerPart) JavaPlugin.getActivePage().showView(JavaUI.ID_PACKAGES);
	view.tryToReveal(packageFragment);
	return null;
}