Search in sources :

Example 1 with IPath

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

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

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

use of org.eclipse.core.runtime.IPath 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(IResource resource, char[][] inclusionPatterns, char[][] exclusionPatterns) {
    IPath path = resource.getFullPath();
    // ensure that folders are only excluded if all of their children are excluded
    int resourceType = resource.getType();
    return isExcluded(path, inclusionPatterns, exclusionPatterns, resourceType == IResource.FOLDER || resourceType == IResource.PROJECT);
}
Also used : IPath(org.eclipse.core.runtime.IPath)

Example 5 with IPath

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

the class IndexManager method writeIndexMapFile.

private void writeIndexMapFile() {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(this.indexNamesMapFile));
        writer.write(DiskIndex.SIGNATURE);
        writer.write('\n');
        Object[] keys = this.indexStates.keyTable;
        Object[] states = this.indexStates.valueTable;
        for (int i = 0, l = states.length; i < l; i++) {
            IndexLocation location = (IndexLocation) keys[i];
            if (location != null && states[i] == REUSE_STATE) {
                IPath container = (IPath) this.indexLocations.keyForValue(location);
                if (container != null) {
                    writer.write(location.toString());
                    writer.write('\n');
                    writer.write(container.toOSString());
                    writer.write('\n');
                }
            }
        }
    } catch (IOException ignored) {
        if (JobManager.VERBOSE)
            //$NON-NLS-1$
            Util.verbose("Failed to write saved index file names", System.err);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) FileWriter(java.io.FileWriter) FileIndexLocation(org.eclipse.jdt.internal.core.index.FileIndexLocation) IndexLocation(org.eclipse.jdt.internal.core.index.IndexLocation) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

IPath (org.eclipse.core.runtime.IPath)500 Path (org.eclipse.core.runtime.Path)128 File (java.io.File)106 IFile (org.eclipse.core.resources.IFile)89 IResource (org.eclipse.core.resources.IResource)80 CoreException (org.eclipse.core.runtime.CoreException)74 ArrayList (java.util.ArrayList)72 IFolder (org.eclipse.core.resources.IFolder)63 IProject (org.eclipse.core.resources.IProject)60 IOException (java.io.IOException)57 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)51 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)40 IStatus (org.eclipse.core.runtime.IStatus)33 IJavaProject (org.eclipse.jdt.core.IJavaProject)33 URL (java.net.URL)26 InputStream (java.io.InputStream)23 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)23 Status (org.eclipse.core.runtime.Status)23 HashSet (java.util.HashSet)22 Test (org.junit.Test)22