jdk/src/share/classes/sun/tools/java/ClassPath.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 12429 91210fbc1747
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12429
91210fbc1747 6610897: New constructor in sun.tools.java.ClassPath builds a path using File.separator instead of File.pathSeparator
youdwei
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class is used to represent a class path, which can contain both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * directories and zip files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class ClassPath {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final char dirSeparator = File.pathSeparatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The original class path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    String pathstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * List of class path entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private ClassPathEntry[] path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Build a class path from the specified path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public ClassPath(String pathstr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        init(pathstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Build a class path from the specified array of class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * element strings.  This constructor, and the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * "init" method, were added as part of the fix for 6473331, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * adds support for Class-Path manifest entries in JAR files to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * rmic.  It is conceivable that the value of a Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * manifest entry will contain a path separator, which would cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * incorrect behavior if the expanded path were passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * previous constructor as a single path-separator-delimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * string; use of this constructor avoids that problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public ClassPath(String[] patharray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        init(patharray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Build a default class path from the path strings specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * the properties sun.boot.class.path and env.class.path, in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public ClassPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String syscp = System.getProperty("sun.boot.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String envcp = System.getProperty("env.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (envcp == null) envcp = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String cp = syscp + File.pathSeparator + envcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        init(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private void init(String pathstr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int i, j, n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Save original class path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.pathstr = pathstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (pathstr.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.path = new ClassPathEntry[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Count the number of path separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        i = n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while ((i = pathstr.indexOf(dirSeparator, i)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            n++; i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // Build the class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        ClassPathEntry[] path = new ClassPathEntry[n+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int len = pathstr.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (i = n = 0; i < len; i = j + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if ((j = pathstr.indexOf(dirSeparator, i)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                j = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (i == j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                path[n] = new ClassPathEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                path[n++].dir = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                File file = new File(pathstr.substring(i, j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        ZipFile zip = new ZipFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        path[n] = new ClassPathEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        path[n++].zip = zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    } catch (ZipException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        // Ignore exceptions, at least for now...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    path[n] = new ClassPathEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    path[n++].dir = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Trim class path to exact size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.path = new ClassPathEntry[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.arraycopy((Object)path, 0, (Object)this.path, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private void init(String[] patharray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Save original class path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (patharray.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            this.pathstr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            StringBuilder sb = new StringBuilder(patharray[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (int i = 1; i < patharray.length; i++) {
12429
91210fbc1747 6610897: New constructor in sun.tools.java.ClassPath builds a path using File.separator instead of File.pathSeparator
youdwei
parents: 5506
diff changeset
   144
                sb.append(File.pathSeparatorChar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                sb.append(patharray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            this.pathstr = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Build the class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ClassPathEntry[] path = new ClassPathEntry[patharray.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        for (String name : patharray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            File file = new File(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    ZipFile zip = new ZipFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    path[n] = new ClassPathEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    path[n++].zip = zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                } catch (ZipException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    // Ignore exceptions, at least for now...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                path[n] = new ClassPathEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                path[n++].dir = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // Trim class path to exact size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.path = new ClassPathEntry[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.arraycopy((Object)path, 0, (Object)this.path, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Find the specified directory in the class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public ClassFile getDirectory(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return getFile(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Load the specified file from the class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public ClassFile getFile(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return getFile(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private final String fileSeparatorChar = "" + File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private ClassFile getFile(String name, boolean isDirectory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        String subdir = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        String basename = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!isDirectory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int i = name.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            subdir = name.substring(0, i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            basename = name.substring(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else if (!subdir.equals("")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                   && !subdir.endsWith(fileSeparatorChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // zip files are picky about "foo" vs. "foo/".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // also, the getFiles caches are keyed with a trailing /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            subdir = subdir + File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            name = subdir;      // Note: isDirectory==true & basename==""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int i = 0; i < path.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (path[i].zip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                String newname = name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                ZipEntry entry = path[i].zip.getEntry(newname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    return new ClassFile(path[i].zip, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                File file = new File(path[i].dir.getPath(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                String list[] = path[i].getFiles(subdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (isDirectory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    if (list.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        return new ClassFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    for (int j = 0; j < list.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        if (basename.equals(list[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            // Don't bother checking !file.isDir,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            // since we only look for names which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            // cannot already be packages (foo.java, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            return new ClassFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns list of files given a package name and extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 12429
diff changeset
   236
    public Enumeration<ClassFile> getFiles(String pkg, String ext) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 12429
diff changeset
   237
        Hashtable<String, ClassFile> files = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        for (int i = path.length; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (path[i].zip != null) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 12429
diff changeset
   240
                Enumeration<? extends ZipEntry> e = path[i].zip.entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    ZipEntry entry = (ZipEntry)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    String name = entry.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    name = name.replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    if (name.startsWith(pkg) && name.endsWith(ext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        files.put(name, new ClassFile(path[i].zip, entry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                String[] list = path[i].getFiles(pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                for (int j = 0; j < list.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    String name = list[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    if (name.endsWith(ext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        name = pkg + File.separatorChar + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        File file = new File(path[i].dir.getPath(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        files.put(name, new ClassFile(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return files.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Release resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        for (int i = path.length; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (path[i].zip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                path[i].zip.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns original class path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return pathstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * A class path entry, which can either be a directory or an open zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
class ClassPathEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    File dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    ZipFile zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 12429
diff changeset
   290
    Hashtable<String, String[]> subdirs = new Hashtable<>(29); // cache of sub-directory listings:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    String[] getFiles(String subdir) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 12429
diff changeset
   292
        String files[] = subdirs.get(subdir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (files == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // search the directory, exactly once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            File sd = new File(dir.getPath(), subdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (sd.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                files = sd.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (files == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    // should not happen, but just in case, fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    files = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (files.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    String nonEmpty[] = { "" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    files = nonEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                files = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            subdirs.put(subdir, files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}