Search in sources :

Example 1 with ClassFormatException

use of org.eclipse.jdt.core.util.ClassFormatException 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)

Aggregations

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