Search in sources :

Example 1 with Path

use of org.eclipse.core.runtime.Path 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 Path

use of org.eclipse.core.runtime.Path in project che by eclipse.

the class Util method getJdkLevel.

/**
     * Get the jdk level of this root.
     * The value can be:
     * <ul>
     * <li>major<<16 + minor : see predefined constants on ClassFileConstants </li>
     * <li><code>0</null> if the root is a source package fragment root or if a Java model exception occured</li>
     * </ul>
     * Returns the jdk level
     */
public static long getJdkLevel(Object targetLibrary) {
    try {
        ClassFileReader reader = null;
        if (targetLibrary instanceof IFolder) {
            // only internal classfolders are allowed
            IFile classFile = findFirstClassFile((IFolder) targetLibrary);
            if (classFile != null)
                reader = Util.newClassFileReader(classFile);
        } else {
            // root is a jar file or a zip file
            ZipFile jar = null;
            try {
                IPath path = null;
                if (targetLibrary instanceof IResource) {
                    path = ((IResource) targetLibrary).getFullPath();
                } else if (targetLibrary instanceof File) {
                    File f = (File) targetLibrary;
                    if (!f.isDirectory()) {
                        path = new Path(((File) targetLibrary).getPath());
                    }
                }
                if (path != null) {
                    jar = JavaModelManager.getJavaModelManager().getZipFile(path);
                    for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
                        ZipEntry member = (ZipEntry) e.nextElement();
                        String entryName = member.getName();
                        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
                            reader = ClassFileReader.read(jar, entryName);
                            break;
                        }
                    }
                }
            } catch (CoreException e) {
            // ignore
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }
        if (reader != null) {
            return reader.getVersion();
        }
    } catch (CoreException e) {
    // ignore
    } catch (ClassFormatException e) {
    // ignore
    } catch (IOException e) {
    // ignore
    }
    return 0;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) Enumeration(java.util.Enumeration) IPath(org.eclipse.core.runtime.IPath) IClassFileReader(org.eclipse.jdt.core.util.IClassFileReader) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with Path

use of org.eclipse.core.runtime.Path in project che by eclipse.

the class Util method isExcluded.

/*
 * Returns whether the given resource matches one of the exclusion patterns.
 * NOTE: should not be asked directly using pkg root pathes
 * @see IClasspathEntry#getExclusionPatterns
 */
public static final boolean isExcluded(File resource, char[][] inclusionPatterns, char[][] exclusionPatterns) {
    IPath path = new Path(resource.getAbsolutePath());
    // ensure that folders are only excluded if all of their children are excluded
    int resourceType = resource.isFile() ? IResource.FILE : IResource.FOLDER;
    return isExcluded(path, inclusionPatterns, exclusionPatterns, resourceType == IResource.FOLDER || resourceType == IResource.PROJECT);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath)

Example 4 with Path

use of org.eclipse.core.runtime.Path 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 5 with Path

use of org.eclipse.core.runtime.Path in project che by eclipse.

the class IndexManager method readIndexMap.

private void readIndexMap() {
    try {
        char[] indexMaps = org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(this.indexNamesMapFile, null);
        char[][] names = CharOperation.splitOn('\n', indexMaps);
        if (names.length >= 3) {
            // First line is DiskIndex signature (see writeIndexMapFile())
            String savedSignature = DiskIndex.SIGNATURE;
            if (savedSignature.equals(new String(names[0]))) {
                for (int i = 1, l = names.length - 1; i < l; i += 2) {
                    IndexLocation indexPath = IndexLocation.createIndexLocation(new URL(new String(names[i])));
                    if (indexPath == null)
                        continue;
                    this.indexLocations.put(new Path(new String(names[i + 1])), indexPath);
                    this.indexStates.put(indexPath, REUSE_STATE);
                }
            }
        }
    } catch (IOException ignored) {
        if (JobManager.VERBOSE)
            //$NON-NLS-1$
            Util.verbose("Failed to read saved index file names");
    }
    return;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) FileIndexLocation(org.eclipse.jdt.internal.core.index.FileIndexLocation) IndexLocation(org.eclipse.jdt.internal.core.index.IndexLocation) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

Path (org.eclipse.core.runtime.Path)1829 IPath (org.eclipse.core.runtime.IPath)1209 IFile (org.eclipse.core.resources.IFile)577 File (java.io.File)445 CoreException (org.eclipse.core.runtime.CoreException)297 IOException (java.io.IOException)278 IProject (org.eclipse.core.resources.IProject)269 URL (java.net.URL)207 ArrayList (java.util.ArrayList)181 IFolder (org.eclipse.core.resources.IFolder)162 Test (org.junit.Test)156 IResource (org.eclipse.core.resources.IResource)150 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)132 InputStream (java.io.InputStream)128 Bundle (org.osgi.framework.Bundle)126 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)111 ByteArrayInputStream (java.io.ByteArrayInputStream)108 IContainer (org.eclipse.core.resources.IContainer)99 IStatus (org.eclipse.core.runtime.IStatus)77 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)65