jdk/make/modules/tools/src/com/sun/classanalyzer/ClassPath.java
author ohair
Tue, 07 Sep 2010 15:17:43 -0700
changeset 6326 047748ce0a45
parent 5506 202f599c92aa
permissions -rw-r--r--
6982946: Change make/jprt.properties to defer to JPRT itself for jdk platform list Reviewed-by: kamg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4524
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4524
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4524
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     4
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     8
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    13
 * accompanied this code).
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    14
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4524
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4524
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4524
diff changeset
    21
 * questions.
4524
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    22
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    23
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    24
package com.sun.classanalyzer;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    25
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    26
import java.io.BufferedInputStream;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    27
import java.io.File;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    28
import java.io.FileInputStream;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    29
import java.io.IOException;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    30
import java.io.InputStream;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    31
import java.util.ArrayList;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    32
import java.util.Enumeration;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    33
import java.util.HashSet;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    34
import java.util.List;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    35
import java.util.Set;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    36
import java.util.jar.JarEntry;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    37
import java.util.jar.JarFile;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    38
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    39
/**
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    40
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    41
 * @author mchung
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    42
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    43
public class ClassPath {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    44
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    45
    public class FileInfo {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    46
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    47
        File file;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    48
        JarFile jarfile;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    49
        int classCount;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    50
        long filesize;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    51
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    52
        FileInfo(File f) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    53
            this.file = f;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    54
            this.classCount = 0;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    55
            if (file.getName().endsWith(".jar")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    56
                this.filesize = file.length();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    57
                jarfile = new JarFile(f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    58
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    59
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    60
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    61
        File getFile() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    62
            return file;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    63
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    64
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    65
        JarFile getJarFile() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    66
            return jarfile;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    67
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    68
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    69
        String getName() throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    70
            return file.getCanonicalPath();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    71
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    72
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    73
    private List<FileInfo> fileList = new ArrayList<FileInfo>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    74
    private static ClassPath instance = new ClassPath();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    75
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    76
    static List<FileInfo> getFileInfos() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    77
        return instance.fileList;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    78
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    79
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    80
    static ClassPath setJDKHome(String jdkhome) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    81
        List<File> files = new ArrayList<File>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    82
        File jre = new File(jdkhome, "jre");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    83
        File lib = new File(jdkhome, "lib");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    84
        if (jre.exists() && jre.isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    85
            listFiles(new File(jre, "lib"), ".jar", files);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    86
        } else if (lib.exists() && lib.isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    87
            // either a JRE or a jdk build image
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    88
            listFiles(lib, ".jar", files);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    89
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    90
            File classes = new File(jdkhome, "classes");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    91
            if (classes.exists() && classes.isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    92
                // jdk build outputdir
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    93
                instance.add(classes);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    94
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    95
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    96
            throw new RuntimeException("\"" + jdkhome + "\" not a JDK home");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    97
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    98
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    99
        for (File f : files) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   100
            instance.add(f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   101
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   102
        return instance;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   103
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   104
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   105
    static ClassPath setClassPath(String path) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   106
        if (path.endsWith(".class")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   107
            // one class file
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   108
            File f = new File(path);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   109
            if (!f.exists()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   110
                throw new RuntimeException("Classfile \"" + f + "\" doesn't exist");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   111
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   112
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   113
            instance.add(f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   114
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   115
            List<File> jarFiles = new ArrayList<File>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   116
            String[] locs = path.split(File.pathSeparator);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   117
            for (String p : locs) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   118
                File f = new File(p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   119
                if (!f.exists()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   120
                    throw new RuntimeException("\"" + f + "\" doesn't exist");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   121
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   122
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   123
                if (f.isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   124
                    instance.add(f);  // add the directory to look up .class files
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   125
                    listFiles(f, ".jar", jarFiles);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   126
                } else if (p.endsWith(".jar")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   127
                    // jar files
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   128
                    jarFiles.add(f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   129
                } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   130
                    throw new RuntimeException("Invalid file \"" + f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   131
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   132
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   133
            // add jarFiles if any
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   134
            for (File f : jarFiles) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   135
                instance.add(f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   136
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   137
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   138
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   139
        return instance;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   140
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   141
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   142
    private void add(File f) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   143
        fileList.add(new FileInfo(f));
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   144
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   145
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   146
    public static InputStream open(String pathname) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   147
        for (FileInfo fi : instance.fileList) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   148
            if (fi.getName().endsWith(".jar")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   149
                String path = pathname.replace(File.separatorChar, '/');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   150
                JarEntry e = fi.jarfile.getJarEntry(path);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   151
                if (e != null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   152
                    return fi.jarfile.getInputStream(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   153
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   154
            } else if (fi.getFile().isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   155
                File f = new File(fi.getFile(), pathname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   156
                if (f.exists()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   157
                    return new FileInputStream(f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   158
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   159
            } else if (fi.file.isFile()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   160
                if (fi.getName().endsWith(File.separator + pathname)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   161
                    return new FileInputStream(fi.file);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   162
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   163
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   164
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   165
        return null;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   166
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   167
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   168
    static ClassFileParser parserForClass(String classname) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   169
        String pathname = classname.replace('.', File.separatorChar) + ".class";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   170
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   171
        ClassFileParser cfparser = null;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   172
        for (FileInfo fi : instance.fileList) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   173
            if (fi.getName().endsWith(".class")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   174
                if (fi.getName().endsWith(File.separator + pathname)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   175
                    cfparser = ClassFileParser.newParser(fi.getFile(), true);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   176
                    break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   177
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   178
            } else if (fi.getName().endsWith(".jar")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   179
                JarEntry e = fi.jarfile.getJarEntry(classname.replace('.', '/') + ".class");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   180
                if (e != null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   181
                    cfparser = ClassFileParser.newParser(fi.jarfile.getInputStream(e), e.getSize(), true);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   182
                    break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   183
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   184
            } else if (fi.getFile().isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   185
                File f = new File(fi.getFile(), pathname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   186
                if (f.exists()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   187
                    cfparser = ClassFileParser.newParser(f, true);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   188
                    break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   189
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   190
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   191
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   192
        return cfparser;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   193
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   194
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   195
    public static void parseAllClassFiles() throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   196
        instance.parseFiles();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   197
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   198
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   199
    private void parseFiles() throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   200
        Set<Klass> classes = new HashSet<Klass>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   201
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   202
        int count = 0;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   203
        for (FileInfo fi : fileList) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   204
            // filter out public generated classes (i.e. not public API)
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   205
            // javax.management.remote.rmi._RMIConnectionImpl_Tie
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   206
            // javax.management.remote.rmi._RMIServerImpl_Tie
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   207
            if (fi.getName().endsWith(".class")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   208
                parseClass(fi);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   209
            } else if (fi.getName().endsWith(".jar")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   210
                Enumeration<JarEntry> entries = fi.jarfile.entries();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   211
                while (entries.hasMoreElements()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   212
                    JarEntry e = entries.nextElement();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   213
                    if (e.getName().endsWith(".class")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   214
                        ClassFileParser cfparser = ClassFileParser.newParser(fi.jarfile.getInputStream(e), e.getSize(), true);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   215
                        cfparser.parseDependency(false);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   216
                        fi.classCount++;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   217
                    } else if (!e.isDirectory() && ResourceFile.isResource(e.getName())) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   218
                        ResourceFile.addResource(e.getName(), fi.jarfile.getInputStream(e));
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   219
                    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   220
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   221
            } else if (fi.getFile().isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   222
                List<File> files = new ArrayList<File>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   223
                listFiles(fi.getFile(), "", files);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   224
                for (File f : files) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   225
                    if (f.getName().endsWith(".class")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   226
                        parseClass(fi, f);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   227
                    } else if (!f.isDirectory() && ResourceFile.isResource(f.getCanonicalPath())) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   228
                        String pathname = f.getCanonicalPath();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   229
                        String dir = fi.getName();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   230
                        if (!pathname.startsWith(dir)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   231
                            throw new RuntimeException("Incorrect pathname " + pathname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   232
                        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   233
                        String name = pathname.substring(dir.length() + 1, pathname.length());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   234
                        BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   235
                        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   236
                            ResourceFile.addResource(name, in);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   237
                        } finally {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   238
                            in.close();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   239
                        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   240
                    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   241
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   242
            } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   243
                // should not reach here
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   244
                throw new RuntimeException("Unexpected class path: " + fi.getFile());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   245
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   246
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   247
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   248
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   249
    private void parseClass(FileInfo fi) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   250
        parseClass(fi, fi.getFile());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   251
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   252
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   253
    private void parseClass(FileInfo fi, File f) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   254
        ClassFileParser cfparser = ClassFileParser.newParser(f, true);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   255
        cfparser.parseDependency(false);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   256
        fi.classCount++;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   257
        // need to update the filesize for this directory
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   258
        fi.filesize += fi.getFile().length();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   259
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   260
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   261
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   262
    public static void listFiles(File path, String suffix, List<File> result) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   263
        if (path.isDirectory()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   264
            File[] children = path.listFiles();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   265
            for (File c : children) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   266
                listFiles(c, suffix, result);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   267
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   268
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   269
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   270
            if (suffix.isEmpty() || path.getName().endsWith(suffix)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   271
                result.add(path);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   272
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   273
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   274
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   275
}