langtools/src/share/classes/com/sun/tools/jdeps/PlatformClassPath.java
author mchung
Thu, 14 Mar 2013 10:33:31 -0700
changeset 16550 f20e2521f3df
parent 15030 2d8dec41f029
child 16560 c6c7f0c26568
permissions -rw-r--r--
8005428: Update jdeps to read the same profile information as by javac Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     1
/*
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     4
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    10
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    15
 * accompanied this code).
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    16
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    20
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    23
 * questions.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    24
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    25
package com.sun.tools.jdeps;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    26
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    27
import java.io.File;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    28
import java.io.IOException;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    29
import java.nio.file.FileVisitResult;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    30
import java.nio.file.Files;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    31
import java.nio.file.Path;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    32
import java.nio.file.SimpleFileVisitor;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    33
import java.nio.file.attribute.BasicFileAttributes;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    34
import java.util.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    35
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    36
/**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    37
 * ClassPath for Java SE and JDK
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    38
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    39
class PlatformClassPath {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
    private final static List<Archive> javaHomeArchives = init();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    41
    static List<Archive> getArchives() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    42
        return javaHomeArchives;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    43
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    44
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    45
    static boolean contains(Archive archive) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    46
        return javaHomeArchives.contains(archive);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    47
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    48
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    49
    private static List<Archive> init() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    50
        List<Archive> result = new ArrayList<Archive>();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    51
        String javaHome = System.getProperty("java.home");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    52
        List<File> files = new ArrayList<File>();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    53
        File jre = new File(javaHome, "jre");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    54
        File lib = new File(javaHome, "lib");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    55
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    56
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    57
            if (jre.exists() && jre.isDirectory()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
                result.addAll(addJarFiles(new File(jre, "lib")));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    59
                result.addAll(addJarFiles(lib));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    60
            } else if (lib.exists() && lib.isDirectory()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    61
                // either a JRE or a jdk build image
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    62
                File classes = new File(javaHome, "classes");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    63
                if (classes.exists() && classes.isDirectory()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    64
                    // jdk build outputdir
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    65
                    result.add(new Archive(classes, ClassFileReader.newInstance(classes)));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    66
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    67
                // add other JAR files
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    68
                result.addAll(addJarFiles(lib));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    69
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    70
                throw new RuntimeException("\"" + javaHome + "\" not a JDK home");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    71
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    72
        } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    73
            throw new RuntimeException(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    74
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    75
        return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    76
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    77
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    78
    private static List<Archive> addJarFiles(File f) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    79
        final List<Archive> result = new ArrayList<Archive>();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    80
        final Path root = f.toPath();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    81
        final Path ext = root.resolve("ext");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    82
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    83
            @Override
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    84
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    85
                throws IOException
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    86
            {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    87
                if (dir.equals(root) || dir.equals(ext)) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    88
                    return FileVisitResult.CONTINUE;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    89
                } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    90
                    // skip other cobundled JAR files
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    91
                    return FileVisitResult.SKIP_SUBTREE;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    92
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    93
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    94
            @Override
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    95
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    96
                throws IOException
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    97
            {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    98
                File f = file.toFile();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    99
                String fn = f.getName();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   100
                if (fn.endsWith(".jar") && !fn.equals("alt-rt.jar")) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   101
                    result.add(new Archive(f, ClassFileReader.newInstance(f)));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   102
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   103
                return FileVisitResult.CONTINUE;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   104
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   105
        });
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   106
        return result;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   107
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   108
}