Search in sources :

Example 1 with JarPackageFragmentRoot

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

the class JavaNavigation method getContent.

public ClassContent getContent(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }
    if (path.startsWith("/")) {
        //non java file
        if (root instanceof JarPackageFragmentRoot) {
            JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
            ZipFile jar = null;
            try {
                jar = jarPackageFragmentRoot.getJar();
                ZipEntry entry = jar.getEntry(path.substring(1));
                if (entry != null) {
                    try (InputStream stream = jar.getInputStream(entry)) {
                        return createContent(IoUtil.readStream(stream), false);
                    } catch (IOException e) {
                        LOG.error("Can't read file content: " + entry.getName(), e);
                    }
                }
            } finally {
                if (jar != null) {
                    JavaModelManager.getJavaModelManager().closeZipFile(jar);
                }
            }
        }
        Object[] resources = root.getNonJavaResources();
        for (Object resource : resources) {
            if (resource instanceof JarEntryFile) {
                JarEntryFile file = (JarEntryFile) resource;
                if (file.getFullPath().toOSString().equals(path)) {
                    return readFileContent(file);
                }
            }
            if (resource instanceof JarEntryDirectory) {
                JarEntryDirectory directory = (JarEntryDirectory) resource;
                JarEntryFile file = findJarFile(directory, path);
                if (file != null) {
                    return readFileContent(file);
                }
            }
        }
    } else {
        return getContent(project, path);
    }
    return null;
}
Also used : JarEntryDirectory(org.eclipse.jdt.internal.core.JarEntryDirectory) ZipFile(java.util.zip.ZipFile) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 2 with JarPackageFragmentRoot

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

the class JavaNavigation method getProjectDependecyJars.

public List<Jar> getProjectDependecyJars(IJavaProject project) throws JavaModelException {
    List<Jar> jars = new ArrayList<>();
    for (IPackageFragmentRoot fragmentRoot : project.getAllPackageFragmentRoots()) {
        if (fragmentRoot instanceof JarPackageFragmentRoot) {
            Jar jar = DtoFactory.getInstance().createDto(Jar.class);
            jar.setId(fragmentRoot.hashCode());
            jar.setName(fragmentRoot.getElementName());
            jars.add(jar);
        }
    }
    return jars;
}
Also used : JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) ArrayList(java.util.ArrayList) Jar(org.eclipse.che.ide.ext.java.shared.Jar) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 3 with JarPackageFragmentRoot

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

the class IndexSelector method canSeeFocus.

private static int canSeeFocus(IJavaElement focus, JavaProject javaProject, char[][][] focusQualifiedNames) {
    try {
        if (focus == null)
            return PROJECT_CAN_NOT_SEE_FOCUS;
        if (focus.equals(javaProject))
            return PROJECT_CAN_SEE_FOCUS;
        if (focus instanceof JarPackageFragmentRoot) {
            // focus is part of a jar
            IPath focusPath = focus.getPath();
            IClasspathEntry[] entries = javaProject.getExpandedClasspath();
            for (int i = 0, length = entries.length; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(focusPath))
                    return PROJECT_CAN_SEE_FOCUS;
            }
            return PROJECT_CAN_NOT_SEE_FOCUS;
        }
        // look for dependent projects
        IPath focusPath = ((JavaProject) focus).getProject().getFullPath();
        IClasspathEntry[] entries = javaProject.getExpandedClasspath();
        for (int i = 0, length = entries.length; i < length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT && entry.getPath().equals(focusPath)) {
                if (focusQualifiedNames != null) {
                    // builder state is usable, hence use it to try to reduce project which can see the focus...
                    State projectState = (State) JavaModelManager.getJavaModelManager().getLastBuiltState(javaProject.getProject(), null);
                    if (projectState != null) {
                        Object[] values = projectState.getReferences().valueTable;
                        int vLength = values.length;
                        for (int j = 0; j < vLength; j++) {
                            if (values[j] == null)
                                continue;
                            ReferenceCollection references = (ReferenceCollection) values[j];
                            if (references.includes(focusQualifiedNames, null, null)) {
                                return PROJECT_CAN_SEE_FOCUS;
                            }
                        }
                        return PROJECT_SOURCE_CAN_NOT_SEE_FOCUS;
                    }
                }
                return PROJECT_CAN_SEE_FOCUS;
            }
        }
        return PROJECT_CAN_NOT_SEE_FOCUS;
    } catch (JavaModelException e) {
        return PROJECT_CAN_NOT_SEE_FOCUS;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) State(org.eclipse.jdt.internal.core.builder.State) ReferenceCollection(org.eclipse.jdt.internal.core.builder.ReferenceCollection)

Example 4 with JarPackageFragmentRoot

use of org.eclipse.jdt.internal.core.JarPackageFragmentRoot in project xtext-eclipse by eclipse.

the class JavaClassPathResourceForIEditorInputFactoryTest method testBug463258_03c.

@Test(expected = CoreException.class)
public void testBug463258_03c() throws Throwable {
    IJavaProject project = createJavaProject("foo");
    IFile file = project.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/bar.testlanguage", "//empty")), true, monitor());
    addJarToClasspath(project, file);
    IPackageFragmentRoot root = new JarPackageFragmentRoot(file, (JavaProject) project) {
    };
    IPackageFragment foo = root.getPackageFragment("foo");
    JarEntryFile fileInJar = new JarEntryFile("bar.testlanguage");
    fileInJar.setParent(foo);
    File jarFile = file.getLocation().toFile();
    assertTrue("exists", jarFile.exists());
    assertTrue("delete", jarFile.delete());
    // simulate an automated refresh
    file.refreshLocal(IResource.DEPTH_ONE, null);
    XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
    try {
        factory.createResource(editorInput);
    } catch (WrappedException e) {
        throw e.getCause();
    }
}
Also used : XtextReadonlyEditorInput(org.eclipse.xtext.ui.editor.XtextReadonlyEditorInput) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) WrappedException(org.eclipse.emf.common.util.WrappedException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) TextFile(org.eclipse.xtext.ui.testing.util.JavaProjectSetupUtil.TextFile) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) IFile(org.eclipse.core.resources.IFile) TextFile(org.eclipse.xtext.ui.testing.util.JavaProjectSetupUtil.TextFile) File(java.io.File) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractWorkbenchTest(org.eclipse.xtext.ui.testing.AbstractWorkbenchTest) Test(org.junit.Test)

Example 5 with JarPackageFragmentRoot

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

the class JavaNavigation method getEntry.

public JarEntry getEntry(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }
    if (path.startsWith("/")) {
        JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
        ZipFile jar = null;
        try {
            jar = jarPackageFragmentRoot.getJar();
            ZipEntry entry = jar.getEntry(path.substring(1));
            if (entry != null) {
                JarEntry result = DtoFactory.getInstance().createDto(JarEntry.class);
                result.setType(JarEntryType.FILE);
                result.setPath(path);
                result.setName(entry.getName().substring(entry.getName().lastIndexOf("/") + 1));
                return result;
            }
        } finally {
            if (jar != null) {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }
        Object[] resources = root.getNonJavaResources();
        for (Object resource : resources) {
            if (resource instanceof JarEntryFile) {
                JarEntryFile file = (JarEntryFile) resource;
                if (file.getFullPath().toOSString().equals(path)) {
                    return getJarEntryResource(file);
                }
            }
            if (resource instanceof JarEntryDirectory) {
                JarEntryDirectory directory = (JarEntryDirectory) resource;
                JarEntryFile file = findJarFile(directory, path);
                if (file != null) {
                    return getJarEntryResource(file);
                }
            }
        }
    } else {
        //java class or file
        IType type = project.findType(path);
        if (type != null && type.isBinary()) {
            IClassFile classFile = type.getClassFile();
            return getJarClass(classFile);
        }
    }
    return null;
}
Also used : JarEntryDirectory(org.eclipse.jdt.internal.core.JarEntryDirectory) IClassFile(org.eclipse.jdt.core.IClassFile) ZipFile(java.util.zip.ZipFile) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) ZipEntry(java.util.zip.ZipEntry) JarEntry(org.eclipse.che.ide.ext.java.shared.JarEntry) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType)

Aggregations

JarPackageFragmentRoot (org.eclipse.jdt.internal.core.JarPackageFragmentRoot)9 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)8 JarEntryFile (org.eclipse.jdt.internal.core.JarEntryFile)6 File (java.io.File)4 IFile (org.eclipse.core.resources.IFile)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 TextFile (org.eclipse.xtext.ui.testing.util.JavaProjectSetupUtil.TextFile)4 Test (org.junit.Test)4 ZipFile (java.util.zip.ZipFile)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ZipEntry (java.util.zip.ZipEntry)2 URI (org.eclipse.emf.common.util.URI)2 WrappedException (org.eclipse.emf.common.util.WrappedException)2 JarEntryDirectory (org.eclipse.jdt.internal.core.JarEntryDirectory)2 XtextReadonlyEditorInput (org.eclipse.xtext.ui.editor.XtextReadonlyEditorInput)2 Storage2UriMapperJavaImpl (org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl)2 AbstractWorkbenchTest (org.eclipse.xtext.ui.testing.AbstractWorkbenchTest)2 ArrayList (java.util.ArrayList)1