use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class HandleFactory method getPkgFragmentRoot.
/**
* Returns the package fragment root that contains the given resource path.
*/
private PackageFragmentRoot getPkgFragmentRoot(String pathString) {
IPath path = new Path(pathString);
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (int i = 0, max = projects.length; i < max; i++) {
try {
IProject project = projects[i];
if (!project.isAccessible() || !project.hasNature(JavaCore.NATURE_ID))
continue;
IJavaProject javaProject = this.javaModel.getJavaProject(project);
IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
for (int j = 0, rootCount = roots.length; j < rootCount; j++) {
PackageFragmentRoot root = (PackageFragmentRoot) roots[j];
if (root.internalPath().isPrefixOf(path) && !Util.isExcluded(path, root.fullInclusionPatternChars(), root.fullExclusionPatternChars(), false)) {
return root;
}
}
} catch (CoreException e) {
// CoreException from hasNature - should not happen since we check that the project is accessible
// JavaModelException from getPackageFragmentRoots - a problem occured while accessing project: nothing we can do, ignore
}
}
return null;
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class ReorgCorrectionsSubProcessor method canModifyAccessRules.
private static boolean canModifyAccessRules(IBinding binding) {
IJavaElement element = binding.getJavaElement();
if (element == null)
return false;
IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
if (root == null)
return false;
try {
IClasspathEntry classpathEntry = root.getRawClasspathEntry();
if (classpathEntry == null)
return false;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
return true;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
ClasspathContainerInitializer classpathContainerInitializer = JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0));
IStatus status = classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject());
return status.isOK();
}
} catch (JavaModelException e) {
return false;
}
return false;
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class IndexSelector method getFocusedElementsAndTypes.
/*
* Create the list of focused jars or projects.
*/
private static IJavaElement[] getFocusedElementsAndTypes(SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes) throws JavaModelException {
if (pattern instanceof MethodPattern) {
// For method pattern, it needs to walk along the focus type super hierarchy
// and add jars/projects of all the encountered types.
IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE);
MethodPattern methodPattern = (MethodPattern) pattern;
String selector = new String(methodPattern.selector);
int parameterCount = methodPattern.parameterCount;
ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
IType[] allTypes = superHierarchy.getAllSupertypes(type);
int length = allTypes.length;
SimpleSet focusSet = new SimpleSet(length + 1);
if (focusElement != null)
focusSet.add(focusElement);
for (int i = 0; i < length; i++) {
IMethod[] methods = allTypes[i].getMethods();
int mLength = methods.length;
for (int m = 0; m < mLength; m++) {
if (parameterCount == methods[m].getNumberOfParameters() && methods[m].getElementName().equals(selector)) {
IPackageFragmentRoot root = (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
IJavaElement element = root.isArchive() ? root : root.getParent();
focusSet.add(element);
if (superTypes != null)
superTypes.add(allTypes[i]);
break;
}
}
}
// Rebuilt a contiguous array
IJavaElement[] focuses = new IJavaElement[focusSet.elementSize];
Object[] values = focusSet.values;
int count = 0;
for (int i = values.length; --i >= 0; ) {
if (values[i] != null) {
focuses[count++] = (IJavaElement) values[i];
}
}
return focuses;
}
if (focusElement == null)
return new IJavaElement[0];
return new IJavaElement[] { focusElement };
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavaSearchScope method packageFragmentRoot.
/**
* @see AbstractJavaSearchScope#packageFragmentRoot(String, int, String)
*/
public IPackageFragmentRoot packageFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPath) {
int index = -1;
boolean isJarFile = jarSeparatorIndex != -1;
if (isJarFile) {
// internal or external jar (case 3, 4, or 5)
String relativePath = resourcePathString.substring(jarSeparatorIndex + 1);
index = indexOf(jarPath, relativePath);
} else {
// resource in workspace (case 1 or 2)
index = indexOf(resourcePathString);
}
if (index >= 0) {
int idx = this.projectIndexes[index];
String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
if (projectPath != null) {
IJavaProject project = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
if (isJarFile) {
IResource resource = JavaModel.getWorkspaceTarget(new Path(jarPath));
if (resource != null)
return project.getPackageFragmentRoot(resource);
return project.getPackageFragmentRoot(jarPath);
}
Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[index] + '/' + this.relativePaths[index]));
if (target != null) {
if (target instanceof IProject) {
return project.getPackageFragmentRoot((IProject) target);
}
IJavaElement element = JavaModelManager.create((IResource) target, project);
return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
}
}
}
return null;
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavaSearchScope method encloses.
/* (non-Javadoc)
* @see IJavaSearchScope#encloses(IJavaElement)
*/
public boolean encloses(IJavaElement element) {
if (this.elements != null) {
for (int i = 0, length = this.elements.size(); i < length; i++) {
IJavaElement scopeElement = (IJavaElement) this.elements.get(i);
IJavaElement searchedElement = element;
while (searchedElement != null) {
if (searchedElement.equals(scopeElement))
return true;
searchedElement = searchedElement.getParent();
}
}
return false;
}
IPackageFragmentRoot root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (root != null && root.isArchive()) {
// external or internal jar
IPath rootPath = root.getPath();
String rootPathToString = rootPath.getDevice() == null ? rootPath.toString() : rootPath.toOSString();
IPath relativePath = getPath(element, true);
return indexOf(rootPathToString, relativePath.toString()) >= 0;
}
// resource in workspace
String fullResourcePathString = getPath(element, false).toString();
return indexOf(fullResourcePathString) >= 0;
}
Aggregations