src/share/classes/com/sun/tools/jdeps/PlatformClassPath.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/jdeps/PlatformClassPath.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,113 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.jdeps;
    1.29 +
    1.30 +import java.io.IOException;
    1.31 +import java.nio.file.FileVisitResult;
    1.32 +import java.nio.file.Files;
    1.33 +import java.nio.file.Path;
    1.34 +import java.nio.file.Paths;
    1.35 +import java.nio.file.SimpleFileVisitor;
    1.36 +import java.nio.file.attribute.BasicFileAttributes;
    1.37 +import java.util.*;
    1.38 +
    1.39 +/**
    1.40 + * ClassPath for Java SE and JDK
    1.41 + */
    1.42 +class PlatformClassPath {
    1.43 +    private final static List<Archive> javaHomeArchives = init();
    1.44 +
    1.45 +    static List<Archive> getArchives() {
    1.46 +        return javaHomeArchives;
    1.47 +    }
    1.48 +
    1.49 +    private static List<Archive> init() {
    1.50 +        List<Archive> result = new ArrayList<>();
    1.51 +        Path home = Paths.get(System.getProperty("java.home"));
    1.52 +        try {
    1.53 +            if (home.endsWith("jre")) {
    1.54 +                // jar files in <javahome>/jre/lib
    1.55 +                result.addAll(addJarFiles(home.resolve("lib")));
    1.56 +            } else if (Files.exists(home.resolve("lib"))) {
    1.57 +                // either a JRE or a jdk build image
    1.58 +                Path classes = home.resolve("classes");
    1.59 +                if (Files.isDirectory(classes)) {
    1.60 +                    // jdk build outputdir
    1.61 +                    result.add(new JDKArchive(classes, ClassFileReader.newInstance(classes)));
    1.62 +                }
    1.63 +                // add other JAR files
    1.64 +                result.addAll(addJarFiles(home.resolve("lib")));
    1.65 +            } else {
    1.66 +                throw new RuntimeException("\"" + home + "\" not a JDK home");
    1.67 +            }
    1.68 +            return result;
    1.69 +        } catch (IOException e) {
    1.70 +            throw new Error(e);
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    private static List<Archive> addJarFiles(final Path root) throws IOException {
    1.75 +        final List<Archive> result = new ArrayList<>();
    1.76 +        final Path ext = root.resolve("ext");
    1.77 +        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
    1.78 +            @Override
    1.79 +            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
    1.80 +                throws IOException
    1.81 +            {
    1.82 +                if (dir.equals(root) || dir.equals(ext)) {
    1.83 +                    return FileVisitResult.CONTINUE;
    1.84 +                } else {
    1.85 +                    // skip other cobundled JAR files
    1.86 +                    return FileVisitResult.SKIP_SUBTREE;
    1.87 +                }
    1.88 +            }
    1.89 +            @Override
    1.90 +            public FileVisitResult visitFile(Path p, BasicFileAttributes attrs)
    1.91 +                throws IOException
    1.92 +            {
    1.93 +                String fn = p.getFileName().toString();
    1.94 +                if (fn.endsWith(".jar")) {
    1.95 +                    // JDK may cobundle with JavaFX that doesn't belong to any profile
    1.96 +                    // Treat jfxrt.jar as regular Archive
    1.97 +                    result.add(fn.equals("jfxrt.jar")
    1.98 +                        ? new Archive(p, ClassFileReader.newInstance(p))
    1.99 +                        : new JDKArchive(p, ClassFileReader.newInstance(p)));
   1.100 +                }
   1.101 +                return FileVisitResult.CONTINUE;
   1.102 +            }
   1.103 +        });
   1.104 +        return result;
   1.105 +    }
   1.106 +
   1.107 +    /**
   1.108 +     * A JDK archive is part of the JDK containing the Java SE API
   1.109 +     * or implementation classes (i.e. JDK internal API)
   1.110 +     */
   1.111 +    static class JDKArchive extends Archive {
   1.112 +        JDKArchive(Path p, ClassFileReader reader) {
   1.113 +            super(p, reader);
   1.114 +        }
   1.115 +    }
   1.116 +}

mercurial