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