langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
author rwarburton
Wed, 29 Oct 2014 12:09:17 +0100
changeset 27318 4660a5da7d90
parent 27226 53535e4e1b08
child 27321 c8eceba990bf
permissions -rw-r--r--
8062376: Suppress cast warnings when using NIO buffers Reviewed-by: psandoz, jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. 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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
    23
 * questions.
10
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
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
    28
import java.io.ByteArrayOutputStream;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.FileNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
    32
import java.io.OutputStreamWriter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.net.URI;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
    35
import java.net.URISyntaxException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.nio.CharBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.Charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.util.Arrays;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import java.util.Collections;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8837
diff changeset
    43
import java.util.Comparator;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import java.util.zip.ZipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import javax.tools.FileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    55
import javax.tools.StandardJavaFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
    57
import com.sun.tools.javac.file.RelativePath.RelativeFile;
27225
8369cde9152a 8060056: replace java.io.File with java.nio.file.Path
jjg
parents: 26266
diff changeset
    58
import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
    59
import com.sun.tools.javac.util.BaseFileManager;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    60
import com.sun.tools.javac.util.Context;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    61
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    62
import com.sun.tools.javac.util.DefinedBy.Api;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    63
import com.sun.tools.javac.util.List;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    64
import com.sun.tools.javac.util.ListBuffer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
    66
import static javax.tools.StandardLocation.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 * This class provides access to the source, class and other files
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 * used by the compiler and related tools.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    71
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    72
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    73
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    74
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    75
 * deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 */
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
    77
public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
27318
4660a5da7d90 8062376: Suppress cast warnings when using NIO buffers
rwarburton
parents: 27226
diff changeset
    79
    @SuppressWarnings("cast")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public static char[] toArray(CharBuffer buffer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        if (buffer.hasArray())
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            return ((CharBuffer)buffer.compact().flip()).array();
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            return buffer.toString().toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
    87
    private FSInfo fsInfo;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
    88
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
    89
    private boolean contextUseOptimizedZip;
8223
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 8034
diff changeset
    90
    private ZipFileIndexCache zipFileIndexCache;
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 8034
diff changeset
    91
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    private final Set<JavaFileObject.Kind> sourceOrClass =
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    protected boolean mmappedIO;
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
    96
    protected boolean symbolFileEnabled;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
    98
    protected enum SortFiles implements Comparator<File> {
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
    99
        FORWARD {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   100
            public int compare(File f1, File f2) {
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   101
                return f1.getName().compareTo(f2.getName());
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   102
            }
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   103
        },
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   104
        REVERSE {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   105
            public int compare(File f1, File f2) {
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   106
                return -f1.getName().compareTo(f2.getName());
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   107
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   108
        }
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   109
    }
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   110
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   111
    protected SortFiles sortFiles;
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   112
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * Register a Context.Factory to create a JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8432
diff changeset
   116
    public static void preRegister(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8432
diff changeset
   118
            public JavaFileManager make(Context c) {
06e42328ddab 7021650: fix Context issues
jjg
parents: 8432
diff changeset
   119
                return new JavacFileManager(c, true, null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * Create a JavacFileManager using a given context, optionally registering
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * it as the JavaFileManager for that context.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    public JavacFileManager(Context context, boolean register, Charset charset) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   129
        super(charset);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        if (register)
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            context.put(JavaFileManager.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * Set the context for JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   138
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    public void setContext(Context context) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   140
        super.setContext(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   142
        fsInfo = FSInfo.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   144
        contextUseOptimizedZip = options.getBoolean("useOptimizedZip", true);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   145
        if (contextUseOptimizedZip)
8223
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 8034
diff changeset
   146
            zipFileIndexCache = ZipFileIndexCache.getSharedInstance();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6719
diff changeset
   148
        mmappedIO = options.isSet("mmappedIO");
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   149
        symbolFileEnabled = !options.isSet("ignore.symbol.file");
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   150
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   151
        String sf = options.get("sortFiles");
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   152
        if (sf != null) {
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   153
            sortFiles = (sf.equals("reverse") ? SortFiles.REVERSE : SortFiles.FORWARD);
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   154
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   157
    /**
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   158
     * Set whether or not to use ct.sym as an alternate to rt.jar.
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   159
     */
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   160
    public void setSymbolFileEnabled(boolean b) {
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   161
        symbolFileEnabled = b;
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   162
    }
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   163
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    public JavaFileObject getFileForInput(String name) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   165
        return getRegularFile(new File(name));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   168
    public JavaFileObject getRegularFile(File file) {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   169
        return new RegularFileObject(this, file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    public JavaFileObject getFileForOutput(String classname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                                           JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                                           JavaFileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   180
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   182
        ListBuffer<File> files = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        for (String name : names)
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            files.append(new File(nullCheck(name)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        return getJavaFileObjectsFromFiles(files.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   188
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    private static boolean isValidName(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        // but the set of keywords depends on the source level, and we don't want
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        // impls of JavaFileManager to have to be dependent on the source level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        // Therefore we simply check that the argument is a sequence of identifiers
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        // separated by ".".
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        for (String s : name.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            if (!SourceVersion.isIdentifier(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    private static void validateClassName(String className) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        if (!isValidName(className))
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            throw new IllegalArgumentException("Invalid class name: " + className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    private static void validatePackageName(String packageName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        if (packageName.length() > 0 && !isValidName(packageName))
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            throw new IllegalArgumentException("Invalid packageName name: " + packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    public static void testName(String name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                                boolean isValidPackageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                                boolean isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            validatePackageName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            if (!isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                throw new AssertionError("Invalid package name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            printAscii("Valid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            if (isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                throw new AssertionError("Valid package name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            printAscii("Invalid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            validateClassName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            if (!isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                throw new AssertionError("Invalid class name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            printAscii("Valid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            if (isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                throw new AssertionError("Valid class name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            printAscii("Invalid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   241
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    private static void printAscii(String format, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        String message;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            final String ascii = "US-ASCII";
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            message = new String(String.format(null, format, args).getBytes(ascii), ascii);
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        } catch (java.io.UnsupportedEncodingException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        System.out.println(message);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   253
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    /**
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   255
     * Insert all files in subdirectory subdirectory of directory directory
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   256
     * which match fileKinds into resultList
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
     */
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   258
    private void listDirectory(File directory,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   259
                               RelativeDirectory subdirectory,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                               Set<JavaFileObject.Kind> fileKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                               boolean recurse,
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   262
                               ListBuffer<JavaFileObject> resultList) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   263
        File d = subdirectory.getFile(directory);
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   264
        if (!caseMapCheck(d, subdirectory))
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   265
            return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   267
        File[] files = d.listFiles();
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   268
        if (files == null)
27225
8369cde9152a 8060056: replace java.io.File with java.nio.file.Path
jjg
parents: 26266
diff changeset
   269
            return;
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   270
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   271
        if (sortFiles != null)
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   272
            Arrays.sort(files, sortFiles);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   274
        for (File f: files) {
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   275
            String fname = f.getName();
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   276
            if (f.isDirectory()) {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   277
                if (recurse && SourceVersion.isIdentifier(fname)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   278
                    listDirectory(directory,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   279
                                  new RelativeDirectory(subdirectory, fname),
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   280
                                  fileKinds,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   281
                                  recurse,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   282
                                  resultList);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   284
            } else {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   285
                if (isValidFile(fname, fileKinds)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   286
                    JavaFileObject fe =
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   287
                        new RegularFileObject(this, fname, new File(d, fname));
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   288
                    resultList.append(fe);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   294
    /**
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   295
     * Insert all files in subdirectory subdirectory of archive archive
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   296
     * which match fileKinds into resultList
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   297
     */
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   298
    private void listArchive(Archive archive,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   299
                               RelativeDirectory subdirectory,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   300
                               Set<JavaFileObject.Kind> fileKinds,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   301
                               boolean recurse,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   302
                               ListBuffer<JavaFileObject> resultList) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   303
        // Get the files directly in the subdir
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   304
        List<String> files = archive.getFiles(subdirectory);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   305
        if (files != null) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   306
            for (; !files.isEmpty(); files = files.tail) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   307
                String file = files.head;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   308
                if (isValidFile(file, fileKinds)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   309
                    resultList.append(archive.getFileObject(subdirectory, file));
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   310
                }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   311
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   312
        }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   313
        if (recurse) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   314
            for (RelativeDirectory s: archive.getSubdirectories()) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   315
                if (subdirectory.contains(s)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   316
                    // Because the archive map is a flat list of directories,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   317
                    // the enclosing loop will pick up all child subdirectories.
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   318
                    // Therefore, there is no need to recurse deeper.
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   319
                    listArchive(archive, s, fileKinds, false, resultList);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   320
                }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   321
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   322
        }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   323
    }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   324
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   325
    /**
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   326
     * container is a directory, a zip file, or a non-existant path.
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   327
     * Insert all files in subdirectory subdirectory of container which
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   328
     * match fileKinds into resultList
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   329
     */
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   330
    private void listContainer(File container,
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   331
                               RelativeDirectory subdirectory,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   332
                               Set<JavaFileObject.Kind> fileKinds,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   333
                               boolean recurse,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   334
                               ListBuffer<JavaFileObject> resultList) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   335
        Archive archive = archives.get(container);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   336
        if (archive == null) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   337
            // archives are not created for directories.
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   338
            if  (fsInfo.isDirectory(container)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   339
                listDirectory(container,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   340
                              subdirectory,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   341
                              fileKinds,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   342
                              recurse,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   343
                              resultList);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   344
                return;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   345
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   346
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   347
            // Not a directory; either a file or non-existant, create the archive
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   348
            try {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   349
                archive = openArchive(container);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   350
            } catch (IOException ex) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   351
                log.error("error.reading.file",
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   352
                          container, getMessage(ex));
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   353
                return;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   354
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   355
        }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   356
        listArchive(archive,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   357
                    subdirectory,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   358
                    fileKinds,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   359
                    recurse,
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   360
                    resultList);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   361
    }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   362
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
    private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   364
        JavaFileObject.Kind kind = getKind(s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        return fileKinds.contains(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
    private static final boolean fileSystemIsCaseSensitive =
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        File.separatorChar == '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    /** Hack to make Windows case sensitive. Test whether given path
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
     *  ends in a string of characters with the same case as given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     *  Ignore file separators in both path and name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     */
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   375
    private boolean caseMapCheck(File f, RelativePath name) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
        if (fileSystemIsCaseSensitive) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        // Note that getCanonicalPath() returns the case-sensitive
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        // spelled file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        String path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        try {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   381
            path = f.getCanonicalPath();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        char[] pcs = path.toCharArray();
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   386
        char[] ncs = name.path.toCharArray();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        int i = pcs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        int j = ncs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        while (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            while (i >= 0 && pcs[i] == File.separatorChar) i--;
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   391
            while (j >= 0 && ncs[j] == '/') j--;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            if (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                if (pcs[i] != ncs[j]) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                i--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        return j < 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     * An archive provides a flat directory structure of a ZipFile by
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
     * mapping directory names to lists of files (basenames).
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
    public interface Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        void close() throws IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   408
        boolean contains(RelativePath name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   410
        JavaFileObject getFileObject(RelativeDirectory subdirectory, String file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   412
        List<String> getFiles(RelativeDirectory subdirectory);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   414
        Set<RelativeDirectory> getSubdirectories();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    public class MissingArchive implements Archive {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   418
        final File zipFileName;
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   419
        public MissingArchive(File name) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            zipFileName = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   422
        public boolean contains(RelativePath name) {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   423
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   429
        public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   433
        public List<String> getFiles(RelativeDirectory subdirectory) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   437
        public Set<RelativeDirectory> getSubdirectories() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   440
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   441
        @Override
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   442
        public String toString() {
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   443
            return "MissingArchive[" + zipFileName + "]";
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   444
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    /** A directory of zip files already opened.
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
     */
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   449
    Map<File, Archive> archives = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   451
    private static final String[] symbolFileLocation = { "lib", "ct.sym" };
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   452
    private static final RelativeDirectory symbolFilePrefix
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   453
            = new RelativeDirectory("META-INF/sym/rt.jar/");
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   454
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   455
    /*
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   456
     * This method looks for a ZipFormatException and takes appropriate
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   457
     * evasive action. If there is a failure in the fast mode then we
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   458
     * fail over to the platform zip, and allow it to deal with a potentially
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   459
     * non compliant zip file.
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   460
     */
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   461
    protected Archive openArchive(File zipFilename) throws IOException {
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   462
        try {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   463
            return openArchive(zipFilename, contextUseOptimizedZip);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   464
        } catch (IOException ioe) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   465
            if (ioe instanceof ZipFileIndex.ZipFormatException) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   466
                return openArchive(zipFilename, false);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   467
            } else {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   468
                throw ioe;
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   469
            }
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   470
        }
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   471
    }
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   472
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   473
    /** Open a new zip file directory, and cache it.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
     */
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   475
    private Archive openArchive(File zipFileName, boolean useOptimizedZip) throws IOException {
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   476
        File origZipFileName = zipFileName;
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   477
        if (symbolFileEnabled && locations.isDefaultBootClassPathRtJar(zipFileName)) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   478
            File file = zipFileName.getParentFile().getParentFile(); // ${java.home}
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   479
            if (new File(file.getName()).equals(new File("jre")))
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   480
                file = file.getParentFile();
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   481
            // file == ${jdk.home}
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   482
            for (String name : symbolFileLocation)
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   483
                file = new File(file, name);
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   484
            // file == ${jdk.home}/lib/ct.sym
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   485
            if (file.exists())
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   486
                zipFileName = file;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   487
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   489
        Archive archive;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   490
        try {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   492
            ZipFile zdir = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   494
            boolean usePreindexedCache = false;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   495
            String preindexCacheLocation = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   497
            if (!useOptimizedZip) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   498
                zdir = new ZipFile(zipFileName);
8432
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 8223
diff changeset
   499
            } else {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   500
                usePreindexedCache = options.isSet("usezipindex");
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   501
                preindexCacheLocation = options.get("java.io.tmpdir");
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   502
                String optCacheLoc = options.get("cachezipindexdir");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   504
                if (optCacheLoc != null && optCacheLoc.length() != 0) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   505
                    if (optCacheLoc.startsWith("\"")) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   506
                        if (optCacheLoc.endsWith("\"")) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   507
                            optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   508
                        }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   509
                        else {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   510
                            optCacheLoc = optCacheLoc.substring(1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                        }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   512
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   514
                    File cacheDir = new File(optCacheLoc);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   515
                    if (cacheDir.exists() && cacheDir.canWrite()) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   516
                        preindexCacheLocation = optCacheLoc;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   517
                        if (!preindexCacheLocation.endsWith("/") &&
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   518
                            !preindexCacheLocation.endsWith(File.separator)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   519
                            preindexCacheLocation += File.separator;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   523
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   525
            if (origZipFileName == zipFileName) {
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   526
                if (!useOptimizedZip) {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   527
                    archive = new ZipArchive(this, zdir);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   528
                } else {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   529
                    archive = new ZipFileIndexArchive(this,
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   530
                                    zipFileIndexCache.getZipFileIndex(zipFileName,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   531
                                    null,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   532
                                    usePreindexedCache,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   533
                                    preindexCacheLocation,
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6719
diff changeset
   534
                                    options.isSet("writezipindexfiles")));
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   535
                }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   536
            } else {
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   537
                if (!useOptimizedZip) {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   538
                    archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix);
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   539
                } else {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   540
                    archive = new ZipFileIndexArchive(this,
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   541
                                    zipFileIndexCache.getZipFileIndex(zipFileName,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   542
                                    symbolFilePrefix,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   543
                                    usePreindexedCache,
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   544
                                    preindexCacheLocation,
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6719
diff changeset
   545
                                    options.isSet("writezipindexfiles")));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
            }
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   548
        } catch (FileNotFoundException ex) {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   549
            archive = new MissingArchive(zipFileName);
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   550
        } catch (ZipFileIndex.ZipFormatException zfe) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 8614
diff changeset
   551
            throw zfe;
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   552
        } catch (IOException ex) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   553
            if (zipFileName.exists())
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   554
                log.error("error.reading.file", zipFileName, getMessage(ex));
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   555
            archive = new MissingArchive(zipFileName);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   556
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   558
        archives.put(origZipFileName, archive);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        return archive;
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    /** Flush any output resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   564
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
        contentCache.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
     * Close the JavaFileManager, releasing resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   572
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
            Archive a = i.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            i.remove();
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                a.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   584
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
    public ClassLoader getClassLoader(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            return null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   590
        ListBuffer<URL> lb = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
        for (File f: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
                lb.append(f.toURI().toURL());
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        }
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   598
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   599
        return getClassLoader(lb.toArray(new URL[lb.size()]));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   602
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
    public Iterable<JavaFileObject> list(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                                         String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                                         Set<JavaFileObject.Kind> kinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                                         boolean recurse)
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        nullCheck(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
        nullCheck(kinds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   613
        Iterable<? extends File> path = getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            return List.nil();
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   616
        RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   617
        ListBuffer<JavaFileObject> results = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   619
        for (File directory : path)
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   620
            listContainer(directory, subdirectory, kinds, recurse, results);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        return results.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   624
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
    public String inferBinaryName(Location location, JavaFileObject file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        file.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        location.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        // Need to match the path semantics of list(location, ...)
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   629
        Iterable<? extends File> path = getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        if (path == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   634
        if (file instanceof BaseFileObject) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   635
            return ((BaseFileObject) file).inferBinaryName(path);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
            throw new IllegalArgumentException(file.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   640
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
    public boolean isSameFile(FileObject a, FileObject b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
        nullCheck(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        nullCheck(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        if (!(a instanceof BaseFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            throw new IllegalArgumentException("Not supported: " + a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
        if (!(b instanceof BaseFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
            throw new IllegalArgumentException("Not supported: " + b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        return a.equals(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   651
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
    public boolean hasLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        return getLocation(location) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   656
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    public JavaFileObject getJavaFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
                                              String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
                                              JavaFileObject.Kind kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
        if (!sourceOrClass.contains(kind))
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   667
            throw new IllegalArgumentException("Invalid kind: " + kind);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   668
        return getFileForInput(location, RelativeFile.forClass(className, kind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   671
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
    public FileObject getFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
                                      String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                                      String relativeName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        nullCheck(packageName);
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   680
        if (!isRelativeUri(relativeName))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   682
        RelativeFile name = packageName.length() == 0
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   683
            ? new RelativeFile(relativeName)
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   684
            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        return getFileForInput(location, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   688
    private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   689
        Iterable<? extends File> path = getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   693
        for (File dir: path) {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   694
            Archive a = archives.get(dir);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   695
            if (a == null) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   696
                if (fsInfo.isDirectory(dir)) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   697
                    File f = name.getFile(dir);
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   698
                    if (f.exists())
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   699
                        return new RegularFileObject(this, f);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   700
                    continue;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
                }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   702
                // Not a directory, create the archive
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   703
                a = openArchive(dir);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   704
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   705
            // Process the archive
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   706
            if (a.contains(name)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   707
                return a.getFileObject(name.dirname(), name.basename());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   713
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    public JavaFileObject getJavaFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                                               String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                                               JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                                               FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        if (!sourceOrClass.contains(kind))
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   725
            throw new IllegalArgumentException("Invalid kind: " + kind);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   726
        return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   729
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
    public FileObject getFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
                                       String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
                                       String relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
                                       FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        nullCheck(packageName);
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   739
        if (!isRelativeUri(relativeName))
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   740
            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   741
        RelativeFile name = packageName.length() == 0
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   742
            ? new RelativeFile(relativeName)
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   743
            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
        return getFileForOutput(location, name, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
    private JavaFileObject getFileForOutput(Location location,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   748
                                            RelativeFile fileName,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
                                            FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
    {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   752
        File dir;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
        if (location == CLASS_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            if (getClassOutDir() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
                dir = getClassOutDir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            } else {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   757
                File siblingDir = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
                if (sibling != null && sibling instanceof RegularFileObject) {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   759
                    siblingDir = ((RegularFileObject)sibling).file.getParentFile();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
                }
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   761
                return new RegularFileObject(this, new File(siblingDir, fileName.basename()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
        } else if (location == SOURCE_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
            dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir());
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        } else {
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   766
            Iterable<? extends File> path = locations.getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
            dir = null;
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   768
            for (File f: path) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                dir = f;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   774
        File file = fileName.getFile(dir); // null-safe
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 809
diff changeset
   775
        return new RegularFileObject(this, file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   779
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
        Iterable<? extends File> files)
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
        ArrayList<RegularFileObject> result;
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1208
diff changeset
   784
        if (files instanceof Collection<?>)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   785
            result = new ArrayList<>(((Collection<?>)files).size());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
        else
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   787
            result = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
        for (File f: files)
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   789
            result.add(new RegularFileObject(this, nullCheck(f)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   793
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
    public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   798
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
    public void setLocation(Location location,
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   800
                            Iterable<? extends File> path)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        nullCheck(location);
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   804
        locations.setLocation(location, path);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   807
    @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
    public Iterable<? extends File> getLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
        nullCheck(location);
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   810
        return locations.getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   813
    private File getClassOutDir() {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   814
        return locations.getOutputLocation(CLASS_OUTPUT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
   817
    private File getSourceOutDir() {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   818
        return locations.getOutputLocation(SOURCE_OUTPUT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
    /**
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10818
diff changeset
   822
     * Enforces the specification of a "relative" name as used in
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10818
diff changeset
   823
     * {@linkplain #getFileForInput(Location,String,String)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     * getFileForInput}.  This method must follow the rules defined in
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
     * that method, do not make any changes without consulting the
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
     * specification.
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
    protected static boolean isRelativeUri(URI uri) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        if (uri.isAbsolute())
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        String path = uri.normalize().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        if (path.length() == 0 /* isEmpty() is mustang API */)
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            return false;
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   834
        if (!path.equals(uri.getPath())) // implicitly checks for embedded . and ..
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   835
            return false;
7840
dfc4669cbc84 6999891: DefaultFileManager incorrect
jjg
parents: 7839
diff changeset
   836
        if (path.startsWith("/") || path.startsWith("./") || path.startsWith("../"))
dfc4669cbc84 6999891: DefaultFileManager incorrect
jjg
parents: 7839
diff changeset
   837
            return false;
dfc4669cbc84 6999891: DefaultFileManager incorrect
jjg
parents: 7839
diff changeset
   838
        return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   841
    // Convenience method
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   842
    protected static boolean isRelativeUri(String u) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   843
        try {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   844
            return isRelativeUri(new URI(u));
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   845
        } catch (URISyntaxException e) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   846
            return false;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   847
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   848
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   849
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
     * Converts a relative file name to a relative URI.  This is
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
     * different from File.toURI as this method does not canonicalize
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
     * the file before creating the URI.  Furthermore, no schema is
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
     * used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
     * @param file a relative file name
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
     * @return a relative URI
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
     * @throws IllegalArgumentException if the file name is not
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
     * relative according to the definition given in {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
     * javax.tools.JavaFileManager#getFileForInput}
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
    public static String getRelativeName(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
        if (!file.isAbsolute()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            String result = file.getPath().replace(File.separatorChar, '/');
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   864
            if (isRelativeUri(result))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
        throw new IllegalArgumentException("Invalid relative path: " + file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
    }
5007
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   869
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   870
    /**
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   871
     * Get a detail message from an IOException.
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   872
     * Most, but not all, instances of IOException provide a non-null result
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   873
     * for getLocalizedMessage().  But some instances return null: in these
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   874
     * cases, fallover to getMessage(), and if even that is null, return the
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   875
     * name of the exception itself.
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   876
     * @param e an IOException
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   877
     * @return a string to include in a compiler diagnostic
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   878
     */
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   879
    public static String getMessage(IOException e) {
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   880
        String s = e.getLocalizedMessage();
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   881
        if (s != null)
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   882
            return s;
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   883
        s = e.getMessage();
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   884
        if (s != null)
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   885
            return s;
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   886
        return e.toString();
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
   887
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
}