Search in sources :

Example 1 with IPackageFragmentRoot

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 2 with IPackageFragmentRoot

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IStatus(org.eclipse.core.runtime.IStatus) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 3 with IPackageFragmentRoot

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 };
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) MethodPattern(org.eclipse.jdt.internal.core.search.matching.MethodPattern) IType(org.eclipse.jdt.core.IType) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

Example 4 with IPackageFragmentRoot

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 5 with IPackageFragmentRoot

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)171 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)94 Test (org.junit.Test)91 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)77 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)30 File (java.io.File)26 IJavaElement (org.eclipse.jdt.core.IJavaElement)20 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)16 IResource (org.eclipse.core.resources.IResource)16 IPath (org.eclipse.core.runtime.IPath)13 ArrayList (java.util.ArrayList)12 Path (org.eclipse.core.runtime.Path)11 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 CoreException (org.eclipse.core.runtime.CoreException)10 IFile (org.eclipse.core.resources.IFile)8 IType (org.eclipse.jdt.core.IType)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)8 IFolder (org.eclipse.core.resources.IFolder)7