深入解析Java编译器学习笔记

1、Javac文件系统

1.1 文件相关实现类

Archive:压缩包接口

ZipArchive:操作除rt.jar包外的所有JAR包,包括ct.sym包

SymbolArchive:操作ct.sym包

ZipFileIndexArchive:在读取非ct.sym包时会选择该类或者ZipArchive类。

可以指定useOptimizedZip命令的值为false来使用ZipArchive或者SymbolArchive类读取压缩包。详细代码如下

private Archive openArchive(File zipFileName, boolean useOptimizedZip) throws IOException {
        File origZipFileName = zipFileName;
        if (symbolFileEnabled && locations.isDefaultBootClassPathRtJar(zipFileName)) {
            File file = zipFileName.getParentFile().getParentFile(); // ${java.home}
            if (new File(file.getName()).equals(new File("jre")))
                file = file.getParentFile();
            // file == ${jdk.home}
            for (String name : symbolFileLocation)
                file = new File(file, name);
            // file == ${jdk.home}/lib/ct.sym
            if (file.exists())
                zipFileName = file;
        }

        Archive archive;
        try {

            ZipFile zdir = null;

            boolean usePreindexedCache = false;
            String preindexCacheLocation = null;

            if (!useOptimizedZip) {
                zdir = new ZipFile(zipFileName);
            } else {
                usePreindexedCache = options.isSet("usezipindex");
                preindexCacheLocation = options.get("java.io.tmpdir");
                String optCacheLoc = options.get("cachezipindexdir");

                if (optCacheLoc != null && optCacheLoc.length() != 0) {
                    if (optCacheLoc.startsWith("\"")) {
                        if (optCacheLoc.endsWith("\"")) {
                            optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1);
                        }
                        else {
                            optCacheLoc = optCacheLoc.substring(1);
                        }
                    }

                    File cacheDir = new File(optCacheLoc);
                    if (cacheDir.exists() && cacheDir.canWrite()) {
                        preindexCacheLocation = optCacheLoc;
                        if (!preindexCacheLocation.endsWith("/") &&
                            !preindexCacheLocation.endsWith(File.separator)) {
                            preindexCacheLocation += File.separator;
                        }
                    }
                }
            }

            if (origZipFileName == zipFileName) {
                if (!useOptimizedZip) {
                    archive = new ZipArchive(this, zdir);
                } else {
                    archive = new ZipFileIndexArchive(this,
                                    zipFileIndexCache.getZipFileIndex(zipFileName,
                                    null,
                                    usePreindexedCache,
                                    preindexCacheLocation,
                                    options.isSet("writezipindexfiles")));
                }
            } else {
                if (!useOptimizedZip) {
                    archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix);
                } else {
                    archive = new ZipFileIndexArchive(this,
                                    zipFileIndexCache.getZipFileIndex(zipFileName,
                                    symbolFilePrefix,
                                    usePreindexedCache,
                                    preindexCacheLocation,
                                    options.isSet("writezipindexfiles")));
                }
            }
        } catch (FileNotFoundException ex) {
            archive = new MissingArchive(zipFileName);
        } catch (ZipFileIndex.ZipFormatException zfe) {
            throw zfe;
        } catch (IOException ex) {
            if (zipFileName.exists())
                log.error("error.reading.file", zipFileName, getMessage(ex));
            archive = new MissingArchive(zipFileName);
        }

        archives.put(origZipFileName, archive);
        return archive;
    }

每个Archive接口的实现类中都定义了表示压缩包中具体压缩文件的静态内部类

 ZipFileObject:定义在ZipArchive中

ZipFileIndexFileObject:定义在ZipFileIndexArchive中

SymboleFileObject:定义在SymbolArchive中

RegularFileObject:用于Java源文件

参考资料:

https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT

猜你喜欢

转载自blog.csdn.net/wuli2496/article/details/120686205