Java Code Examples for org.eclipse.jdt.core.IPackageFragment#getAncestor()

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#getAncestor() . 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: CreateJavaTypeQuickfixes.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void setPackageName(NewTypeWizardPage page, URI contextUri, String packageName) {
	IJavaProject javaProject = getJavaProject(contextUri);
	String path = contextUri.trimSegments(1).toPlatformString(true);
	try {
		if(javaProject != null) {
			IPackageFragment contextPackageFragment = javaProject.findPackageFragment(new Path(path));
			if (contextPackageFragment != null) {
				IPackageFragmentRoot root = (IPackageFragmentRoot) contextPackageFragment
						.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
				IPackageFragment packageFragment;
				if(!isEmpty(packageName)) {
					packageFragment = root.getPackageFragment(packageName);
				} else {
					packageFragment = contextPackageFragment;
				}
				page.setPackageFragment(packageFragment, true);
				page.setPackageFragmentRoot(root, true);
			}
		}
	} catch (JavaModelException e) {
		LOG.error("Could not find package for " + path, e);
	}
}
 
Example 2
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 3
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 4
Source File: JavaElementResourceMapping.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static ResourceMapping create(final IPackageFragment pack) {
	// test if in an archive
	IPackageFragmentRoot root= (IPackageFragmentRoot)pack.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (!root.isArchive() && !root.isExternal()) {
		return new PackageFragmentResourceMapping(pack);
	}
	return null;
}
 
Example 5
Source File: JavaElementResourceMapping.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ResourceMapping create(final IPackageFragment pack) {
	// test if in an archive
	IPackageFragmentRoot root= (IPackageFragmentRoot)pack.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (!root.isArchive() && !root.isExternal()) {
		return new PackageFragmentResourceMapping(pack);
	}
	return null;
}
 
Example 6
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;
}
 
Example 7
Source File: EclipseResourceTypeDetector.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the given resource is a test resource according to the Java or Maven standard.
 *
 * @param resource the resource to test.
 * @return {@link Boolean#TRUE} if the resource is a test resource. {@link Boolean#FALSE}
 *     if the resource is not a test resource. {@code null} if the detector cannot determine
 *     the type of the resource.
 */
@SuppressWarnings("checkstyle:nestedifdepth")
protected static Boolean isJavaOrMavenTestResource(Resource resource) {
	final URI uri = resource.getURI();
	if (uri.isPlatformResource()) {
		final String platformString = uri.toPlatformString(true);
		final IResource iresource = ResourcesPlugin.getWorkspace().getRoot().findMember(platformString);
		if (iresource != null) {
			final IProject project = iresource.getProject();
			final IJavaProject javaProject = JavaCore.create(project);
			try {
				final IPackageFragment packageFragment = javaProject.findPackageFragment(iresource.getParent().getFullPath());
				final IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
				if (root != null) {
					final IPath rootPath = root.getPath();
					String name = null;
					if (root.isExternal()) {
						name = rootPath.toOSString();
					} else if (javaProject.getElementName().equals(rootPath.segment(0))) {
					    if (rootPath.segmentCount() != 1) {
							name = rootPath.removeFirstSegments(1).makeRelative().toString();
					    }
					} else {
						name = rootPath.toString();
					}
					if (name != null && name.startsWith(SARLConfig.FOLDER_MAVEN_TEST_PREFIX)) {
						return Boolean.TRUE;
					}
				}
			} catch (JavaModelException e) {
				//
			}
		}
	}
	return null;
}