Search in sources :

Example 6 with JarPackageFragmentRoot

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

the class ToolFactory method createDefaultClassFileReader.

/**
     * Create a classfile reader onto a classfile Java element.
     * Create a default classfile reader, able to expose the internal representation of a given classfile
     * according to the decoding flag used to initialize the reader.
     * Answer null if the file named fileName doesn't represent a valid .class file.
     * <p/>
     * The decoding flags are described in IClassFileReader.
     *
     * @param classfile
     *         the classfile element to introspect
     * @param decodingFlag
     *         the flag used to decode the class file reader.
     * @return a default classfile reader
     * @see org.eclipse.jdt.core.util.IClassFileReader
     */
public static IClassFileReader createDefaultClassFileReader(IClassFile classfile, int decodingFlag) {
    IPackageFragmentRoot root = (IPackageFragmentRoot) classfile.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root != null) {
        try {
            if (root instanceof JarPackageFragmentRoot) {
                String archiveName = null;
                ZipFile jar = null;
                try {
                    jar = ((JarPackageFragmentRoot) root).getJar();
                    archiveName = jar.getName();
                } finally {
                    JavaModelManager.getJavaModelManager().closeZipFile(jar);
                }
                PackageFragment packageFragment = (PackageFragment) classfile.getParent();
                String classFileName = classfile.getElementName();
                String entryName = org.eclipse.jdt.internal.core.util.Util.concatWith(packageFragment.names, classFileName, '/');
                return createDefaultClassFileReader(archiveName, entryName, decodingFlag);
            } else {
                InputStream in = null;
                try {
                    in = ((IFile) ((JavaElement) classfile).resource()).getContents();
                    return createDefaultClassFileReader(in, decodingFlag);
                } finally {
                    if (in != null)
                        try {
                            in.close();
                        } catch (IOException e) {
                        // ignore
                        }
                }
            }
        } catch (CoreException e) {
        // unable to read
        }
    }
    return null;
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) InputStream(java.io.InputStream) IOException(java.io.IOException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 7 with JarPackageFragmentRoot

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

the class JavaClassPathResourceForIEditorInputFactoryTest method testBug463258_03b.

@Test(expected = CoreException.class)
public void testBug463258_03b() throws Throwable {
    IJavaProject project = createJavaProject("foo");
    IFile file = project.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/bar.testlanguage", "//empty")), true, monitor());
    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());
    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 8 with JarPackageFragmentRoot

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

the class Storage2UriMapperJdtImplTest method testBug463258_03c.

@Test
public void testBug463258_03c() throws Exception {
    IJavaProject project = createJavaProject("foo");
    IFile file = project.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/bar.notindexed", "//empty")), true, monitor());
    addJarToClasspath(project, file);
    Storage2UriMapperJavaImpl impl = getStorage2UriMapper();
    IPackageFragmentRoot root = new JarPackageFragmentRoot(file, (JavaProject) project) {
    };
    IPackageFragment foo = root.getPackageFragment("foo");
    JarEntryFile fileInJar = new JarEntryFile("bar.notindexed");
    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);
    URI uri = impl.getUri(fileInJar);
    assertNull(uri);
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) Storage2UriMapperJavaImpl(org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl) 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) URI(org.eclipse.emf.common.util.URI) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 9 with JarPackageFragmentRoot

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

the class Storage2UriMapperJdtImplTest method testBug463258_03b.

@Test
public void testBug463258_03b() throws Exception {
    IJavaProject project = createJavaProject("foo");
    IFile file = project.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/bar.notindexed", "//empty")), true, monitor());
    Storage2UriMapperJavaImpl impl = getStorage2UriMapper();
    IPackageFragmentRoot root = new JarPackageFragmentRoot(file, (JavaProject) project) {
    };
    IPackageFragment foo = root.getPackageFragment("foo");
    JarEntryFile fileInJar = new JarEntryFile("bar.notindexed");
    fileInJar.setParent(foo);
    File jarFile = file.getLocation().toFile();
    assertTrue("exists", jarFile.exists());
    assertTrue("delete", jarFile.delete());
    URI uri = impl.getUri(fileInJar);
    assertNull(uri);
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) Storage2UriMapperJavaImpl(org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl) 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) URI(org.eclipse.emf.common.util.URI) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

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