jdk/src/jdk.rmic/share/classes/sun/rmi/rmic/BatchEnvironment.java
author amlu
Tue, 10 Feb 2015 12:28:02 -0500
changeset 28856 1c525c8d1cf7
parent 27565 729f9700483a
child 36511 9d0388c6b336
permissions -rw-r--r--
8069255: Suppress deprecation warnings in jdk.rmic module (jdk repo) Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28856
1c525c8d1cf7 8069255: Suppress deprecation warnings in jdk.rmic module (jdk repo)
amlu
parents: 27565
diff changeset
     2
 * Copyright (c) 1996, 2015, 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
/*****************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*                    Copyright (c) IBM Corporation 1998                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/*                                                                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/* (C) Copyright IBM Corp. 1998                                              */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*                                                                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*****************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package sun.rmi.rmic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.LinkedHashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.tools.java.ClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * BatchEnvironment for rmic extends javac's version in four ways:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 1. It overrides errorString() to handle looking for rmic-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * error messages in rmic's resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * 2. It provides a mechanism for recording intermediate generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * files so that they can be deleted later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 3. It holds a reference to the Main instance so that generators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * can refer to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * 4. It provides access to the ClassPath passed to the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
28856
1c525c8d1cf7 8069255: Suppress deprecation warnings in jdk.rmic module (jdk repo)
amlu
parents: 27565
diff changeset
    64
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class BatchEnvironment extends sun.tools.javac.BatchEnvironment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /** instance of Main which created this environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Main main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Create a ClassPath object for rmic from a class path string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static ClassPath createClassPath(String classPathString) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    74
        ClassPath[] paths = classPaths(null, classPathString, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return paths[1];
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
     * Create a ClassPath object for rmic from the relevant command line
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    80
     * options for class path and boot class path.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static ClassPath createClassPath(String classPathString,
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    83
                                            String sysClassPathString)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * Previously, this method delegated to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * sun.tools.javac.BatchEnvironment.classPaths method in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * to supply default values for paths not specified on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * command line, expand extensions directories into specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * JAR files, and construct the ClassPath object-- but as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * of the fix for 6473331, which adds support for Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * manifest entries in JAR files, those steps are now handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * here directly, with the help of a Path utility class copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * from the new javac implementation (see below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Path path = new Path();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (sysClassPathString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            sysClassPathString = System.getProperty("sun.boot.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (sysClassPathString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            path.addFiles(sysClassPathString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * Class-Path manifest entries are supported for JAR files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * everywhere except in the boot class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        path.expandJarClassPaths(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * In the application class path, an empty element means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * the current working directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        path.emptyPathDefault(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (classPathString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // The env.class.path property is the user's CLASSPATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // environment variable, and it set by the wrapper (ie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // javac.exe).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            classPathString = System.getProperty("env.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (classPathString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                classPathString = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        path.addFiles(classPathString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return new ClassPath(path.toArray(new String[path.size()]));
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
     * Create a BatchEnvironment for rmic with the given class path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * stream for messages and Main.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public BatchEnvironment(OutputStream out, ClassPath path, Main main) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        super(out, new ClassPath(""), path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                // use empty "sourcePath" (see 4666958)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.main = main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Get the instance of Main which created this environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public Main getMain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Get the ClassPath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public ClassPath getClassPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return binaryPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /** list of generated source files created in this environment */
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   156
    private Vector<File> generatedFiles = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Remember a generated source file generated so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * can be removed later, if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void addGeneratedFile(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        generatedFiles.addElement(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Delete all the generated source files made during the execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * of this environment (those that have been registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * "addGeneratedFile" method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void deleteGeneratedFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        synchronized(generatedFiles) {
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   173
            Enumeration<File> enumeration = generatedFiles.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            while (enumeration.hasMoreElements()) {
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   175
                File file = enumeration.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                file.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            generatedFiles.removeAllElements();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Release resources, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        main = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        generatedFiles = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        super.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Return the formatted, localized string for a named error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and supplied arguments.  For rmic error messages, with names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * being with "rmic.", look up the error message in rmic's resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * bundle; otherwise, defer to java's superclass method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public String errorString(String err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                              Object arg0, Object arg1, Object arg2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (err.startsWith("rmic.") || err.startsWith("warn.rmic.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            String result =  Main.getText(err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                          (arg0 != null ? arg0.toString() : null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                          (arg1 != null ? arg1.toString() : null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                          (arg2 != null ? arg2.toString() : null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (err.startsWith("warn.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                result = "warning: " + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return super.errorString(err, arg0, arg1, arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Utility for building paths of directories and JAR files.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * class was copied from com.sun.tools.javac.util.Paths as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the fix for 6473331, which adds support for Class-Path manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * entries in JAR files.  Diagnostic code is simply commented out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * because rmic silently ignored these conditions historically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static class Path extends LinkedHashSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        private static final long serialVersionUID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private static final boolean warn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        private static class PathIterator implements Collection<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            private int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            private final String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            private final String emptyPathDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            public PathIterator(String path, String emptyPathDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                this.emptyPathDefault = emptyPathDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            public PathIterator(String path) { this(path, null); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            public Iterator<String> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return new Iterator<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        return pos <= path.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    public String next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        int beg = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        int end = path.indexOf(File.pathSeparator, beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        if (end == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            end = path.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        pos = end + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        if (beg == end && emptyPathDefault != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            return emptyPathDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            return path.substring(beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        throw new UnsupportedOperationException();
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
            // required for Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            public boolean add(String o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            public boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            public boolean addAll(Collection<? extends String> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /** Is this the name of a zip file? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        private static boolean isZip(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return new File(name).isFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        private boolean expandJarClassPaths = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        public Path expandJarClassPaths(boolean x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            expandJarClassPaths = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /** What to use when path element is the empty string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        private String emptyPathDefault = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        public Path emptyPathDefault(String x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            emptyPathDefault = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public Path() { super(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public Path addDirectories(String dirs, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (dirs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                for (String dir : new PathIterator(dirs))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    addDirectory(dir, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        public Path addDirectories(String dirs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return addDirectories(dirs, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        private void addDirectory(String dir, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (! new File(dir).isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
//              if (warn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
//                  log.warning(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
//                              "dir.path.element.not.found", dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            for (String direntry : new File(dir).list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                String canonicalized = direntry.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (canonicalized.endsWith(".jar") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    canonicalized.endsWith(".zip"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    addFile(dir + File.separator + direntry, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public Path addFiles(String files, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (files != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                for (String file : new PathIterator(files, emptyPathDefault))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    addFile(file, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        public Path addFiles(String files) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return addFiles(files, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        private void addFile(String file, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (contains(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                /* Discard duplicates and avoid infinite recursion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            File ele = new File(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (! ele.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                /* No such file or directory exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (warn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
//                      log.warning(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
//                          "path.element.not.found", file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (ele.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                /* File is an ordinay file  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                String arcname = file.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                if (! (arcname.endsWith(".zip") ||
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   385
                       arcname.endsWith(".jar") ||
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   386
                       arcname.endsWith(".jimage"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    /* File name don't have right extension */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
//                      if (warn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
//                          log.warning(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
//                              "invalid.archive.file", file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            /* Now what we have left is either a directory or a file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
               confirming to archive naming convention */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            super.add(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (expandJarClassPaths && isZip(file))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                addJarClassPath(file, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // Adds referenced classpath elements from a jar's Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // Manifest entry.  In some future release, we may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // update this code to recognize URLs rather than simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // filenames, but if we do, we should redo all path-related code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        private void addJarClassPath(String jarFileName, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                String jarParent = new File(jarFileName).getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                JarFile jar = new JarFile(jarFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    Manifest man = jar.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    if (man == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    if (attr == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    String path = attr.getValue(Attributes.Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (path == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    for (StringTokenizer st = new StringTokenizer(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        st.hasMoreTokens();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        String elt = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        if (jarParent != null)
5623
5c9695dc013a 6934615: Relative classpaths in jarfile manifests are handled inefficiently by rmic
jjg
parents: 2
diff changeset
   426
                            elt = new File(jarParent, elt).getCanonicalPath();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        addFile(elt, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    jar.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
//              log.error(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
//                        "error.reading.file", jarFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
//                        e.getLocalizedMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}