langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 42815 050370edaade
child 43565 266c9503e22f
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
     2
 * Copyright (c) 2005, 2016, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.net.URI;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
    32
import java.net.URISyntaxException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.nio.CharBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.Charset;
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    36
import java.nio.file.FileSystem;
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    37
import java.nio.file.FileSystems;
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    38
import java.nio.file.FileVisitOption;
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    39
import java.nio.file.FileVisitResult;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    40
import java.nio.file.Files;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    41
import java.nio.file.InvalidPathException;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    42
import java.nio.file.LinkOption;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    43
import java.nio.file.Path;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    44
import java.nio.file.Paths;
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
    45
import java.nio.file.ProviderNotFoundException;
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    46
import java.nio.file.SimpleFileVisitor;
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    47
import java.nio.file.attribute.BasicFileAttributes;
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
    48
import java.nio.file.spi.FileSystemProvider;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import java.util.Arrays;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import java.util.Collection;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
    52
import java.util.Collections;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8837
diff changeset
    53
import java.util.Comparator;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import java.util.Map;
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 28332
diff changeset
    58
import java.util.Objects;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
    59
import java.util.ServiceLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import java.util.Set;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    61
import java.util.stream.Collectors;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    62
import java.util.stream.Stream;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
import javax.tools.FileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    68
import javax.tools.StandardJavaFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
    70
import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
27226
53535e4e1b08 8061723: 8060056 breaks tests on Windows
jjg
parents: 27225
diff changeset
    71
import com.sun.tools.javac.file.RelativePath.RelativeFile;
40513
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 38533
diff changeset
    72
import com.sun.tools.javac.util.Assert;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    73
import com.sun.tools.javac.util.Context;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42815
diff changeset
    74
import com.sun.tools.javac.util.Context.Factory;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    75
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    76
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
    77
import com.sun.tools.javac.util.List;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    78
import com.sun.tools.javac.util.ListBuffer;
38533
2bd347dde526 8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents: 37944
diff changeset
    79
import com.sun.tools.javac.util.JDK9Wrappers.Configuration;
2bd347dde526 8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents: 37944
diff changeset
    80
import com.sun.tools.javac.util.JDK9Wrappers.Layer;
2bd347dde526 8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents: 37944
diff changeset
    81
import com.sun.tools.javac.util.JDK9Wrappers.ModuleFinder;
41033
49af2ecba616 8164742: ServiceConfigurationError on invoke of getServiceLoader method of StandardJavaFileManager
sadayapalam
parents: 40513
diff changeset
    82
import com.sun.tools.javac.util.JDK9Wrappers.Module;
38533
2bd347dde526 8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents: 37944
diff changeset
    83
import com.sun.tools.javac.util.JDK9Wrappers.ServiceLoaderHelper;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    85
import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
    86
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
    87
import static javax.tools.StandardLocation.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 * This class provides access to the source, class and other files
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 * used by the compiler and related tools.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1789
diff changeset
    92
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    93
 * <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
    94
 * 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
    95
 * 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
    96
 * deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
 */
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
    98
public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
27318
4660a5da7d90 8062376: Suppress cast warnings when using NIO buffers
rwarburton
parents: 27226
diff changeset
   100
    @SuppressWarnings("cast")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public static char[] toArray(CharBuffer buffer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        if (buffer.hasArray())
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            return ((CharBuffer)buffer.compact().flip()).array();
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            return buffer.toString().toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   108
    private FSInfo fsInfo;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   109
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    private final Set<JavaFileObject.Kind> sourceOrClass =
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   113
    protected boolean symbolFileEnabled;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   115
    private PathFactory pathFactory = Paths::get;
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   116
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   117
    protected enum SortFiles implements Comparator<Path> {
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   118
        FORWARD {
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   119
            @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   120
            public int compare(Path f1, Path f2) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   121
                return f1.getFileName().compareTo(f2.getFileName());
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   122
            }
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   123
        },
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   124
        REVERSE {
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   125
            @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   126
            public int compare(Path f1, Path f2) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   127
                return -f1.getFileName().compareTo(f2.getFileName());
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   128
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   129
        }
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   130
    }
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   131
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   132
    protected SortFiles sortFiles;
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   133
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * Register a Context.Factory to create a JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     */
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8432
diff changeset
   137
    public static void preRegister(Context context) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42815
diff changeset
   138
        context.put(JavaFileManager.class,
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42815
diff changeset
   139
                (Factory<JavaFileManager>)c -> new JavacFileManager(c, true, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     * Create a JavacFileManager using a given context, optionally registering
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * it as the JavaFileManager for that context.
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    public JavacFileManager(Context context, boolean register, Charset charset) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   147
        super(charset);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        if (register)
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            context.put(JavaFileManager.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     * Set the context for JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     */
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   156
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    public void setContext(Context context) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   158
        super.setContext(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 1205
diff changeset
   160
        fsInfo = FSInfo.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   162
        symbolFileEnabled = !options.isSet("ignore.symbol.file");
7334
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   163
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   164
        String sf = options.get("sortFiles");
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   165
        if (sf != null) {
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   166
            sortFiles = (sf.equals("reverse") ? SortFiles.REVERSE : SortFiles.FORWARD);
f432af22de29 7003006: add option to list directory in deterministic order
jjg
parents: 6721
diff changeset
   167
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   170
    @Override @DefinedBy(DefinedBy.Api.COMPILER)
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   171
    public void setPathFactory(PathFactory f) {
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   172
        pathFactory = Objects.requireNonNull(f);
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   173
        locations.setPathFactory(f);
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   174
    }
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   175
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   176
    private Path getPath(String first, String... more) {
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   177
        return pathFactory.getPath(first, more);
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   178
    }
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   179
14362
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   180
    /**
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   181
     * 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
   182
     */
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   183
    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
   184
        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
   185
    }
598fd301e4de 8001229: refactor javac so that ct.sym is just used for javac, not all clients of JavacFileManager
jjg
parents: 14259
diff changeset
   186
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   187
    public boolean isSymbolFileEnabled() {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   188
        return symbolFileEnabled;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   189
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   190
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   191
    // used by tests
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   192
    public JavaFileObject getJavaFileObject(String name) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   193
        return getJavaFileObjects(name).iterator().next();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   196
    // used by tests
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   197
    public JavaFileObject getJavaFileObject(Path file) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   198
        return getJavaFileObjects(file).iterator().next();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    public JavaFileObject getFileForOutput(String classname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                                           JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                                           JavaFileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   209
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   211
        ListBuffer<Path> paths = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        for (String name : names)
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   213
            paths.append(getPath(nullCheck(name)));
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   214
        return getJavaFileObjectsFromPaths(paths.toList());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   217
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    private static boolean isValidName(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        // but the set of keywords depends on the source level, and we don't want
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        // impls of JavaFileManager to have to be dependent on the source level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        // Therefore we simply check that the argument is a sequence of identifiers
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        // separated by ".".
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        for (String s : name.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            if (!SourceVersion.isIdentifier(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    private static void validateClassName(String className) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        if (!isValidName(className))
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            throw new IllegalArgumentException("Invalid class name: " + className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    private static void validatePackageName(String packageName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        if (packageName.length() > 0 && !isValidName(packageName))
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            throw new IllegalArgumentException("Invalid packageName name: " + packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    public static void testName(String name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                                boolean isValidPackageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                                boolean isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            validatePackageName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            if (!isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                throw new AssertionError("Invalid package name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            printAscii("Valid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            if (isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                throw new AssertionError("Valid package name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            printAscii("Invalid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            validateClassName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            if (!isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                throw new AssertionError("Invalid class name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            printAscii("Valid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            if (isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                throw new AssertionError("Valid class name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            printAscii("Invalid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    }
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   270
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    private static void printAscii(String format, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        String message;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            final String ascii = "US-ASCII";
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            message = new String(String.format(null, format, args).getBytes(ascii), ascii);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        } catch (java.io.UnsupportedEncodingException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        System.out.println(message);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   282
    private final Map<Path, Container> containers = new HashMap<>();
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   283
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   284
    synchronized Container getContainer(Path path) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   285
        Container fs = containers.get(path);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   286
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   287
        if (fs != null) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   288
            return fs;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   289
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   290
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   291
        if (fsInfo.isFile(path) && path.equals(Locations.thisSystemModules)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   292
            containers.put(path, fs = new JRTImageContainer());
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   293
            return fs;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   294
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   295
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   296
        Path realPath = fsInfo.getCanonicalFile(path);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   297
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   298
        fs = containers.get(realPath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   299
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   300
        if (fs != null) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   301
            containers.put(path, fs);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   302
            return fs;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   303
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   304
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   305
        BasicFileAttributes attr = null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   306
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   307
        try {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   308
            attr = Files.readAttributes(realPath, BasicFileAttributes.class);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   309
        } catch (IOException ex) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   310
            //non-existing
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   311
            fs = MISSING_CONTAINER;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   312
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   313
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   314
        if (attr != null) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   315
            if (attr.isDirectory()) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   316
                fs = new DirectoryContainer(realPath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   317
            } else {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   318
                try {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   319
                    fs = new ArchiveContainer(realPath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   320
                } catch (ProviderNotFoundException | SecurityException ex) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   321
                    throw new IOException(ex);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   322
                }
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   323
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   324
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   325
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   326
        containers.put(realPath, fs);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   327
        containers.put(path, fs);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   328
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   329
        return fs;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   330
    }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   331
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   332
    private interface Container {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   333
        /**
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   334
         * Insert all files in subdirectory subdirectory of container which
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   335
         * match fileKinds into resultList
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   336
         */
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   337
        public abstract void list(Path userPath,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   338
                                  RelativeDirectory subdirectory,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   339
                                  Set<JavaFileObject.Kind> fileKinds,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   340
                                  boolean recurse,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   341
                                  ListBuffer<JavaFileObject> resultList) throws IOException;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   342
        public abstract JavaFileObject getFileObject(Path userPath, RelativeFile name) throws IOException;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   343
        public abstract void close() throws IOException;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   344
    }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   345
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   346
    private static final Container MISSING_CONTAINER =  new Container() {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   347
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   348
        public void list(Path userPath,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   349
                         RelativeDirectory subdirectory,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   350
                         Set<JavaFileObject.Kind> fileKinds,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   351
                         boolean recurse,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   352
                         ListBuffer<JavaFileObject> resultList) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   353
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   354
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   355
        public JavaFileObject getFileObject(Path userPath, RelativeFile name) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   356
            return null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   357
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   358
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   359
        public void close() throws IOException {}
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   360
    };
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   361
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   362
    private final class JRTImageContainer implements Container {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   363
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   364
        /**
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   365
         * Insert all files in a subdirectory of the platform image
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   366
         * which match fileKinds into resultList.
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   367
         */
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   368
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   369
        public void list(Path userPath,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   370
                         RelativeDirectory subdirectory,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   371
                         Set<JavaFileObject.Kind> fileKinds,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   372
                         boolean recurse,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   373
                         ListBuffer<JavaFileObject> resultList) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   374
            try {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   375
                JRTIndex.Entry e = getJRTIndex().getEntry(subdirectory);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   376
                if (symbolFileEnabled && e.ctSym.hidden)
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   377
                    return;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   378
                for (Path file: e.files.values()) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   379
                    if (fileKinds.contains(getKind(file))) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   380
                        JavaFileObject fe
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   381
                                = PathFileObject.forJRTPath(JavacFileManager.this, file);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   382
                        resultList.append(fe);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   383
                    }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   384
                }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   385
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   386
                if (recurse) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   387
                    for (RelativeDirectory rd: e.subdirs) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   388
                        list(userPath, rd, fileKinds, recurse, resultList);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   389
                    }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   390
                }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   391
            } catch (IOException ex) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   392
                ex.printStackTrace(System.err);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   393
                log.error("error.reading.file", userPath, getMessage(ex));
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   394
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   395
        }
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   396
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   397
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   398
        public JavaFileObject getFileObject(Path userPath, RelativeFile name) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   399
            JRTIndex.Entry e = getJRTIndex().getEntry(name.dirname());
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   400
            if (symbolFileEnabled && e.ctSym.hidden)
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   401
                return null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   402
            Path p = e.files.get(name.basename());
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   403
            if (p != null) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   404
                return PathFileObject.forJRTPath(JavacFileManager.this, p);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   405
            } else {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   406
                return null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   407
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   408
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   409
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   410
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   411
        public void close() throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   412
        }
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   413
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   414
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   415
    private synchronized JRTIndex getJRTIndex() {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   416
        if (jrtIndex == null)
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   417
            jrtIndex = JRTIndex.getSharedInstance();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   418
        return jrtIndex;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   419
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   420
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   421
    private JRTIndex jrtIndex;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   422
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   423
    private final class DirectoryContainer implements Container {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   424
        private final Path directory;
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   425
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   426
        public DirectoryContainer(Path directory) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   427
            this.directory = directory;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   428
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   430
        /**
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   431
         * Insert all files in subdirectory subdirectory of directory userPath
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   432
         * which match fileKinds into resultList
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   433
         */
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   434
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   435
        public void list(Path userPath,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   436
                         RelativeDirectory subdirectory,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   437
                         Set<JavaFileObject.Kind> fileKinds,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   438
                         boolean recurse,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   439
                         ListBuffer<JavaFileObject> resultList) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   440
            Path d;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   441
            try {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   442
                d = subdirectory.resolveAgainst(userPath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   443
            } catch (InvalidPathException ignore) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   444
                return ;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   445
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   446
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   447
            if (!Files.exists(d)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   448
               return;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   449
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   450
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   451
            if (!caseMapCheck(d, subdirectory)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   452
                return;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   453
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   454
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   455
            java.util.List<Path> files;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   456
            try (Stream<Path> s = Files.list(d)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   457
                files = (sortFiles == null ? s : s.sorted(sortFiles)).collect(Collectors.toList());
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   458
            } catch (IOException ignore) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   459
                return;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   460
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   461
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   462
            for (Path f: files) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   463
                String fname = f.getFileName().toString();
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   464
                if (fname.endsWith("/"))
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   465
                    fname = fname.substring(0, fname.length() - 1);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   466
                if (Files.isDirectory(f)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   467
                    if (recurse && SourceVersion.isIdentifier(fname)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   468
                        list(userPath,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   469
                             new RelativeDirectory(subdirectory, fname),
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   470
                             fileKinds,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   471
                             recurse,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   472
                             resultList);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   473
                    }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   474
                } else {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   475
                    if (isValidFile(fname, fileKinds)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   476
                        RelativeFile file = new RelativeFile(subdirectory, fname);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   477
                        JavaFileObject fe = PathFileObject.forDirectoryPath(JavacFileManager.this,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   478
                                file.resolveAgainst(directory), userPath, file);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   479
                        resultList.append(fe);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   480
                    }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   481
                }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   482
            }
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   483
        }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   484
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   485
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   486
        public JavaFileObject getFileObject(Path userPath, RelativeFile name) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   487
            try {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   488
                Path f = name.resolveAgainst(userPath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   489
                if (Files.exists(f))
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   490
                    return PathFileObject.forSimplePath(JavacFileManager.this,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   491
                            fsInfo.getCanonicalFile(f), f);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   492
            } catch (InvalidPathException ignore) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   493
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   494
            return null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   495
        }
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   496
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   497
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   498
        public void close() throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   502
    private final class ArchiveContainer implements Container {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   503
        private final Path archivePath;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   504
        private final FileSystem fileSystem;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   505
        private final Map<RelativePath, Path> pathCache = new HashMap<>();
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   506
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   507
        public ArchiveContainer(Path archivePath) throws IOException, ProviderNotFoundException, SecurityException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   508
            this.archivePath = archivePath;
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   509
            if (multiReleaseValue != null && archivePath.toString().endsWith(".jar")) {
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   510
                Map<String,String> env = Collections.singletonMap("multi-release", multiReleaseValue);
40513
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 38533
diff changeset
   511
                FileSystemProvider jarFSProvider = fsInfo.getJarFSProvider();
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 38533
diff changeset
   512
                Assert.checkNonNull(jarFSProvider, "should have been caught before!");
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 38533
diff changeset
   513
                this.fileSystem = jarFSProvider.newFileSystem(archivePath, env);
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   514
            } else {
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   515
                this.fileSystem = FileSystems.newFileSystem(archivePath, null);
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   516
            }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   517
        }
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   518
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   519
        /**
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   520
         * Insert all files in subdirectory subdirectory of this archive
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   521
         * which match fileKinds into resultList
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   522
         */
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   523
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   524
        public void list(Path userPath,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   525
                         RelativeDirectory subdirectory,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   526
                         Set<JavaFileObject.Kind> fileKinds,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   527
                         boolean recurse,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   528
                         ListBuffer<JavaFileObject> resultList) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   529
            Path resolvedSubdirectory = resolvePath(subdirectory);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   530
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   531
            if (resolvedSubdirectory == null)
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   532
                return ;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   533
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   534
            int maxDepth = (recurse ? Integer.MAX_VALUE : 1);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   535
            Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   536
            Files.walkFileTree(resolvedSubdirectory, opts, maxDepth,
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   537
                    new SimpleFileVisitor<Path>() {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   538
                        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   539
                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   540
                            if (isValid(dir.getFileName())) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   541
                                return FileVisitResult.CONTINUE;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   542
                            } else {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   543
                                return FileVisitResult.SKIP_SUBTREE;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   544
                            }
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   545
                        }
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   546
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   547
                        boolean isValid(Path fileName) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   548
                            if (fileName == null) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   549
                                return true;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   550
                            } else {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   551
                                String name = fileName.toString();
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   552
                                if (name.endsWith("/")) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   553
                                    name = name.substring(0, name.length() - 1);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   554
                                }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   555
                                return SourceVersion.isIdentifier(name);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   556
                            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   557
                        }
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   558
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   559
                        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   560
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   561
                            if (attrs.isRegularFile() && fileKinds.contains(getKind(file.getFileName().toString()))) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   562
                                JavaFileObject fe = PathFileObject.forJarPath(
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   563
                                        JavacFileManager.this, file, archivePath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   564
                                resultList.append(fe);
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   565
                            }
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   566
                            return FileVisitResult.CONTINUE;
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   567
                        }
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   568
                    });
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   569
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   570
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   571
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   572
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   573
        public JavaFileObject getFileObject(Path userPath, RelativeFile name) throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   574
            Path p = resolvePath(name);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   575
            if (p != null)
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   576
                return PathFileObject.forJarPath(JavacFileManager.this, p, userPath);
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   577
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   578
            return null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   579
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   580
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   581
        private synchronized Path resolvePath(RelativePath path) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   582
            if (!pathCache.containsKey(path)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   583
                Path relativePath = path.resolveAgainst(fileSystem);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   584
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   585
                if (!Files.exists(relativePath)) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   586
                    relativePath = null;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   587
                }
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   588
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   589
                pathCache.put(path, relativePath);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   590
                return relativePath;
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   591
            }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   592
            return pathCache.get(path);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   593
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   594
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   595
        @Override
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   596
        public void close() throws IOException {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   597
            fileSystem.close();
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   598
        }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   599
    }
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   600
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   601
    /**
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   602
     * container is a directory, a zip file, or a non-existent path.
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7335
diff changeset
   603
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
    private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   605
        JavaFileObject.Kind kind = getKind(s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
        return fileKinds.contains(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
    private static final boolean fileSystemIsCaseSensitive =
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        File.separatorChar == '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
    /** Hack to make Windows case sensitive. Test whether given path
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
     *  ends in a string of characters with the same case as given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
     *  Ignore file separators in both path and name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
     */
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   616
    private boolean caseMapCheck(Path f, RelativePath name) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        if (fileSystemIsCaseSensitive) return true;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   618
        // Note that toRealPath() returns the case-sensitive
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
        // spelled file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        String path;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   621
        char sep;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        try {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   623
            path = f.toRealPath(LinkOption.NOFOLLOW_LINKS).toString();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   624
            sep = f.getFileSystem().getSeparator().charAt(0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        char[] pcs = path.toCharArray();
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   629
        char[] ncs = name.path.toCharArray();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        int i = pcs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
        int j = ncs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        while (i >= 0 && j >= 0) {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   633
            while (i >= 0 && pcs[i] == sep) i--;
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   634
            while (j >= 0 && ncs[j] == '/') j--;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
            if (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
                if (pcs[i] != ncs[j]) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
                i--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
                j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        return j < 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
    /** Flush any output resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
     */
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   646
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        contentCache.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
     * Close the JavaFileManager, releasing resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
     */
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   654
    @Override @DefinedBy(Api.COMPILER)
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   655
    public void close() throws IOException {
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   656
        if (deferredCloseTimeout > 0) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   657
            deferredClose();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   658
            return;
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   659
        }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   660
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   661
        locations.close();
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   662
        for (Container container: containers.values()) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   663
            container.close();
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   664
        }
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   665
        containers.clear();
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 34560
diff changeset
   666
        contentCache.clear();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   669
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
    public ClassLoader getClassLoader(Location location) {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   671
        checkNotModuleOrientedLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
            return null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   675
        ListBuffer<URL> lb = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        for (File f: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
                lb.append(f.toURI().toURL());
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
                throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        }
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3380
diff changeset
   683
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
   684
        return getClassLoader(lb.toArray(new URL[lb.size()]));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   687
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
    public Iterable<JavaFileObject> list(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                                         String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
                                         Set<JavaFileObject.Kind> kinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
                                         boolean recurse)
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
    {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   694
        checkNotModuleOrientedLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
        nullCheck(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
        nullCheck(kinds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
27858
chegar
parents: 27579 27852
diff changeset
   699
        Iterable<? extends Path> path = getLocationAsPaths(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            return List.nil();
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   702
        RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   703
        ListBuffer<JavaFileObject> results = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   705
        for (Path directory : path) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   706
            Container container = getContainer(directory);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   707
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   708
            container.list(directory, subdirectory, kinds, recurse, results);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   709
        }
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   710
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        return results.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   714
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
    public String inferBinaryName(Location location, JavaFileObject file) {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   716
        checkNotModuleOrientedLocation(location);
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 28332
diff changeset
   717
        Objects.requireNonNull(file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        // Need to match the path semantics of list(location, ...)
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   719
        Iterable<? extends Path> path = getLocationAsPaths(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
        if (path == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   724
        if (file instanceof PathFileObject) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   725
            return ((PathFileObject) file).inferBinaryName(path);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
            throw new IllegalArgumentException(file.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   730
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
    public boolean isSameFile(FileObject a, FileObject b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        nullCheck(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
        nullCheck(b);
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   734
        if (a instanceof PathFileObject && b instanceof PathFileObject)
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   735
            return ((PathFileObject) a).isSameFile((PathFileObject) b);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        return a.equals(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   739
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
    public boolean hasLocation(Location location) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   741
        nullCheck(location);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   742
        return locations.hasLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   745
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
    public JavaFileObject getJavaFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                                              String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
                                              JavaFileObject.Kind kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
    {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   751
        checkNotModuleOrientedLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        if (!sourceOrClass.contains(kind))
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   756
            throw new IllegalArgumentException("Invalid kind: " + kind);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   757
        return getFileForInput(location, RelativeFile.forClass(className, kind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   760
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
    public FileObject getFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
                                      String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
                                      String relativeName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
    {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   766
        checkNotModuleOrientedLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        nullCheck(packageName);
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   769
        if (!isRelativeUri(relativeName))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   771
        RelativeFile name = packageName.length() == 0
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   772
            ? new RelativeFile(relativeName)
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   773
            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        return getFileForInput(location, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   777
    private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException {
27858
chegar
parents: 27579 27852
diff changeset
   778
        Iterable<? extends Path> path = getLocationAsPaths(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
27858
chegar
parents: 27579 27852
diff changeset
   782
        for (Path file: path) {
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   783
            JavaFileObject fo = getContainer(file).getFileObject(file, name);
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   784
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   785
            if (fo != null) {
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   786
                return fo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   792
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
    public JavaFileObject getJavaFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
                                               String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
                                               JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
                                               FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
    {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   799
        checkOutputLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        if (!sourceOrClass.contains(kind))
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   804
            throw new IllegalArgumentException("Invalid kind: " + kind);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   805
        return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   808
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
    public FileObject getFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                                       String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
                                       String relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
                                       FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
    {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   815
        checkOutputLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
        nullCheck(packageName);
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
   818
        if (!isRelativeUri(relativeName))
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
   819
            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   820
        RelativeFile name = packageName.length() == 0
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   821
            ? new RelativeFile(relativeName)
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   822
            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
        return getFileForOutput(location, name, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
    private JavaFileObject getFileForOutput(Location location,
1205
b316e32eb90c 6508981: cleanup file separator handling in JavacFileManager
jjg
parents: 815
diff changeset
   827
                                            RelativeFile fileName,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                                            FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
    {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   831
        Path dir;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        if (location == CLASS_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            if (getClassOutDir() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
                dir = getClassOutDir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
            } else {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   836
                String baseName = fileName.basename();
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   837
                if (sibling != null && sibling instanceof PathFileObject) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   838
                    return ((PathFileObject) sibling).getSibling(baseName);
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   839
                } else {
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   840
                    Path p = getPath(baseName);
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   841
                    Path real = fsInfo.getCanonicalFile(p);
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   842
                    return PathFileObject.forSimplePath(this, real, p);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
        } else if (location == SOURCE_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
            dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir());
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
        } else {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   848
            Iterable<? extends Path> path = locations.getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
            dir = null;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   850
            for (Path f: path) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
                dir = f;
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   856
        try {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   857
            if (dir == null) {
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   858
                dir = getPath(System.getProperty("user.dir"));
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   859
            }
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   860
            Path path = fileName.resolveAgainst(fsInfo.getCanonicalFile(dir));
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   861
            return PathFileObject.forDirectoryPath(this, path, dir, fileName);
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   862
        } catch (InvalidPathException e) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   863
            throw new IOException("bad filename " + fileName, e);
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   864
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   867
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
        Iterable<? extends File> files)
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
    {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   871
        ArrayList<PathFileObject> result;
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1208
diff changeset
   872
        if (files instanceof Collection<?>)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   873
            result = new ArrayList<>(((Collection<?>)files).size());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
        else
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14362
diff changeset
   875
            result = new ArrayList<>();
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   876
        for (File f: files) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   877
            Objects.requireNonNull(f);
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   878
            Path p = f.toPath();
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   879
            result.add(PathFileObject.forSimplePath(this,
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   880
                    fsInfo.getCanonicalFile(p), p));
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   881
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   885
    @Override @DefinedBy(Api.COMPILER)
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   886
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   887
        Iterable<? extends Path> paths)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   888
    {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   889
        ArrayList<PathFileObject> result;
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   890
        if (paths instanceof Collection<?>)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   891
            result = new ArrayList<>(((Collection<?>)paths).size());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   892
        else
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   893
            result = new ArrayList<>();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   894
        for (Path p: paths)
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   895
            result.add(PathFileObject.forSimplePath(this,
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   896
                    fsInfo.getCanonicalFile(p), p));
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   897
        return result;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   898
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   899
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   900
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
    public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
        return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   905
    @Override @DefinedBy(Api.COMPILER)
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   906
    public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   907
        return getJavaFileObjectsFromPaths(Arrays.asList(nullCheck(paths)));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   908
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   909
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   910
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
    public void setLocation(Location location,
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   912
                            Iterable<? extends File> searchpath)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
        nullCheck(location);
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   916
        locations.setLocation(location, asPaths(searchpath));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27380
diff changeset
   919
    @Override @DefinedBy(Api.COMPILER)
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   920
    public void setLocationFromPaths(Location location,
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37394
diff changeset
   921
                            Collection<? extends Path> searchpath)
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   922
        throws IOException
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   923
    {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   924
        nullCheck(location);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   925
        locations.setLocation(location, nullCheck(searchpath));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   926
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   927
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   928
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
    public Iterable<? extends File> getLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
        nullCheck(location);
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   931
        return asFiles(locations.getLocation(location));
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   932
    }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   933
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   934
    @Override @DefinedBy(Api.COMPILER)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   935
    public Iterable<? extends Path> getLocationAsPaths(Location location) {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   936
        nullCheck(location);
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   937
        return locations.getLocation(location);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   940
    private Path getClassOutDir() {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   941
        return locations.getOutputLocation(CLASS_OUTPUT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
   944
    private Path getSourceOutDir() {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   945
        return locations.getOutputLocation(SOURCE_OUTPUT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
   948
    @Override @DefinedBy(Api.COMPILER)
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   949
    public Location getLocationForModule(Location location, String moduleName) throws IOException {
42814
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
   950
        checkModuleOrientedOrOutputLocation(location);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   951
        nullCheck(moduleName);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42814
diff changeset
   952
        if (location == SOURCE_OUTPUT && getSourceOutDir() == null)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42814
diff changeset
   953
            location = CLASS_OUTPUT;
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   954
        return locations.getLocationForModule(location, moduleName);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   955
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   956
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   957
    @Override @DefinedBy(Api.COMPILER)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   958
    public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   959
        nullCheck(location);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   960
        nullCheck(service);
41033
49af2ecba616 8164742: ServiceConfigurationError on invoke of getServiceLoader method of StandardJavaFileManager
sadayapalam
parents: 40513
diff changeset
   961
        Module.getModule(getClass()).addUses(service);
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   962
        if (location.isModuleOrientedLocation()) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   963
            Collection<Path> paths = locations.getLocation(location);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   964
            ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   965
            Layer bootLayer = Layer.boot();
38533
2bd347dde526 8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents: 37944
diff changeset
   966
            Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.of(), finder, Collections.emptySet());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   967
            Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   968
            return ServiceLoaderHelper.load(layer, service);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   969
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   970
            return ServiceLoader.load(service, getClassLoader(location));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   971
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   972
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   973
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   974
    @Override @DefinedBy(Api.COMPILER)
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   975
    public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
42814
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
   976
        checkModuleOrientedOrOutputLocation(location);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   977
        if (!(fo instanceof PathFileObject))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   978
            throw new IllegalArgumentException(fo.getName());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   979
        int depth = 1; // allow 1 for filename
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   980
        if (pkgName != null && !pkgName.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   981
            depth += 1;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   982
            for (int i = 0; i < pkgName.length(); i++) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   983
                switch (pkgName.charAt(i)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   984
                    case '/': case '.':
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   985
                        depth++;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   986
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   987
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   988
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   989
        Path p = Locations.normalize(((PathFileObject) fo).path);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   990
        int fc = p.getNameCount();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   991
        if (depth < fc) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   992
            Path root = p.getRoot();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   993
            Path subpath = p.subpath(0, fc - depth);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   994
            Path dir = (root == null) ? subpath : root.resolve(subpath);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   995
            // need to find dir in location
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
   996
            return locations.getLocationForModule(location, dir);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   997
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   998
            return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
   999
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1000
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1001
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1002
    @Override @DefinedBy(Api.COMPILER)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1003
    public String inferModuleName(Location location) {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1004
        checkNotModuleOrientedLocation(location);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1005
        return locations.inferModuleName(location);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1006
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1007
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1008
    @Override @DefinedBy(Api.COMPILER)
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1009
    public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {
42814
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
  1010
        checkModuleOrientedOrOutputLocation(location);
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1011
        return locations.listLocationsForModules(location);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1012
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1013
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36158
diff changeset
  1014
    @Override @DefinedBy(Api.COMPILER)
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1015
    public Path asPath(FileObject file) {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
  1016
        if (file instanceof PathFileObject) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
  1017
            return ((PathFileObject) file).path;
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1018
        } else
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1019
            throw new IllegalArgumentException(file.getName());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1020
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1021
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
    /**
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10818
diff changeset
  1023
     * Enforces the specification of a "relative" name as used in
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10818
diff changeset
  1024
     * {@linkplain #getFileForInput(Location,String,String)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
     * getFileForInput}.  This method must follow the rules defined in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
     * that method, do not make any changes without consulting the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
     * specification.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
    protected static boolean isRelativeUri(URI uri) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
        if (uri.isAbsolute())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
        String path = uri.normalize().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
        if (path.length() == 0 /* isEmpty() is mustang API */)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            return false;
6719
1ce993f87850 6502392: Invalid relative names for Filer.createResource and Filer.getResource
jjg
parents: 5847
diff changeset
  1035
        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
  1036
            return false;
7840
dfc4669cbc84 6999891: DefaultFileManager incorrect
jjg
parents: 7839
diff changeset
  1037
        if (path.startsWith("/") || path.startsWith("./") || path.startsWith("../"))
dfc4669cbc84 6999891: DefaultFileManager incorrect
jjg
parents: 7839
diff changeset
  1038
            return false;
dfc4669cbc84 6999891: DefaultFileManager incorrect
jjg
parents: 7839
diff changeset
  1039
        return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1042
    // Convenience method
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1043
    protected static boolean isRelativeUri(String u) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1044
        try {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1045
            return isRelativeUri(new URI(u));
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1046
        } catch (URISyntaxException e) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1047
            return false;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1048
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1049
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1050
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
     * Converts a relative file name to a relative URI.  This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
     * different from File.toURI as this method does not canonicalize
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
     * the file before creating the URI.  Furthermore, no schema is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
     * used.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
     * @param file a relative file name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
     * @return a relative URI
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
     * @throws IllegalArgumentException if the file name is not
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
     * relative according to the definition given in {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
     * javax.tools.JavaFileManager#getFileForInput}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
    public static String getRelativeName(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        if (!file.isAbsolute()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
            String result = file.getPath().replace(File.separatorChar, '/');
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3656
diff changeset
  1065
            if (isRelativeUri(result))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
        throw new IllegalArgumentException("Invalid relative path: " + file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
    }
5007
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1070
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1071
    /**
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1072
     * 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
  1073
     * 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
  1074
     * 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
  1075
     * 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
  1076
     * name of the exception itself.
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1077
     * @param e an IOException
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1078
     * @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
  1079
     */
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1080
    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
  1081
        String s = e.getLocalizedMessage();
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1082
        if (s != null)
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1083
            return s;
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1084
        s = e.getMessage();
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1085
        if (s != null)
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1086
            return s;
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1087
        return e.toString();
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4548
diff changeset
  1088
    }
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1089
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1090
    private void checkOutputLocation(Location location) {
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1091
        Objects.requireNonNull(location);
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1092
        if (!location.isOutputLocation())
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1093
            throw new IllegalArgumentException("location is not an output location: " + location.getName());
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1094
    }
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1095
42814
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
  1096
    private void checkModuleOrientedOrOutputLocation(Location location) {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1097
        Objects.requireNonNull(location);
42814
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
  1098
        if (!location.isModuleOrientedLocation() && !location.isOutputLocation())
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
  1099
            throw new IllegalArgumentException(
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
  1100
                    "location is not an output location or a module-oriented location: "
058fc03646d9 8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
jlahoda
parents: 42261
diff changeset
  1101
                            + location.getName());
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1102
    }
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1103
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1104
    private void checkNotModuleOrientedLocation(Location location) {
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1105
        Objects.requireNonNull(location);
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1106
        if (location.isModuleOrientedLocation())
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1107
            throw new IllegalArgumentException("location is module-oriented: " + location.getName());
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1108
    }
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41033
diff changeset
  1109
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1110
    /* Converters between files and paths.
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1111
     * These are temporary until we can update the StandardJavaFileManager API.
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1112
     */
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1113
27858
chegar
parents: 27579 27852
diff changeset
  1114
    private static Iterable<Path> asPaths(final Iterable<? extends File> files) {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1115
        if (files == null)
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1116
            return null;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1117
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1118
        return () -> new Iterator<Path>() {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1119
            Iterator<? extends File> iter = files.iterator();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1120
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1121
            @Override
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1122
            public boolean hasNext() {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1123
                return iter.hasNext();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1124
            }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1125
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1126
            @Override
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1127
            public Path next() {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1128
                return iter.next().toPath();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1129
            }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1130
        };
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1131
    }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1132
27858
chegar
parents: 27579 27852
diff changeset
  1133
    private static Iterable<File> asFiles(final Iterable<? extends Path> paths) {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1134
        if (paths == null)
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1135
            return null;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1136
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1137
        return () -> new Iterator<File>() {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1138
            Iterator<? extends Path> iter = paths.iterator();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1139
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1140
            @Override
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1141
            public boolean hasNext() {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1142
                return iter.hasNext();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1143
            }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1144
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1145
            @Override
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1146
            public File next() {
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1147
                try {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1148
                    return iter.next().toFile();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1149
                } catch (UnsupportedOperationException e) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1150
                    throw new IllegalStateException(e);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents: 27858
diff changeset
  1151
                }
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1152
            }
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1153
        };
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27380
diff changeset
  1154
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
}