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