langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java
author jjg
Wed, 23 Sep 2009 18:48:13 -0700
changeset 3995 73af8b6fb8bc
parent 3782 ae62279eeb46
child 3998 c66be272f350
permissions -rw-r--r--
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject. 6747645: ZipFileObject.getName is incorrectly deprecated 6885123: JavaFileObject getName issues Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 731
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    26
package com.sun.tools.javac.file;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
    29
import java.io.Closeable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.FileInputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.FileNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.OutputStreamWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.SoftReference;
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
    37
import java.lang.reflect.Constructor;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.net.URI;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
    40
import java.net.URISyntaxException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import java.net.URLClassLoader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import java.nio.ByteBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import java.nio.CharBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import java.nio.channels.FileChannel;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import java.nio.charset.Charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import java.nio.charset.CharsetDecoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import java.nio.charset.CoderResult;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import java.nio.charset.CodingErrorAction;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import java.nio.charset.IllegalCharsetNameException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import java.nio.charset.UnsupportedCharsetException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import java.util.Arrays;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import java.util.zip.ZipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import javax.tools.FileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    67
import javax.tools.StandardJavaFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
import com.sun.tools.javac.code.Source;
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
    70
import com.sun.tools.javac.file.RelativePath.RelativeFile;
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
    71
import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    72
import com.sun.tools.javac.main.JavacOption;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    73
import com.sun.tools.javac.main.OptionName;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    74
import com.sun.tools.javac.main.RecognizedOptions;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    75
import com.sun.tools.javac.util.Context;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    77
import com.sun.tools.javac.util.List;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    78
import com.sun.tools.javac.util.ListBuffer;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    79
import com.sun.tools.javac.util.Log;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    80
import com.sun.tools.javac.util.Options;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
    82
import static javax.tools.StandardLocation.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
import static com.sun.tools.javac.main.OptionName.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 * This class provides access to the source, class and other files
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 * used by the compiler and related tools.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    88
 *
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    89
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    90
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    91
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    92
 * deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
public class JavacFileManager implements StandardJavaFileManager {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    boolean useZipFileIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    public static char[] toArray(CharBuffer buffer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        if (buffer.hasArray())
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            return ((CharBuffer)buffer.compact().flip()).array();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            return buffer.toString().toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * The log to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    protected Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /** Encapsulates knowledge of paths
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    private Paths paths;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private Options options;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   116
    private FSInfo fsInfo;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   117
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    private final File uninited = new File("U N I N I T E D");
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    private final Set<JavaFileObject.Kind> sourceOrClass =
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /** The standard output directory, primarily used for classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     *  Initialized by the "-d" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     *  If classOutDir = null, files are written into same directory as the sources
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     *  they were generated from.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    private File classOutDir = uninited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /** The output directory, used when generating sources while processing annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     *  Initialized by the "-s" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    private File sourceOutDir = uninited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    protected boolean mmappedIO;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    protected boolean ignoreSymbolFile;
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   137
    protected String classLoaderClass;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * User provided charset (through javax.tools).
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    protected Charset charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * Register a Context.Factory to create a JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    public static void preRegister(final Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            public JavaFileManager make() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                return new JavacFileManager(context, true, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * Create a JavacFileManager using a given context, optionally registering
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * it as the JavaFileManager for that context.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public JavacFileManager(Context context, boolean register, Charset charset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        if (register)
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            context.put(JavaFileManager.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        byteBufferCache = new ByteBufferCache();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        this.charset = charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     * Set the context for JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    public void setContext(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        if (paths == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            paths = Paths.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            // Reuse the Paths object as it stores the locations that
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            // have been set with setLocation, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            paths.setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        options = Options.instance(context);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   181
        fsInfo = FSInfo.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        useZipFileIndex = System.getProperty("useJavaUtilZip") == null;// TODO: options.get("useJavaUtilZip") == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        mmappedIO = options.get("mmappedIO") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        ignoreSymbolFile = options.get("ignore.symbol.file") != null;
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   187
        classLoaderClass = options.get("procloader");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    public JavaFileObject getFileForInput(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        return getRegularFile(new File(name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    public JavaFileObject getRegularFile(File file) {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   195
        return new RegularFileObject(this, file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    public JavaFileObject getFileForOutput(String classname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                                           JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                                           JavaFileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        ListBuffer<File> files = new ListBuffer<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        for (String name : names)
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            files.append(new File(nullCheck(name)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        return getJavaFileObjectsFromFiles(files.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    protected JavaFileObject.Kind getKind(String extension) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        if (extension.equals(JavaFileObject.Kind.CLASS.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            return JavaFileObject.Kind.CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        else if (extension.equals(JavaFileObject.Kind.SOURCE.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            return JavaFileObject.Kind.SOURCE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        else if (extension.equals(JavaFileObject.Kind.HTML.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            return JavaFileObject.Kind.HTML;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            return JavaFileObject.Kind.OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    private static boolean isValidName(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        // but the set of keywords depends on the source level, and we don't want
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        // impls of JavaFileManager to have to be dependent on the source level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        // Therefore we simply check that the argument is a sequence of identifiers
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        // separated by ".".
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        for (String s : name.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            if (!SourceVersion.isIdentifier(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
    private static void validateClassName(String className) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        if (!isValidName(className))
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            throw new IllegalArgumentException("Invalid class name: " + className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    private static void validatePackageName(String packageName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        if (packageName.length() > 0 && !isValidName(packageName))
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            throw new IllegalArgumentException("Invalid packageName name: " + packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    public static void testName(String name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                                boolean isValidPackageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                                boolean isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            validatePackageName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            if (!isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                throw new AssertionError("Invalid package name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            printAscii("Valid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            if (isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                throw new AssertionError("Valid package name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            printAscii("Invalid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            validateClassName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            if (!isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                throw new AssertionError("Invalid class name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            printAscii("Valid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            if (isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                throw new AssertionError("Valid class name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            printAscii("Invalid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   276
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    private static void printAscii(String format, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        String message;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            final String ascii = "US-ASCII";
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            message = new String(String.format(null, format, args).getBytes(ascii), ascii);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        } catch (java.io.UnsupportedEncodingException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        System.out.println(message);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * Insert all files in subdirectory `subdirectory' of `directory' which end
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     * in one of the extensions in `extensions' into packageSym.
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    private void listDirectory(File directory,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   293
                               RelativeDirectory subdirectory,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                               Set<JavaFileObject.Kind> fileKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                               boolean recurse,
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                               ListBuffer<JavaFileObject> l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        Archive archive = archives.get(directory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   299
        boolean isFile = fsInfo.isFile(directory);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        if (archive != null || isFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            if (archive == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                    archive = openArchive(directory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                    log.error("error.reading.file",
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                       directory, ex.getLocalizedMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            List<String> files = archive.getFiles(subdirectory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            if (files != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                for (String file; !files.isEmpty(); files = files.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    file = files.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    if (isValidFile(file, fileKinds)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                        l.append(archive.getFileObject(subdirectory, file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            if (recurse) {
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   322
                for (RelativeDirectory s: archive.getSubdirectories()) {
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   323
                    if (subdirectory.contains(s)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                        // Because the archive map is a flat list of directories,
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                        // the enclosing loop will pick up all child subdirectories.
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                        // Therefore, there is no need to recurse deeper.
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                        listDirectory(directory, s, fileKinds, false, l);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        } else {
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   332
            File d = subdirectory.getFile(directory);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            if (!caseMapCheck(d, subdirectory))
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            File[] files = d.listFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            if (files == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            for (File f: files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                String fname = f.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                if (f.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                    if (recurse && SourceVersion.isIdentifier(fname)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                        listDirectory(directory,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   345
                                      new RelativeDirectory(subdirectory, fname),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                                      fileKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                                      recurse,
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                                      l);
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                    if (isValidFile(fname, fileKinds)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                        JavaFileObject fe =
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   353
                            new RegularFileObject(this, fname, new File(d, fname));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                        l.append(fe);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
    private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        int lastDot = s.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        String extn = (lastDot == -1 ? s : s.substring(lastDot));
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        JavaFileObject.Kind kind = getKind(extn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        return fileKinds.contains(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
    private static final boolean fileSystemIsCaseSensitive =
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        File.separatorChar == '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    /** Hack to make Windows case sensitive. Test whether given path
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
     *  ends in a string of characters with the same case as given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     *  Ignore file separators in both path and name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     */
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   375
    private boolean caseMapCheck(File f, RelativePath name) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
        if (fileSystemIsCaseSensitive) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        // Note that getCanonicalPath() returns the case-sensitive
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        // spelled file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        String path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            path = f.getCanonicalPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        char[] pcs = path.toCharArray();
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   386
        char[] ncs = name.path.toCharArray();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        int i = pcs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        int j = ncs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        while (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            while (i >= 0 && pcs[i] == File.separatorChar) i--;
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   391
            while (j >= 0 && ncs[j] == '/') j--;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            if (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                if (pcs[i] != ncs[j]) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                i--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        return j < 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     * An archive provides a flat directory structure of a ZipFile by
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
     * mapping directory names to lists of files (basenames).
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
    public interface Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        void close() throws IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   408
        boolean contains(RelativePath name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   410
        JavaFileObject getFileObject(RelativeDirectory subdirectory, String file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   412
        List<String> getFiles(RelativeDirectory subdirectory);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   414
        Set<RelativeDirectory> getSubdirectories();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    public class MissingArchive implements Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        final File zipFileName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        public MissingArchive(File name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            zipFileName = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   422
        public boolean contains(RelativePath name) {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   423
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   429
        public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   433
        public List<String> getFiles(RelativeDirectory subdirectory) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   437
        public Set<RelativeDirectory> getSubdirectories() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   440
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   441
        @Override
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   442
        public String toString() {
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   443
            return "MissingArchive[" + zipFileName + "]";
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   444
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    /** A directory of zip files already opened.
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
    Map<File, Archive> archives = new HashMap<File,Archive>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   451
    private static final String[] symbolFileLocation = { "lib", "ct.sym" };
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   452
    private static final RelativeDirectory symbolFilePrefix
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   453
            = new RelativeDirectory("META-INF/sym/rt.jar/");
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   454
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    /** Open a new zip file directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
    protected Archive openArchive(File zipFileName) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        Archive archive = archives.get(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        if (archive == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
            File origZipFileName = zipFileName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                File file = zipFileName.getParentFile().getParentFile(); // ${java.home}
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                if (new File(file.getName()).equals(new File("jre")))
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                    file = file.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                // file == ${jdk.home}
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                for (String name : symbolFileLocation)
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                    file = new File(file, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                // file == ${jdk.home}/lib/ct.sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                if (file.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                    zipFileName = file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                ZipFile zdir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                boolean usePreindexedCache = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                String preindexCacheLocation = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                if (!useZipFileIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                    zdir = new ZipFile(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                    usePreindexedCache = options.get("usezipindex") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                    preindexCacheLocation = options.get("java.io.tmpdir");
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                    String optCacheLoc = options.get("cachezipindexdir");
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                    if (optCacheLoc != null && optCacheLoc.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                        if (optCacheLoc.startsWith("\"")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                            if (optCacheLoc.endsWith("\"")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                                optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                           else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                                optCacheLoc = optCacheLoc.substring(1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                        File cacheDir = new File(optCacheLoc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                        if (cacheDir.exists() && cacheDir.canWrite()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                            preindexCacheLocation = optCacheLoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                            if (!preindexCacheLocation.endsWith("/") &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                                !preindexCacheLocation.endsWith(File.separator)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                                preindexCacheLocation += File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                if (origZipFileName == zipFileName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                    if (!useZipFileIndex) {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   511
                        archive = new ZipArchive(this, zdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                    } else {
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   513
                        archive = new ZipFileIndexArchive(this,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   514
                                ZipFileIndex.getZipFileIndex(zipFileName,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   515
                                    null,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   516
                                    usePreindexedCache,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   517
                                    preindexCacheLocation,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   518
                                    options.get("writezipindexfiles") != null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                    if (!useZipFileIndex) {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   523
                        archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
                    else {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   526
                        archive = new ZipFileIndexArchive(this,
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   527
                                ZipFileIndex.getZipFileIndex(zipFileName,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   528
                                    symbolFilePrefix,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   529
                                    usePreindexedCache,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   530
                                    preindexCacheLocation,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   531
                                    options.get("writezipindexfiles") != null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            } catch (FileNotFoundException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                archive = new MissingArchive(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            } catch (IOException ex) {
809
2106a64c0a38 6625520: javac handles missing entries on classpath badly
jjg
parents: 731
diff changeset
   537
                if (zipFileName.exists())
2106a64c0a38 6625520: javac handles missing entries on classpath badly
jjg
parents: 731
diff changeset
   538
                    log.error("error.reading.file", zipFileName, ex.getLocalizedMessage());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                archive = new MissingArchive(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
            archives.put(origZipFileName, archive);
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        return archive;
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
    /** Flush any output resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
        contentCache.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
     * Close the JavaFileManager, releasing resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
            Archive a = i.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            i.remove();
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
                a.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   567
    CharBuffer getCachedContent(JavaFileObject file) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   568
        SoftReference<CharBuffer> r = contentCache.get(file);
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   569
        return (r == null ? null : r.get());
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   570
    }
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   571
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   572
    void cache(JavaFileObject file, CharBuffer cb) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   573
        contentCache.put(file, new SoftReference<CharBuffer>(cb));
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   574
    }
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   575
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   576
    private final Map<JavaFileObject, SoftReference<CharBuffer>> contentCache
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   577
            = new HashMap<JavaFileObject, SoftReference<CharBuffer>>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
    private String defaultEncodingName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    private String getDefaultEncodingName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        if (defaultEncodingName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            defaultEncodingName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        return defaultEncodingName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    protected String getEncodingName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        String encName = options.get(OptionName.ENCODING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        if (encName == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            return getDefaultEncodingName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            return encName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    protected Source getSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        String sourceName = options.get(OptionName.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        Source source = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        if (sourceName != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            source = Source.lookup(sourceName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        return (source != null ? source : Source.DEFAULT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
     * Make a byte buffer from an input stream.
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
     */
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   607
    ByteBuffer makeByteBuffer(InputStream in)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
        throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        int limit = in.available();
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        if (mmappedIO && in instanceof FileInputStream) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
            // Experimental memory mapped I/O
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
            FileInputStream fin = (FileInputStream)in;
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            return fin.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
        if (limit < 1024) limit = 1024;
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        ByteBuffer result = byteBufferCache.get(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        int position = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
        while (in.available() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            if (position >= limit)
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                // expand buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                result = ByteBuffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                    allocate(limit <<= 1).
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                    put((ByteBuffer)result.flip());
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
            int count = in.read(result.array(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                position,
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                limit - position);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
            if (count < 0) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
            result.position(position += count);
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        return (ByteBuffer)result.flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   633
    void recycleByteBuffer(ByteBuffer bb) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   634
        byteBufferCache.put(bb);
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   635
    }
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   636
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
     * A single-element cache of direct byte buffers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    private static class ByteBufferCache {
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        private ByteBuffer cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
        ByteBuffer get(int capacity) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
            if (capacity < 20480) capacity = 20480;
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
            ByteBuffer result =
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
                (cached != null && cached.capacity() >= capacity)
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
                ? (ByteBuffer)cached.clear()
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
                : ByteBuffer.allocate(capacity + capacity>>1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
            cached = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
        void put(ByteBuffer x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
            cached = x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
    }
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   655
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
    private final ByteBufferCache byteBufferCache;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   658
    CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   659
        Charset cs = (this.charset == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
            ? Charset.forName(encodingName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
            : this.charset;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   662
        CharsetDecoder decoder = cs.newDecoder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
        CodingErrorAction action;
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        if (ignoreEncodingErrors)
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
            action = CodingErrorAction.REPLACE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
            action = CodingErrorAction.REPORT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
        return decoder
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
            .onMalformedInput(action)
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
            .onUnmappableCharacter(action);
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
     * Decode a ByteBuffer into a CharBuffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
     */
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   678
    CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        String encodingName = getEncodingName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
        CharsetDecoder decoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
            decoder = getDecoder(encodingName, ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        } catch (IllegalCharsetNameException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
            log.error("unsupported.encoding", encodingName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
            return (CharBuffer)CharBuffer.allocate(1).flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        } catch (UnsupportedCharsetException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            log.error("unsupported.encoding", encodingName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
            return (CharBuffer)CharBuffer.allocate(1).flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        // slightly overestimate the buffer size to avoid reallocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        float factor =
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
            decoder.averageCharsPerByte() * 0.8f +
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
            decoder.maxCharsPerByte() * 0.2f;
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        CharBuffer dest = CharBuffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
            allocate(10 + (int)(inbuf.remaining()*factor));
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
            CoderResult result = decoder.decode(inbuf, dest, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            dest.flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
            if (result.isUnderflow()) { // done reading
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
                // make sure there is at least one extra character
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                if (dest.limit() == dest.capacity()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
                    dest.flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                return dest;
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
            } else if (result.isOverflow()) { // buffer too small; expand
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
                int newCapacity =
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                    10 + dest.capacity() +
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
                    (int)(inbuf.remaining()*decoder.maxCharsPerByte());
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
                dest = CharBuffer.allocate(newCapacity).put(dest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            } else if (result.isMalformed() || result.isUnmappable()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                // bad character in input
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                // report coding error (warn only pre 1.5)
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                if (!getSource().allowEncodingErrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                    log.error(new SimpleDiagnosticPosition(dest.limit()),
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                              "illegal.char.for.encoding",
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                              charset == null ? encodingName : charset.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                    log.warning(new SimpleDiagnosticPosition(dest.limit()),
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
                                "illegal.char.for.encoding",
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
                                charset == null ? encodingName : charset.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
                // skip past the coding error
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
                inbuf.position(inbuf.position() + result.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
                // undo the flip() to prepare the output buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
                // for more translation
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
                dest.position(dest.limit());
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
                dest.limit(dest.capacity());
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
                dest.put((char)0xfffd); // backward compatible
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
                throw new AssertionError(result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        // unreached
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    public ClassLoader getClassLoader(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
        ListBuffer<URL> lb = new ListBuffer<URL>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        for (File f: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
                lb.append(f.toURI().toURL());
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
            } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
                throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        }
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   756
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   757
        URL[] urls = lb.toArray(new URL[lb.size()]);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   758
        ClassLoader thisClassLoader = getClass().getClassLoader();
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   759
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   760
        // Bug: 6558476
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   761
        // Ideally, ClassLoader should be Closeable, but before JDK7 it is not.
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   762
        // On older versions, try the following, to get a closeable classloader.
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   763
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   764
        // 1: Allow client to specify the class to use via hidden option
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   765
        if (classLoaderClass != null) {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   766
            try {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   767
                Class<? extends ClassLoader> loader =
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   768
                        Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   769
                Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   770
                Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   771
                return constr.newInstance(new Object[] { urls, thisClassLoader });
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   772
            } catch (Throwable t) {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   773
                // ignore errors loading user-provided class loader, fall through
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   774
            }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   775
        }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   776
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   777
        // 2: If URLClassLoader implements Closeable, use that.
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   778
        if (Closeable.class.isAssignableFrom(URLClassLoader.class))
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   779
            return new URLClassLoader(urls, thisClassLoader);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   780
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   781
        // 3: Try using private reflection-based CloseableURLClassLoader
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   782
        try {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   783
            return new CloseableURLClassLoader(urls, thisClassLoader);
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   784
        } catch (Throwable t) {
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   785
            // ignore errors loading workaround class loader, fall through
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   786
        }
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   787
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   788
        // 4: If all else fails, use plain old standard URLClassLoader
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   789
        return new URLClassLoader(urls, thisClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
    public Iterable<JavaFileObject> list(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                                         String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
                                         Set<JavaFileObject.Kind> kinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
                                         boolean recurse)
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
        nullCheck(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
        nullCheck(kinds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            return List.nil();
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   805
        RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
        ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
        for (File directory : path)
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            listDirectory(directory, subdirectory, kinds, recurse, results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
        return results.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
    public String inferBinaryName(Location location, JavaFileObject file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        file.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        location.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
        // Need to match the path semantics of list(location, ...)
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
        if (path == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   823
        if (file instanceof BaseFileObject) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   824
            return ((BaseFileObject) file).inferBinaryName(path);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
            throw new IllegalArgumentException(file.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
    public boolean isSameFile(FileObject a, FileObject b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        nullCheck(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        nullCheck(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        if (!(a instanceof BaseFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            throw new IllegalArgumentException("Not supported: " + a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
        if (!(b instanceof BaseFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
            throw new IllegalArgumentException("Not supported: " + b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        return a.equals(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
    public boolean handleOption(String current, Iterator<String> remaining) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        for (JavacOption o: javacFileManagerOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
            if (o.matches(current))  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                if (o.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
                    if (remaining.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
                        if (!o.process(options, current, remaining.next()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
                    if (!o.process(options, current))
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
                // operand missing, or process returned false
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
                throw new IllegalArgumentException(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
        private static JavacOption[] javacFileManagerOptions =
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
            RecognizedOptions.getJavacFileManagerOptions(
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
            new RecognizedOptions.GrumpyHelper());
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
    public int isSupportedOption(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
        for (JavacOption o : javacFileManagerOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
            if (o.matches(option))
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
                return o.hasArg() ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
    public boolean hasLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
        return getLocation(location) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
    public JavaFileObject getJavaFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                                              String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                                              JavaFileObject.Kind kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        if (!sourceOrClass.contains(kind))
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            throw new IllegalArgumentException("Invalid kind " + kind);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   886
        return getFileForInput(location, RelativeFile.forClass(className, kind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
    public FileObject getFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
                                      String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
                                      String relativeName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
        nullCheck(packageName);
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   897
        if (!isRelativeUri(relativeName))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   899
        RelativeFile name = packageName.length() == 0
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   900
            ? new RelativeFile(relativeName)
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   901
            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
        return getFileForInput(location, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   905
    private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
        for (File dir: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
            if (dir.isDirectory()) {
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   912
                File f = name.getFile(dir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
                if (f.exists())
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   914
                    return new RegularFileObject(this, f);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                Archive a = openArchive(dir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
                if (a.contains(name)) {
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   918
                    return a.getFileObject(name.dirname(), name.basename());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
        }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   923
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
    public JavaFileObject getJavaFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
                                               String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
                                               JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                                               FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
        if (!sourceOrClass.contains(kind))
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
            throw new IllegalArgumentException("Invalid kind " + kind);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   939
        return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
    public FileObject getFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                                       String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                                       String relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                                       FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
        nullCheck(packageName);
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   951
        if (!isRelativeUri(relativeName))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
            throw new IllegalArgumentException("relativeName is invalid");
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   953
        RelativeFile name = packageName.length() == 0
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   954
            ? new RelativeFile(relativeName)
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   955
            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
        return getFileForOutput(location, name, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
    private JavaFileObject getFileForOutput(Location location,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   960
                                            RelativeFile fileName,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                                            FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
        File dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
        if (location == CLASS_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
            if (getClassOutDir() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
                dir = getClassOutDir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                File siblingDir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                if (sibling != null && sibling instanceof RegularFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                    siblingDir = ((RegularFileObject)sibling).f.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   973
                return new RegularFileObject(this, new File(siblingDir, fileName.basename()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
        } else if (location == SOURCE_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
            dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir());
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
            Iterable<? extends File> path = paths.getPathForLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
            dir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
            for (File f: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
                dir = f;
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   986
        File file = fileName.getFile(dir); // null-safe
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   987
        return new RegularFileObject(this, file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
        Iterable<? extends File> files)
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
        ArrayList<RegularFileObject> result;
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1208
diff changeset
   995
        if (files instanceof Collection<?>)
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1208
diff changeset
   996
            result = new ArrayList<RegularFileObject>(((Collection<?>)files).size());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
            result = new ArrayList<RegularFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
        for (File f: files)
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
  1000
            result.add(new RegularFileObject(this, nullCheck(f)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
    public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
        return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
    public void setLocation(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
                            Iterable<? extends File> path)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
        paths.lazy();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
        final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
        if (location == CLASS_OUTPUT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
            classOutDir = getOutputLocation(dir, D);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
        else if (location == SOURCE_OUTPUT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
            sourceOutDir = getOutputLocation(dir, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
            paths.setPathForLocation(location, path);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
        private File getOutputDirectory(Iterable<? extends File> path) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
            if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
            Iterator<? extends File> pathIter = path.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
            if (!pathIter.hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
                throw new IllegalArgumentException("empty path for directory");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
            File dir = pathIter.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
            if (pathIter.hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                throw new IllegalArgumentException("path too long for directory");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            if (!dir.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                throw new FileNotFoundException(dir + ": does not exist");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
            else if (!dir.isDirectory())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                throw new IOException(dir + ": not a directory");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
            return dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
    private File getOutputLocation(File dir, OptionName defaultOptionName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
        if (dir != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
            return dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
        String arg = options.get(defaultOptionName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
        if (arg == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
        return new File(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
    public Iterable<? extends File> getLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
        paths.lazy();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
        if (location == CLASS_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
            return (getClassOutDir() == null ? null : List.of(getClassOutDir()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
        } else if (location == SOURCE_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
            return (getSourceOutDir() == null ? null : List.of(getSourceOutDir()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
            return paths.getPathForLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
    private File getClassOutDir() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
        if (classOutDir == uninited)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
            classOutDir = getOutputLocation(null, D);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
        return classOutDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
    private File getSourceOutDir() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
        if (sourceOutDir == uninited)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
            sourceOutDir = getOutputLocation(null, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
        return sourceOutDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
     * Enforces the specification of a "relative" URI as used in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
     * {@linkplain #getFileForInput(Location,String,URI)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
     * getFileForInput}.  This method must follow the rules defined in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
     * that method, do not make any changes without consulting the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
     * specification.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
    protected static boolean isRelativeUri(URI uri) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
        if (uri.isAbsolute())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
        String path = uri.normalize().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
        if (path.length() == 0 /* isEmpty() is mustang API */)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
        char first = path.charAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
        return first != '.' && first != '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1090
    // Convenience method
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1091
    protected static boolean isRelativeUri(String u) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1092
        try {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1093
            return isRelativeUri(new URI(u));
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1094
        } catch (URISyntaxException e) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1095
            return false;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1096
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1097
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1098
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
     * Converts a relative file name to a relative URI.  This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
     * different from File.toURI as this method does not canonicalize
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
     * the file before creating the URI.  Furthermore, no schema is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
     * used.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
     * @param file a relative file name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
     * @return a relative URI
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
     * @throws IllegalArgumentException if the file name is not
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
     * relative according to the definition given in {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
     * javax.tools.JavaFileManager#getFileForInput}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
    public static String getRelativeName(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
        if (!file.isAbsolute()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
            String result = file.getPath().replace(File.separatorChar, '/');
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1113
            if (isRelativeUri(result))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
        throw new IllegalArgumentException("Invalid relative path: " + file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
    private static <T> T nullCheck(T o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
        o.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
        return o;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
    private static <T> Iterable<T> nullCheck(Iterable<T> it) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
        for (T t : it)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
            t.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
        return it;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
}