langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java
author jlahoda
Mon, 22 Aug 2016 09:59:43 +0200
changeset 40513 39b67170b045
parent 37944 1153fab98d25
permissions -rw-r--r--
8153391: an image created for \"jdk.compiler\" fails to run javac Summary: Improving errors produced by javac when the zipfs implementation is missing in the JDK image. Reviewed-by: jjg
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
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    28
import java.io.IOException;
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37633
diff changeset
    29
import java.nio.file.FileSystems;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    30
import java.nio.file.Files;
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    31
import java.nio.file.Path;
40513
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
    32
import java.nio.file.spi.FileSystemProvider;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    33
import java.util.ArrayList;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    34
import java.util.Collections;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    35
import java.util.List;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    36
import java.util.StringTokenizer;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    37
import java.util.jar.Attributes;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    38
import java.util.jar.JarFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    39
import java.util.jar.Manifest;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    40
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    41
import com.sun.tools.javac.util.Context;
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
/**
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    44
 * 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
    45
 * @see CacheFSInfo
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    46
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 3380
diff changeset
    47
 * <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
    48
 * 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
    49
 * 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
    50
 * deletion without notice.</b>
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    51
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    52
public class FSInfo {
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
    /** Get the FSInfo instance for this context.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    55
     *  @param context the context
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    56
     *  @return the Paths instance for this context
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    57
     */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    58
    public static FSInfo instance(Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    59
        FSInfo instance = context.get(FSInfo.class);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    60
        if (instance == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    61
            instance = new FSInfo();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    62
        return instance;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    63
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    64
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    65
    protected FSInfo() {
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
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    68
    protected FSInfo(Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    69
        context.put(FSInfo.class, this);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    70
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    71
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    72
    public Path getCanonicalFile(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    73
        try {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    74
            return file.toRealPath();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    75
        } catch (IOException e) {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 27852
diff changeset
    76
            return file.toAbsolutePath().normalize();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    77
        }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    78
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    79
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    80
    public boolean exists(Path file) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    81
        return Files.exists(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    82
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    83
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    84
    public boolean isDirectory(Path file) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    85
        return Files.isDirectory(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    86
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    87
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    88
    public boolean isFile(Path file) {
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    89
        return Files.isRegularFile(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    90
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    91
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    92
    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
    93
        Path parent = file.getParent();
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    94
        try (JarFile jarFile = new JarFile(file.toFile())) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    95
            Manifest man = jarFile.getManifest();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    96
            if (man == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    97
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    98
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    99
            Attributes attr = man.getMainAttributes();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   100
            if (attr == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   101
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   102
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   103
            String path = attr.getValue(Attributes.Name.CLASS_PATH);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   104
            if (path == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   105
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   106
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   107
            List<Path> list = new ArrayList<>();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   108
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
   109
            for (StringTokenizer st = new StringTokenizer(path);
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
   110
                 st.hasMoreTokens(); ) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   111
                String elt = st.nextToken();
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37633
diff changeset
   112
                Path f = FileSystems.getDefault().getPath(elt);
26535
24dfa5fd8fe2 8055500: fix for 8030046 is incorrect
pgovereau
parents: 25874
diff changeset
   113
                if (!f.isAbsolute() && parent != null)
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   114
                    f = parent.resolve(f).toAbsolutePath();
26535
24dfa5fd8fe2 8055500: fix for 8030046 is incorrect
pgovereau
parents: 25874
diff changeset
   115
                list.add(f);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   116
            }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   117
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   118
            return list;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   119
        }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   120
    }
40513
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   121
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   122
    private FileSystemProvider jarFSProvider;
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   123
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   124
    public synchronized FileSystemProvider getJarFSProvider() {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   125
        if (jarFSProvider != null) {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   126
            return jarFSProvider;
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
        for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   129
            if (provider.getScheme().equals("jar")) {
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   130
                return (jarFSProvider = provider);
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   131
            }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   132
        }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   133
        return null;
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   134
    }
39b67170b045 8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents: 37944
diff changeset
   135
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   136
}