Search in sources :

Example 1 with IClassFileReader

use of org.eclipse.jdt.core.util.IClassFileReader in project che by eclipse.

the class ToolFactory method createDefaultClassFileReader.

/**
     * 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 zipFileName doesn't represent a valid zip file or if the zipEntryName
     * is not a valid entry name for the specified zip file or if the bytes don't represent a valid
     * .class file according to the JVM specifications.
     * <p/>
     * The decoding flags are described in IClassFileReader.
     *
     * @param zipFileName
     *         the name of the zip file
     * @param zipEntryName
     *         the name of the entry in the zip file to be read
     * @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(String zipFileName, String zipEntryName, int decodingFlag) {
    ZipFile zipFile = null;
    try {
        if (JavaModelManager.ZIP_ACCESS_VERBOSE) {
            System.out.println("(" + Thread.currentThread() + ") [ToolFactory.createDefaultClassFileReader()] Creating ZipFile on " + //$NON-NLS-1$	//$NON-NLS-2$
            zipFileName);
        }
        zipFile = new ZipFile(zipFileName);
        ZipEntry zipEntry = zipFile.getEntry(zipEntryName);
        if (zipEntry == null) {
            return null;
        }
        if (!zipEntryName.toLowerCase().endsWith(SuffixConstants.SUFFIX_STRING_class)) {
            return null;
        }
        byte[] classFileBytes = Util.getZipEntryByteContent(zipEntry, zipFile);
        return new ClassFileReader(classFileBytes, decodingFlag);
    } catch (ClassFormatException e) {
        return null;
    } catch (IOException e) {
        return null;
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) IClassFileReader(org.eclipse.jdt.core.util.IClassFileReader) ClassFileReader(org.eclipse.jdt.internal.core.util.ClassFileReader) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClassFormatException(org.eclipse.jdt.core.util.ClassFormatException)

Example 2 with IClassFileReader

use of org.eclipse.jdt.core.util.IClassFileReader in project eclipse.jdt.ls by eclipse.

the class SerialVersionHashOperation method calculateSerialVersionId.

public static Long calculateSerialVersionId(ITypeBinding typeBinding, final IProgressMonitor monitor) throws CoreException, IOException {
    try {
        IFile classfileResource = getClassfile(typeBinding);
        if (classfileResource == null) {
            return null;
        }
        InputStream contents = classfileResource.getContents();
        try {
            IClassFileReader cfReader = ToolFactory.createDefaultClassFileReader(contents, IClassFileReader.ALL);
            if (cfReader != null) {
                return calculateSerialVersionId(cfReader);
            }
        } finally {
            contents.close();
        }
        return null;
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IClassFileReader(org.eclipse.jdt.core.util.IClassFileReader) InputStream(java.io.InputStream)

Aggregations

IClassFileReader (org.eclipse.jdt.core.util.IClassFileReader)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 IFile (org.eclipse.core.resources.IFile)1 ClassFormatException (org.eclipse.jdt.core.util.ClassFormatException)1 ClassFileReader (org.eclipse.jdt.internal.core.util.ClassFileReader)1