src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java
author jjg
Fri, 22 Nov 2019 10:38:35 -0800
changeset 59229 fba8fa613d1a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause Reviewed-by: jjg Contributed-by: jai.forums2013@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37633
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     1
/*
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37633
diff changeset
     2
 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
37633
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     4
 *
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    10
 *
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    15
 * accompanied this code).
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    16
 *
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    20
 *
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    23
 * questions.
d16d6d59446d 8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents: 34560
diff changeset
    24
 */
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    25
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.file;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    27
59229
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
    28
import java.io.IOError;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    29
import java.io.IOException;
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37633
diff changeset
    30
import java.nio.file.FileSystems;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    31
import java.nio.file.Files;
59229
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
    32
import java.nio.file.InvalidPathException;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    33
import java.nio.file.Path;
40513
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
    34
import java.nio.file.spi.FileSystemProvider;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    35
import java.util.ArrayList;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    36
import java.util.Collections;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    37
import java.util.List;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    38
import java.util.StringTokenizer;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    39
import java.util.jar.Attributes;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    40
import java.util.jar.JarFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    41
import java.util.jar.Manifest;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    42
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    43
import com.sun.tools.javac.util.Context;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    44
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    45
/**
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    46
 * Get meta-info about files. Default direct (non-caching) implementation.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    47
 * @see CacheFSInfo
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    48
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 3380
diff changeset
    49
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    50
 * 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: 1208
diff changeset
    51
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    52
 * deletion without notice.</b>
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    53
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    54
public class FSInfo {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    55
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    56
    /** Get the FSInfo instance for this context.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    57
     *  @param context the context
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    58
     *  @return the Paths instance for this context
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    59
     */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    60
    public static FSInfo instance(Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    61
        FSInfo instance = context.get(FSInfo.class);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    62
        if (instance == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    63
            instance = new FSInfo();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    64
        return instance;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    65
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    66
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    67
    protected FSInfo() {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    68
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    69
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    70
    protected FSInfo(Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    71
        context.put(FSInfo.class, this);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    72
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    73
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    74
    public Path getCanonicalFile(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    75
        try {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    76
            return file.toRealPath();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    77
        } catch (IOException e) {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 27852
diff changeset
    78
            return file.toAbsolutePath().normalize();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    79
        }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    80
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    81
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    82
    public boolean exists(Path file) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    83
        return Files.exists(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    84
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    85
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    86
    public boolean isDirectory(Path file) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    87
        return Files.isDirectory(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    88
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    89
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    90
    public boolean isFile(Path file) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    91
        return Files.isRegularFile(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    92
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    93
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    94
    public List<Path> getJarClassPath(Path file) throws IOException {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    95
        Path parent = file.getParent();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    96
        try (JarFile jarFile = new JarFile(file.toFile())) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    97
            Manifest man = jarFile.getManifest();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    98
            if (man == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    99
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   100
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   101
            Attributes attr = man.getMainAttributes();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   102
            if (attr == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   103
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   104
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   105
            String path = attr.getValue(Attributes.Name.CLASS_PATH);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   106
            if (path == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   107
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   108
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   109
            List<Path> list = new ArrayList<>();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   110
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
   111
            for (StringTokenizer st = new StringTokenizer(path);
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
   112
                 st.hasMoreTokens(); ) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   113
                String elt = st.nextToken();
59229
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   114
                try {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   115
                    Path f = FileSystems.getDefault().getPath(elt);
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   116
                    if (!f.isAbsolute() && parent != null)
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   117
                        f = parent.resolve(f).toAbsolutePath();
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   118
                    list.add(f);
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   119
                } catch (InvalidPathException | IOError e) {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   120
                    throw new IOException(e);
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents: 47216
diff changeset
   121
                }
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   122
            }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   123
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   124
            return list;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   125
        }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   126
    }
40513
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   127
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   128
    private FileSystemProvider jarFSProvider;
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   129
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   130
    public synchronized FileSystemProvider getJarFSProvider() {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   131
        if (jarFSProvider != null) {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   132
            return jarFSProvider;
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   133
        }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   134
        for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   135
            if (provider.getScheme().equals("jar")) {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   136
                return (jarFSProvider = provider);
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   137
            }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   138
        }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   139
        return null;
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   140
    }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   141
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   142
}