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

The following examples show how to use org.eclipse.jdt.core.IPackageFragmentRoot#getResource() . 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: JarEntryAwareTrace.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected AbsoluteURI resolvePath(IJavaProject javaProject, SourceRelativeURI path) {
	try {
		for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots())
			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
				IResource resource = root.getResource();
				if (resource instanceof IFolder) {
					IFolder folder = (IFolder) resource;
					String decodedPath = URI.decode(path.toString());
					IResource candidate = folder.findMember(decodedPath);
					if (candidate != null && candidate.exists())
						return new AbsoluteURI(URI.createPlatformResourceURI(resource.getFullPath() + "/" + decodedPath, true));
				}
			}
	} catch (JavaModelException e) {
		log.error("Error resolving path", e);
	}
	return null;
}
 
Example 2
Source File: ClasspathResourceUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the classpath relative path of a given resource.
 *
 * Note: This method does not consider build path exclusion/inclusion filters.
 *
 * @param resource
 * @param javaProject
 * @return the classpath relative path, or null if it could not be determined
 * @throws JavaModelException
 * @see JavaModelSearch#isValidElement(org.eclipse.jdt.core.IJavaElement)
 */
public static IPath getClasspathRelativePathOfResource(IResource resource, IJavaProject javaProject)
    throws JavaModelException {

  IPackageFragmentRoot root = getPackageFragmentRootForResource(resource, javaProject);
  if (root == null) {
    return null;
  }

  IResource rootResource = root.getResource();
  IPath rootProjectPath = rootResource.getProjectRelativePath();
  IPath resourceProjectPath = resource.getProjectRelativePath();

  if (rootProjectPath.isPrefixOf(resourceProjectPath)) {
    return resourceProjectPath.removeFirstSegments(rootProjectPath.segmentCount());
  } else {
    return null;
  }
}
 
Example 3
Source File: LibraryFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
	if (element instanceof IPackageFragmentRoot) {
		IPackageFragmentRoot root= (IPackageFragmentRoot)element;
		if (root.isArchive()) {
			// don't filter out JARs contained in the project itself
			IResource resource= root.getResource();
			if (resource != null) {
				IProject jarProject= resource.getProject();
				IProject container= root.getJavaProject().getProject();
				return container.equals(jarProject);
			}
			return false;
		}
	} else if (element instanceof ClassPathContainer.RequiredProjectWrapper) {
		return false;
	}
	return true;
}
 
Example 4
Source File: SARLProposalProvider.java    From sarl with Apache License 2.0 6 votes vote down vote up
private static String extractProjectPath(IPath fileWorkspacePath, IJavaProject javaProject) {
	if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
		try {
			for (final IPackageFragmentRoot root: javaProject.getPackageFragmentRoots()) {
				if (!root.isArchive() && !root.isExternal()) {
					final IResource resource = root.getResource();
					if (resource != null) {
						final IPath sourceFolderPath = resource.getFullPath();
						if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
							final IPath claspathRelativePath = fileWorkspacePath.makeRelativeTo(sourceFolderPath);
							return claspathRelativePath.removeLastSegments(1)
									.toString().replace("/", "."); //$NON-NLS-1$//$NON-NLS-2$
						}
					}
				}
			}
		} catch (JavaModelException e) {
			Logger.getLogger(SARLProposalProvider.class).error(e.getLocalizedMessage(), e);
		}
	}
	return null;
}
 
Example 5
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void appendFolderLabel(IPackageFragmentRoot root, long flags) {
	IResource resource= root.getResource();
	if (resource == null) {
		appendExternalArchiveLabel(root, flags);
		return;
	}

	boolean rootQualified= getFlag(flags, JavaElementLabels.ROOT_QUALIFIED);
	boolean referencedQualified= getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED) && isReferenced(root);
	if (rootQualified) {
		fBuffer.append(root.getPath().makeRelative().toString());
	} else {
		IPath projectRelativePath= resource.getProjectRelativePath();
		if (projectRelativePath.segmentCount() == 0) {
			fBuffer.append(resource.getName());
			referencedQualified= false;
		} else {
			fBuffer.append(projectRelativePath.toString());
		}

		int offset= fBuffer.length();
		if (referencedQualified) {
			fBuffer.append(JavaElementLabels.CONCAT_STRING);
			fBuffer.append(resource.getProject().getName());
		} else if (getFlag(flags, JavaElementLabels.ROOT_POST_QUALIFIED)) {
			fBuffer.append(JavaElementLabels.CONCAT_STRING);
			fBuffer.append(root.getParent().getElementName());
		} else {
			return;
		}
		if (getFlag(flags, JavaElementLabels.COLORIZE)) {
			fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
		}
	}
}
 
Example 6
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Change createChange(IPackageFragmentRoot root, IJavaProject destination, NewNameProposer nameProposer, INewNameQueries copyQueries) {
	IResource res= root.getResource();
	IProject destinationProject= destination.getProject();
	String newName= nameProposer.createNewName(res, destinationProject);
	INewNameQuery nameQuery;
	if (newName == null)
		nameQuery= copyQueries.createNullQuery();
	else
		nameQuery= copyQueries.createNewPackageFragmentRootNameQuery(root, newName);
	// TODO fix the query problem
	return new CopyPackageFragmentRootChange(root, destinationProject, nameQuery, null);
}
 
Example 7
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Change createChange(IPackageFragmentRoot root, IContainer destination, NewNameProposer nameProposer, INewNameQueries copyQueries) {
	IResource res= root.getResource();

	String newName= nameProposer.createNewName(res, destination);
	INewNameQuery nameQuery;
	if (newName == null) {
		nameQuery= copyQueries.createNullQuery();
	} else {
		nameQuery= copyQueries.createNewResourceNameQuery(res, newName);
	}

	return new CopyResourceChange(res, destination, nameQuery);
}
 
Example 8
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isRenameAvailable(final IPackageFragmentRoot root) throws JavaModelException {
	if (root == null)
		return false;
	if (!Checks.isAvailable(root))
		return false;
	if (root.isArchive())
		return false;
	if (root.isExternal())
		return false;
	if (!root.isConsistent())
		return false;
	if (root.getResource() instanceof IProject)
		return false;
	return true;
}
 
Example 9
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private Change createChange(IPackageFragmentRoot root, IContainer destination, NewNameProposer nameProposer, INewNameQueries copyQueries) {
	IResource res= root.getResource();

	String newName= nameProposer.createNewName(res, destination);
	INewNameQuery nameQuery;
	if (newName == null) {
		nameQuery= copyQueries.createNullQuery();
	} else {
		nameQuery= copyQueries.createNewResourceNameQuery(res, newName);
	}

	return new CopyResourceChange(res, destination, nameQuery);
}
 
Example 10
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void appendInternalArchiveLabel(IPackageFragmentRoot root, long flags) {
	IResource resource= root.getResource();
	boolean rootQualified= getFlag(flags, JavaElementLabels.ROOT_QUALIFIED);
	if (rootQualified) {
		fBuffer.append(root.getPath().makeRelative().toString());
	} else {
		fBuffer.append(root.getElementName());
		int offset= fBuffer.length();
		boolean referencedPostQualified= getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED);
		if (referencedPostQualified && isReferenced(root)) {
			fBuffer.append(JavaElementLabels.CONCAT_STRING);
			fBuffer.append(resource.getParent().getFullPath().makeRelative().toString());
		} else if (getFlag(flags, JavaElementLabels.ROOT_POST_QUALIFIED)) {
			fBuffer.append(JavaElementLabels.CONCAT_STRING);
			fBuffer.append(root.getParent().getPath().makeRelative().toString());
		}
		if (referencedPostQualified) {
			try {
				IClasspathEntry referencingEntry= JavaModelUtil.getClasspathEntry(root).getReferencingEntry();
				if (referencingEntry != null) {
					fBuffer.append(Messages.format(JavaUIMessages.JavaElementLabels_onClassPathOf, new Object[] { Name.CLASS_PATH.toString(), referencingEntry.getPath().lastSegment() }));
				}
			} catch (JavaModelException e) {
				// ignore
			}
		}
		if (getFlag(flags, JavaElementLabels.COLORIZE)) {
			fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
		}
	}
}
 
Example 11
Source File: PackageExplorerContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public Object getHierarchicalPackageParent(IPackageFragment child) {
	String name= child.getElementName();
	IPackageFragmentRoot parent= (IPackageFragmentRoot) child.getParent();
	int index= name.lastIndexOf('.');
	if (index != -1) {
		String realParentName= name.substring(0, index);
		IPackageFragment element= parent.getPackageFragment(realParentName);
		if (element.exists()) {
			try {
				if (fFoldPackages && isEmpty(element) && findSinglePackageChild(element, parent.getChildren()) != null) {
					return getHierarchicalPackageParent(element);
				}
			} catch (JavaModelException e) {
				// ignore
			}
			return element;
		} else { // bug 65240
			IResource resource= element.getResource();
			if (resource != null) {
				return resource;
			}
		}
	}
	if (parent.getResource() instanceof IProject) {
		return parent.getJavaProject();
	}
	return parent;
}
 
Example 12
Source File: CopyModifications.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void copy(IPackageFragmentRoot sourceFolder, CopyArguments javaArgs, CopyArguments resourceArgs) {
	add(sourceFolder, javaArgs, null);
	ResourceMapping mapping= JavaElementResourceMapping.create(sourceFolder);
	if (mapping != null) {
		add(mapping, resourceArgs, null);
	}
	IResource sourceResource= sourceFolder.getResource();
	if (sourceResource != null) {
		getResourceModifications().addCopyDelta(sourceResource, resourceArgs);
		IFile classpath= getClasspathFile((IResource) resourceArgs.getDestination());
		if (classpath != null) {
			getResourceModifications().addChanged(classpath);
		}
	}
}
 
Example 13
Source File: PackageFragmentRootReorgChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IResource getModifiedResource() {
	IPackageFragmentRoot root= getRoot();
	if (root != null) {
		return root.getResource();
	}
	return null;
}
 
Example 14
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isClasspathDelete(IPackageFragmentRoot pkgRoot) {
	IResource res= pkgRoot.getResource();
	if (res == null)
		return true;
	IProject definingProject= res.getProject();
	if (res.getParent() != null && pkgRoot.isArchive() && ! res.getParent().equals(definingProject))
		return true;

	IProject occurringProject= pkgRoot.getJavaProject().getProject();
	return !definingProject.equals(occurringProject);
}
 
Example 15
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void appendFolderLabel(IPackageFragmentRoot root, long flags) {
	IResource resource= root.getResource();
	if (resource == null) {
		appendExternalArchiveLabel(root, flags);
		return;
	}

	boolean rootQualified= getFlag(flags, JavaElementLabels.ROOT_QUALIFIED);
	boolean referencedQualified= getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED) && isReferenced(root);
	if (rootQualified) {
		fBuilder.append(root.getPath().makeRelative().toString());
	} else {
		IPath projectRelativePath= resource.getProjectRelativePath();
		if (projectRelativePath.segmentCount() == 0) {
			fBuilder.append(resource.getName());
			referencedQualified= false;
		} else {
			fBuilder.append(projectRelativePath.toString());
		}

		if (referencedQualified) {
			fBuilder.append(JavaElementLabels.CONCAT_STRING);
			fBuilder.append(resource.getProject().getName());
		} else if (getFlag(flags, JavaElementLabels.ROOT_POST_QUALIFIED)) {
			fBuilder.append(JavaElementLabels.CONCAT_STRING);
			fBuilder.append(root.getParent().getElementName());
		} else {
			return;
		}
	}
}
 
Example 16
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the given package fragment root is
 * referenced. This means it is a descendant of a different project but is referenced
 * by the root's parent. Returns <code>false</code> if the given root
 * doesn't have an underlying resource.
 *
 * @param root the package fragment root
 * @return returns <code>true</code> if the given package fragment root is referenced
 */
private boolean isReferenced(IPackageFragmentRoot root) {
	IResource resource= root.getResource();
	if (resource != null) {
		IProject jarProject= resource.getProject();
		IProject container= root.getJavaProject().getProject();
		return !container.equals(jarProject);
	}
	return false;
}
 
Example 17
Source File: XtendUIValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected String getExpectedPackageName(XtendFile xtendFile) {
	URI fileURI = xtendFile.eResource().getURI();
	for(Pair<IStorage, IProject> storage: storage2UriMapper.getStorages(fileURI)) {
		if(storage.getFirst() instanceof IFile) {
			IPath fileWorkspacePath = storage.getFirst().getFullPath();
			IJavaProject javaProject = JavaCore.create(storage.getSecond());
			if(javaProject != null && javaProject.exists() && javaProject.isOpen()) {
				try {
					for(IPackageFragmentRoot root: javaProject.getPackageFragmentRoots()) {
						if(!root.isArchive() && !root.isExternal()) {
							IResource resource = root.getResource();
							if(resource != null) {
								IPath sourceFolderPath = resource.getFullPath();
								if(sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
									IPath claspathRelativePath = fileWorkspacePath.makeRelativeTo(sourceFolderPath);
									return claspathRelativePath.removeLastSegments(1).toString().replace("/", ".");
								}
							}
						}
					}
				} catch (JavaModelException e) {
					LOG.error("Error resolving expected path for XtendFile", e);
				}
			}
		}
	}
	return null;
}
 
Example 18
Source File: PackageFragmentRootReorgChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected IResource getModifiedResource() {
	IPackageFragmentRoot root= getRoot();
	if (root != null) {
		return root.getResource();
	}
	return null;
}
 
Example 19
Source File: RenameModifications.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public void rename(IPackageFragmentRoot sourceFolder, RenameArguments arguments) {
	add(sourceFolder, arguments, null);
	if (sourceFolder.getResource() != null) {
		getResourceModifications().addRename(sourceFolder.getResource(), arguments);
	}
}
 
Example 20
Source File: RenameModifications.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void rename(IPackageFragmentRoot sourceFolder, RenameArguments arguments) {
	add(sourceFolder, arguments, null);
	if (sourceFolder.getResource() != null) {
		getResourceModifications().addRename(sourceFolder.getResource(), arguments);
	}
}