langtools/src/share/classes/com/sun/tools/javac/file/FSInfo.java
author pgovereau
Mon, 21 Apr 2014 17:57:47 -0400
changeset 24222 244127f8dd79
parent 22163 3651128c74eb
permissions -rw-r--r--
8030046: javac incorrectly handles absolute paths in manifest classpath Reviewed-by: jjg, vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     1
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     2
package com.sun.tools.javac.file;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     3
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     4
import java.io.File;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     5
import java.io.IOException;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     6
import java.util.ArrayList;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     7
import java.util.Collections;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     8
import java.util.List;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     9
import java.util.StringTokenizer;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    10
import java.util.jar.Attributes;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    11
import java.util.jar.JarFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    12
import java.util.jar.Manifest;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    13
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    14
import com.sun.tools.javac.util.Context;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    15
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    16
/**
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    17
 * 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
    18
 * @see CacheFSInfo
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    19
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 3380
diff changeset
    20
 * <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
    21
 * 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
    22
 * 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
    23
 * deletion without notice.</b>
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    24
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    25
public class FSInfo {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    26
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    27
    /** Get the FSInfo instance for this context.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    28
     *  @param context the context
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    29
     *  @return the Paths instance for this context
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    30
     */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    31
    public static FSInfo instance(Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    32
        FSInfo instance = context.get(FSInfo.class);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    33
        if (instance == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    34
            instance = new FSInfo();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    35
        return instance;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    36
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    37
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    38
    protected FSInfo() {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    39
    }
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
    protected FSInfo(Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    42
        context.put(FSInfo.class, this);
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
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    45
    public File getCanonicalFile(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    46
        try {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    47
            return file.getCanonicalFile();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    48
        } catch (IOException e) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    49
            return file.getAbsoluteFile();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    50
        }
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
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    53
    public boolean exists(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    54
        return file.exists();
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
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    57
    public boolean isDirectory(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    58
        return file.isDirectory();
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
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    61
    public boolean isFile(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    62
        return file.isFile();
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
    public List<File> getJarClassPath(File file) throws IOException {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    66
        String parent = file.getParent();
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
    67
        try (JarFile jarFile = new JarFile(file)) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    68
            Manifest man = jarFile.getManifest();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    69
            if (man == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    70
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    71
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    72
            Attributes attr = man.getMainAttributes();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    73
            if (attr == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    74
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    75
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    76
            String path = attr.getValue(Attributes.Name.CLASS_PATH);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    77
            if (path == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    78
                return Collections.emptyList();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    79
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
    80
            List<File> list = new ArrayList<>();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    81
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
    82
            for (StringTokenizer st = new StringTokenizer(path);
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 5847
diff changeset
    83
                 st.hasMoreTokens(); ) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    84
                String elt = st.nextToken();
24222
244127f8dd79 8030046: javac incorrectly handles absolute paths in manifest classpath
pgovereau
parents: 22163
diff changeset
    85
                try {
244127f8dd79 8030046: javac incorrectly handles absolute paths in manifest classpath
pgovereau
parents: 22163
diff changeset
    86
                    File f = parent == null ? new File(elt): new File(file.toURI().resolve(elt));
244127f8dd79 8030046: javac incorrectly handles absolute paths in manifest classpath
pgovereau
parents: 22163
diff changeset
    87
                    list.add(f);
244127f8dd79 8030046: javac incorrectly handles absolute paths in manifest classpath
pgovereau
parents: 22163
diff changeset
    88
                } catch (IllegalArgumentException ex) {}
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    89
            }
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
            return list;
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
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    94
}