Search in sources :

Example 1 with PackageFragmentRoot

use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project che by eclipse.

the class HandleFactory method getJarPkgFragmentRoot.

/**
	 * Returns the package fragment root that corresponds to the given jar path.
	 * See createOpenable(...) for the format of the jar path string.
	 * If not null, uses the given scope as a hint for getting Java project handles.
	 */
private PackageFragmentRoot getJarPkgFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPathString, IJavaSearchScope scope) {
    IPath jarPath = new Path(jarPathString);
    Object target = JavaModel.getTarget(jarPath, false);
    if (target instanceof IFile) {
        // internal jar: is it on the classpath of its project?
        //  e.g. org.eclipse.swt.win32/ws/win32/swt.jar
        //        is NOT on the classpath of org.eclipse.swt.win32
        IFile jarFile = (IFile) target;
        JavaProject javaProject = (JavaProject) this.javaModel.getJavaProject(jarFile);
        try {
            IClasspathEntry entry = javaProject.getClasspathEntryFor(jarPath);
            if (entry != null) {
                return (PackageFragmentRoot) javaProject.getPackageFragmentRoot(jarFile);
            }
        } catch (JavaModelException e) {
        // ignore and try to find another project
        }
    }
    // walk projects in the scope and find the first one that has the given jar path in its classpath
    IJavaProject[] projects;
    if (scope != null) {
        if (scope instanceof AbstractJavaSearchScope) {
            PackageFragmentRoot root = (PackageFragmentRoot) ((AbstractJavaSearchScope) scope).packageFragmentRoot(resourcePathString, jarSeparatorIndex, jarPathString);
            if (root != null)
                return root;
        } else {
            IPath[] enclosingProjectsAndJars = scope.enclosingProjectsAndJars();
            int length = enclosingProjectsAndJars.length;
            projects = new IJavaProject[length];
            int index = 0;
            for (int i = 0; i < length; i++) {
                IPath path = enclosingProjectsAndJars[i];
                if (path.segmentCount() == 1) {
                    projects[index++] = this.javaModel.getJavaProject(path.segment(0));
                }
            }
            if (index < length) {
                System.arraycopy(projects, 0, projects = new IJavaProject[index], 0, index);
            }
            PackageFragmentRoot root = getJarPkgFragmentRoot(jarPath, target, projects);
            if (root != null) {
                return root;
            }
        }
    }
    // not found in the scope, walk all projects
    try {
        projects = this.javaModel.getJavaProjects();
    } catch (JavaModelException e) {
        // java model is not accessible
        return null;
    }
    return getJarPkgFragmentRoot(jarPath, target, projects);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AbstractJavaSearchScope(org.eclipse.jdt.internal.core.search.AbstractJavaSearchScope) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Example 2 with PackageFragmentRoot

use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project che by eclipse.

the class Util method getPackageFragment.

public static IPackageFragment getPackageFragment(char[] fileName, int pkgEnd, int jarSeparator) {
    if (jarSeparator != -1) {
        String jarMemento = new String(fileName, 0, jarSeparator);
        PackageFragmentRoot root = (PackageFragmentRoot) JavaCore.create(jarMemento);
        if (pkgEnd == jarSeparator)
            return root.getPackageFragment(CharOperation.NO_STRINGS);
        char[] pkgName = CharOperation.subarray(fileName, jarSeparator + 1, pkgEnd);
        char[][] compoundName = CharOperation.splitOn('/', pkgName);
        return root.getPackageFragment(CharOperation.toStrings(compoundName));
    } else {
        Path path = new Path(new String(fileName, 0, pkgEnd));
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IContainer folder = path.segmentCount() == 1 ? workspaceRoot.getProject(path.lastSegment()) : (IContainer) workspaceRoot.getFolder(path);
        IJavaElement element = JavaCore.create(folder);
        if (element == null)
            return null;
        switch(element.getElementType()) {
            case IJavaElement.PACKAGE_FRAGMENT:
                return (IPackageFragment) element;
            case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                return ((PackageFragmentRoot) element).getPackageFragment(CharOperation.NO_STRINGS);
            case IJavaElement.JAVA_PROJECT:
                PackageFragmentRoot root = (PackageFragmentRoot) ((IJavaProject) element).getPackageFragmentRoot(folder);
                if (root == null)
                    return null;
                return root.getPackageFragment(CharOperation.NO_STRINGS);
        }
        return null;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IContainer(org.eclipse.core.resources.IContainer)

Example 3 with PackageFragmentRoot

use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project che by eclipse.

the class Util method isExcluded.

/*
 * Returns whether the given java element is exluded from its root's classpath.
 * It doesn't check whether the root itself is on the classpath or not
 */
public static final boolean isExcluded(IJavaElement element) {
    int elementType = element.getElementType();
    switch(elementType) {
        case IJavaElement.JAVA_MODEL:
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return false;
        case IJavaElement.PACKAGE_FRAGMENT:
            PackageFragmentRoot root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            IResource resource = ((PackageFragment) element).resource();
            return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars());
        case IJavaElement.COMPILATION_UNIT:
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            resource = element.getResource();
            if (resource == null)
                return false;
            if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()))
                return true;
            return isExcluded(element.getParent());
        default:
            IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
            return cu != null && isExcluded(cu);
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IResource(org.eclipse.core.resources.IResource)

Example 4 with PackageFragmentRoot

use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project che by eclipse.

the class HandleFactory method getJarPkgFragmentRoot.

private PackageFragmentRoot getJarPkgFragmentRoot(IPath jarPath, Object target, IJavaProject[] projects) {
    for (int i = 0, projectCount = projects.length; i < projectCount; i++) {
        try {
            JavaProject javaProject = (JavaProject) projects[i];
            IClasspathEntry classpathEnty = javaProject.getClasspathEntryFor(jarPath);
            if (classpathEnty != null) {
                if (target instanceof IFile) {
                    // internal jar
                    return (PackageFragmentRoot) javaProject.getPackageFragmentRoot((IFile) target);
                } else {
                    // external jar
                    return (PackageFragmentRoot) javaProject.getPackageFragmentRoot0(jarPath);
                }
            }
        } catch (JavaModelException e) {
        // JavaModelException from getResolvedClasspath - a problem occurred while accessing project: nothing we can do, ignore
        }
    }
    return null;
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 5 with PackageFragmentRoot

use of org.eclipse.jdt.internal.core.PackageFragmentRoot 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)

Aggregations

PackageFragmentRoot (org.eclipse.jdt.internal.core.PackageFragmentRoot)12 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)9 IJavaElement (org.eclipse.jdt.core.IJavaElement)7 IJavaProject (org.eclipse.jdt.core.IJavaProject)6 IPath (org.eclipse.core.runtime.IPath)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)5 Path (org.eclipse.core.runtime.Path)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 JavaProject (org.eclipse.jdt.internal.core.JavaProject)4 IResource (org.eclipse.core.resources.IResource)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 JavaElement (org.eclipse.jdt.internal.core.JavaElement)3 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)3 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)2 JavaElementInfo (org.eclipse.jdt.internal.core.JavaElementInfo)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (junit.framework.Test)1