src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect/FileSupport.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58533 46b0b7fe255c
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     1
/*
58533
46b0b7fe255c 8231973: Update Graal
dlong
parents: 58387
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     4
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     8
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    13
 * accompanied this code).
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    14
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    18
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    21
 * questions.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    22
 */
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    23
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    24
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    25
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    26
package jdk.tools.jaotc.collect;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    27
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    28
import java.io.IOException;
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    29
import java.net.MalformedURLException;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    30
import java.net.URI;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    31
import java.net.URISyntaxException;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    32
import java.net.URL;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    33
import java.net.URLClassLoader;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    34
import java.nio.file.DirectoryStream;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    35
import java.nio.file.FileSystem;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    36
import java.nio.file.FileSystems;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    37
import java.nio.file.Files;
091c0d22e735 8206992: Update Graal
iveresov
parents: 47216
diff changeset
    38
import java.nio.file.Path;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    39
import java.util.HashMap;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    40
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    41
public class FileSupport {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    42
    public boolean exists(Path path) {
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    43
        return Files.exists(path);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    44
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    45
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    46
    public boolean isDirectory(Path path) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    47
        return Files.isDirectory(path);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    48
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    49
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    50
    private static FileSystem makeJarFileSystem(Path path) {
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    51
        try {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    52
            return FileSystems.newFileSystem(makeJarFileURI(path), new HashMap<>());
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    53
        } catch (IOException e) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    54
            throw new InternalError(e);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    55
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    56
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    57
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    58
    private static URI makeJarFileURI(Path path) {
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    59
        try {
46583
873282753046 8182052: Test compiler/aot/cli/jaotc/CompileJarTest.java fails in JPRT
bobv
parents: 43464
diff changeset
    60
            String name = path.toAbsolutePath().toString();
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    61
            name = name.replace('\\', '/');
58387
c5b80da6b561 8231431: JAOTC issues with JAR path containing spaces
bobv
parents: 52910
diff changeset
    62
            return new URI("jar:file", null, "///" + name + "!/", null);
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    63
        } catch (URISyntaxException e) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    64
            throw new InternalError(e);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    65
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    66
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    67
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    68
    public ClassLoader createClassLoader(Path path, ClassLoader parent) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    69
        try {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    70
            return URLClassLoader.newInstance(buildUrls(path), parent);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    71
        } catch (MalformedURLException e) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    72
            throw new InternalError(e);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    73
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    74
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    75
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    76
    public ClassLoader createClassLoader(Path path) throws MalformedURLException {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    77
        return URLClassLoader.newInstance(buildUrls(path));
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    78
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    79
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    80
    private static URL[] buildUrls(Path path) throws MalformedURLException {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    81
        return new URL[]{path.toUri().toURL()};
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    82
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    83
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    84
    public Path getJarFileSystemRoot(Path jarFile) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    85
        FileSystem fileSystem = makeJarFileSystem(jarFile);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    86
        return fileSystem.getPath("/");
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    87
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    88
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    89
    public boolean isAbsolute(Path entry) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    90
        return entry.isAbsolute();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    91
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    92
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    93
    public Path getSubDirectory(FileSystem fileSystem, Path root, Path path) throws IOException {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46583
diff changeset
    94
        DirectoryStream<Path> paths = fileSystem.provider().newDirectoryStream(root, null);
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    95
        for (Path entry : paths) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    96
            Path relative = root.relativize(entry);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    97
            if (relative.equals(path)) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    98
                return entry;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    99
            }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
   100
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
   101
        return null;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
   102
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
   103
}