Search in sources :

Example 1 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry 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 IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry 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 3 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry 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 4 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathBuilderTest method rawClasspathShouldBeContained3Arguments.

@Test
public void rawClasspathShouldBeContained3Arguments() throws Exception {
    createTestProject();
    library.add("/lib");
    sourceFolders.add("/src");
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("/project");
    IJavaProject iJavaProject = JavaCore.create(project);
    classpathBuilder.generateClasspath(iJavaProject, sourceFolders, library);
    List<IClasspathEntry> classpathEntries = Arrays.asList(iJavaProject.getRawClasspath());
    assertThat(classpathEntries).onProperty("path").containsOnly(new Path(JREContainerInitializer.JRE_CONTAINER), new Path("/project/src"), new Path(root + "/project/lib/a.jar"));
}
Also used : Path(org.eclipse.core.runtime.Path) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IProject(org.eclipse.core.resources.IProject) Test(org.testng.annotations.Test) BaseTest(org.eclipse.che.plugin.java.plain.server.BaseTest)

Example 5 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProjectHelper method removeFromClasspath.

public static void removeFromClasspath(IJavaProject jproject, IPath path) throws JavaModelException {
    IClasspathEntry[] oldEntries = jproject.getRawClasspath();
    int nEntries = oldEntries.length;
    ArrayList list = new ArrayList(nEntries);
    for (int i = 0; i < nEntries; i++) {
        IClasspathEntry curr = oldEntries[i];
        if (!path.equals(curr.getPath())) {
            list.add(curr);
        }
    }
    IClasspathEntry[] newEntries = (IClasspathEntry[]) list.toArray(new IClasspathEntry[list.size()]);
    jproject.setRawClasspath(newEntries, null);
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)115 IPath (org.eclipse.core.runtime.IPath)50 IJavaProject (org.eclipse.jdt.core.IJavaProject)40 ArrayList (java.util.ArrayList)34 Path (org.eclipse.core.runtime.Path)25 JavaModelException (org.eclipse.jdt.core.JavaModelException)20 IProject (org.eclipse.core.resources.IProject)17 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 CoreException (org.eclipse.core.runtime.CoreException)11 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 HashMap (java.util.HashMap)9 IResource (org.eclipse.core.resources.IResource)9 IFile (org.eclipse.core.resources.IFile)8 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)6