Example usage for org.eclipse.jdt.internal.core JarPackageFragmentRoot getJar

List of usage examples for org.eclipse.jdt.internal.core JarPackageFragmentRoot getJar

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JarPackageFragmentRoot getJar.

Prototype

public ZipFile getJar() throws CoreException 

Source Link

Document

Returns the underlying ZipFile for this Jar package fragment root.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

public byte[] getBytes() throws JavaModelException {
    JavaElement pkg = (JavaElement) getParent();
    if (pkg instanceof JarPackageFragment) {
        JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
        ZipFile zip = null;/*from  ww w  .  j a v  a  2 s. c om*/
        try {
            zip = root.getJar();
            String entryName = Util.concatWith(((PackageFragment) pkg).names, getElementName(), '/');
            ZipEntry ze = zip.getEntry(entryName);
            if (ze != null) {
                return org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
            }
            throw new JavaModelException(
                    new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
        } catch (IOException ioe) {
            throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
        } catch (CoreException e) {
            if (e instanceof JavaModelException) {
                throw (JavaModelException) e;
            } else {
                throw new JavaModelException(e);
            }
        } finally {
            manager.closeZipFile(zip);
        }
    } else {
        IFile file = (IFile) resource();
        return Util.getResourceContentsAsByteArray(file);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

private IBinaryType getJarBinaryTypeInfo(PackageFragment pkg, boolean fullyInitialize)
        throws CoreException, IOException, ClassFormatException {
    JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
    ZipFile zip = null;//from  ww  w  . j ava2  s .c  o  m
    try {
        zip = root.getJar();
        String entryName = Util.concatWith(pkg.names, getElementName(), '/');
        ZipEntry ze = zip.getEntry(entryName);
        if (ze != null) {
            byte contents[] = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
            String fileName = root.getHandleIdentifier() + IDependent.JAR_FILE_ENTRY_SEPARATOR + entryName;
            return new ClassFileReader(contents, fileName.toCharArray(), fullyInitialize);
        }
    } finally {
        manager.closeZipFile(zip);
    }
    return null;
}

From source file:com.redhat.ceylon.eclipse.core.model.JDTModule.java

License:Open Source License

public synchronized List<IPackageFragmentRoot> getPackageFragmentRoots() {
    if (packageFragmentRoots.isEmpty() && !moduleManager.isExternalModuleLoadedFromSource(getNameAsString())) {
        IJavaProject javaProject = moduleManager.getJavaProject();
        if (javaProject != null) {
            if (this.equals(getLanguageModule())) {
                IClasspathEntry runtimeClasspathEntry = null;

                try {
                    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().segment(0)
                                .equals(CeylonLanguageModuleContainer.CONTAINER_ID)) {
                            runtimeClasspathEntry = entry;
                            break;
                        }/*from   w  w w.ja  va  2  s. c  om*/
                    }

                    if (runtimeClasspathEntry != null) {
                        for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                            if (root.exists() && javaProject.isOnClasspath(root)
                                    && root.getRawClasspathEntry().equals(runtimeClasspathEntry)) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                File jarToSearch = null;
                try {
                    jarToSearch = returnCarFile();
                    if (jarToSearch == null) {
                        RepositoryManager repoMgr = CeylonBuilder
                                .getProjectRepositoryManager(javaProject.getProject());
                        if (repoMgr != null) {
                            jarToSearch = CeylonProjectModulesContainer.getModuleArtifact(repoMgr, this);
                        }
                    }

                    if (jarToSearch != null) {
                        IPackageFragmentRoot root = moduleManager.getJavaProject()
                                .getPackageFragmentRoot(jarToSearch.toString());
                        if (root instanceof JarPackageFragmentRoot) {
                            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
                            if (jarRoot.getJar().getName().equals(jarToSearch.getPath())) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (CoreException e) {
                    if (jarToSearch != null) {
                        System.err.println("Exception trying to get Jar file '" + jarToSearch + "' :");
                    }
                    e.printStackTrace();
                }
            }
        }
    }
    return packageFragmentRoots;
}

From source file:com.redhat.ceylon.eclipse.core.model.loader.JDTModule.java

License:Open Source License

public synchronized List<IPackageFragmentRoot> getPackageFragmentRoots() {
    if (packageFragmentRoots.isEmpty() && jarPath != null) {
        IPackageFragmentRoot root = moduleManager.getJavaProject().getPackageFragmentRoot(jarPath.toString());
        if (root instanceof JarPackageFragmentRoot) {
            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
            try {
                if (jarRoot.getJar().getName().equals(jarPath.getPath())) {
                    packageFragmentRoots.add(root);
                }/*from   w ww  . ja  v a 2  s  .  co  m*/
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return packageFragmentRoots;
}

From source file:com.redhat.ceylon.eclipse.ui.test.headless.ModelAndPhasedUnitsTests.java

License:Open Source License

@Test
public void checkJavaLibrayrUnits() throws CoreException {
    if (compilationError != null) {
        throw compilationError;
    }//from  w w  w  .  j a  v a 2  s . co m

    IJavaProject javaProject = JavaCore.create(mainProject);

    String jarName = null;
    IClassFile javaElement = null;
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        if (root instanceof JarPackageFragmentRoot) {
            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
            IPackageFragment pkg = root.getPackageFragment("java.util.logging");
            if (pkg.exists()) {
                javaElement = pkg.getClassFile("Logger.class");
                jarName = jarRoot.getJar().getName();
                break;
            }
        }
    }

    Module module = modelLoader.findModule(AbstractModelLoader.JAVA_BASE_MODULE_NAME, JDKUtils.jdk.version);

    JavaClassFile javaClass = checkDeclarationUnit(module, "java.util.logging.Logger", JavaClassFile.class,
            jarName + "!/" + "java/util/logging/Logger.class", "java/util/logging/Logger.class",
            "Logger.class");
    Assert.assertEquals("Wrong Java Element : ", javaElement, javaClass.getTypeRoot());
    Assert.assertNull("Project Resource should be null :", javaClass.getProjectResource());
    Assert.assertNull("Root Folder Resource should be null :", javaClass.getRootFolderResource());
    Assert.assertNull("File Resource should be null :", javaClass.getFileResource());
}

From source file:com.redhat.ceylon.eclipse.ui.test.ModelAndPhasedUnitsTests.java

License:Open Source License

@SuppressWarnings("restriction")
@Test//  ww w.j a v  a  2  s.  co  m
public void checkJavaLibrayrUnits() throws CoreException {
    if (compilationError != null) {
        throw compilationError;
    }

    IJavaProject javaProject = JavaCore.create(mainProject);

    String jarName = null;
    IClassFile javaElement = null;
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        if (root instanceof JarPackageFragmentRoot) {
            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
            IPackageFragment pkg = root.getPackageFragment("java.util.logging");
            if (pkg.exists()) {
                javaElement = pkg.getClassFile("Logger.class");
                jarName = jarRoot.getJar().getName();
                break;
            }
        }
    }

    Module module = modelLoader.findModule(AbstractModelLoader.JAVA_BASE_MODULE_NAME,
            AbstractModelLoader.JDK_MODULE_VERSION);

    JavaClassFile javaClass = checkDeclarationUnit(module, "java.util.logging.Logger", JavaClassFile.class,
            jarName + "!/" + "java/util/logging/Logger.class", "java/util/logging/Logger.class",
            "Logger.class");
    Assert.assertEquals("Wrong Java Element : ", javaElement, javaClass.getJavaElement());
    Assert.assertNull("Project Resource should be null :", javaClass.getProjectResource());
    Assert.assertNull("Root Folder Resource should be null :", javaClass.getRootFolderResource());
    Assert.assertNull("File Resource should be null :", javaClass.getFileResource());
}

From source file:org.dev.toos.constcode.model.ConstModel.java

License:Apache License

/**Jar*/
private void iterateIJarEntryResource(IJarEntryResource atElement, final String resourceName,
        Map<String, Object> findRes) throws CoreException {
    if (atElement.getFullPath().toString().endsWith(resourceName) == true) {
        //System.out.println(atElement.getFullPath().toString());
        JarPackageFragmentRoot root = (JarPackageFragmentRoot) atElement.getPackageFragmentRoot();
        String name = root.getJar().getName();
        findRes.put(name, atElement);/*from  w w w.  j a  v  a  2 s . c  om*/
    }
    IJarEntryResource[] resourcesItem = atElement.getChildren();
    if (resourcesItem != null)
        for (IJarEntryResource element : resourcesItem)
            iterateItem(element, resourceName, findRes);
}

From source file:org.dev.toos.constcode.model.ConstModel.java

License:Apache License

/**?jar */
private void loadConst4Jar(String name, IJarEntryResource atElement) {
    //1./*from   w w  w  . j ava  2  s .  co  m*/
    ConstGroup groupModel = this.constGroupMap.get(name);
    if (groupModel == null) {
        InputStream inStream = null;
        String resourceName = atElement.getName();
        try {
            //                if (atElement.)
            JarPackageFragmentRoot root = (JarPackageFragmentRoot) atElement.getPackageFragmentRoot();
            ZipFile jar = root.getJar();
            ZipEntry zipEntry = jar.getEntry(atElement.getFullPath().toString().substring(1));
            if (zipEntry == null)
                return;
            inStream = jar.getInputStream(zipEntry);
            if (inStream == null)
                return;
        } catch (Exception e) {
            Message.errorInfo("load IJarEntryResource " + resourceName + "?", e);
            return;
        }
        //
        groupModel = new JARSourceConstCodeGroup(name, inStream);
        groupModel.initGroup();
        this.constGroupMap.put(name, groupModel);
        this.constGroupList.add(groupModel);
        groupModel.loadData();
    } else
        groupModel.reloadData();
}

From source file:org.eclipse.che.plugin.java.server.JavaNavigation.java

License:Open Source License

public ClassContent getContent(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }/*from w  ww . j  a  v a 2 s.  c  om*/

    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;
}

From source file:org.eclipse.che.plugin.java.server.JavaNavigation.java

License:Open Source License

public JarEntry getEntry(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }//from ww w.java2s. com
    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;
}