Search in sources :

Example 1 with ManifestAnalyzer

use of org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer in project bazel-jdt-java-toolchain by salesforce.

the class ClasspathJar method fetchLinkedJars.

@Override
public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporter problemReporter) {
    // expected to be called once only - if multiple calls desired, consider
    // using a cache
    InputStream inputStream = null;
    try {
        initialize();
        ArrayList<Classpath> result = new ArrayList<>();
        ZipEntry manifest = this.zipFile.getEntry(TypeConstants.META_INF_MANIFEST_MF);
        if (manifest != null) {
            // non-null implies regular file
            inputStream = this.zipFile.getInputStream(manifest);
            ManifestAnalyzer analyzer = new ManifestAnalyzer();
            boolean success = analyzer.analyzeManifestContents(inputStream);
            List calledFileNames = analyzer.getCalledFileNames();
            if (problemReporter != null) {
                if (!success || analyzer.getClasspathSectionsCount() == 1 && calledFileNames == null) {
                    problemReporter.invalidClasspathSection(getPath());
                } else if (analyzer.getClasspathSectionsCount() > 1) {
                    problemReporter.multipleClasspathSections(getPath());
                }
            }
            if (calledFileNames != null) {
                Iterator calledFilesIterator = calledFileNames.iterator();
                String directoryPath = getPath();
                int lastSeparator = directoryPath.lastIndexOf(File.separatorChar);
                // potentially empty (see bug 214731)
                directoryPath = directoryPath.substring(0, lastSeparator + 1);
                while (calledFilesIterator.hasNext()) {
                    result.add(new ClasspathJar(new File(directoryPath + (String) calledFilesIterator.next()), this.closeZipFileAtEnd, this.accessRuleSet, this.destinationPath));
                }
            }
        }
        return result;
    } catch (IOException | IllegalArgumentException e) {
        // linked jars
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            // best effort
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ManifestAnalyzer(org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer) Classpath(org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with ManifestAnalyzer

use of org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer in project che by eclipse.

the class ClasspathEntry method getCalledFileNames.

private static List getCalledFileNames(IPath jarPath) {
    Object target = JavaModel.getTarget(jarPath, true);
    if (!(target instanceof IFile || target instanceof File))
        return null;
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    ZipFile zip = null;
    InputStream inputStream = null;
    List calledFileNames = null;
    try {
        zip = manager.getZipFile(jarPath);
        //$NON-NLS-1$
        ZipEntry manifest = zip.getEntry("META-INF/MANIFEST.MF");
        if (manifest == null)
            return null;
        // non-null implies regular file
        ManifestAnalyzer analyzer = new ManifestAnalyzer();
        inputStream = zip.getInputStream(manifest);
        boolean success = analyzer.analyzeManifestContents(inputStream);
        calledFileNames = analyzer.getCalledFileNames();
        if (!success || analyzer.getClasspathSectionsCount() == 1 && calledFileNames == null) {
            if (JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                //$NON-NLS-1$
                Util.verbose("Invalid Class-Path header in manifest of jar file: " + jarPath.toOSString());
            }
            return null;
        } else if (analyzer.getClasspathSectionsCount() > 1) {
            if (JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                //$NON-NLS-1$
                Util.verbose("Multiple Class-Path headers in manifest of jar file: " + jarPath.toOSString());
            }
            return null;
        }
    } catch (CoreException e) {
        // not a zip file
        if (JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
            //$NON-NLS-1$
            Util.verbose("Could not read Class-Path header in manifest of jar file: " + jarPath.toOSString());
            e.printStackTrace();
        }
    } catch (IOException e) {
        // not a zip file
        if (JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
            //$NON-NLS-1$
            Util.verbose("Could not read Class-Path header in manifest of jar file: " + jarPath.toOSString());
            e.printStackTrace();
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            // best effort
            }
        }
        manager.closeZipFile(zip);
    }
    return calledFileNames;
}
Also used : IFile(org.eclipse.core.resources.IFile) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) ManifestAnalyzer(org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 ManifestAnalyzer (org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer)2 Iterator (java.util.Iterator)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 Classpath (org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath)1 NodeList (org.w3c.dom.NodeList)1