langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/FSInfo.java
changeset 25874 83c19f00452c
parent 24222 244127f8dd79
child 26535 24dfa5fd8fe2
equal deleted inserted replaced
25873:024ed9c9ed13 25874:83c19f00452c
       
     1 
       
     2 package com.sun.tools.javac.file;
       
     3 
       
     4 import java.io.File;
       
     5 import java.io.IOException;
       
     6 import java.util.ArrayList;
       
     7 import java.util.Collections;
       
     8 import java.util.List;
       
     9 import java.util.StringTokenizer;
       
    10 import java.util.jar.Attributes;
       
    11 import java.util.jar.JarFile;
       
    12 import java.util.jar.Manifest;
       
    13 
       
    14 import com.sun.tools.javac.util.Context;
       
    15 
       
    16 /**
       
    17  * Get meta-info about files. Default direct (non-caching) implementation.
       
    18  * @see CacheFSInfo
       
    19  *
       
    20  * <p><b>This is NOT part of any supported API.
       
    21  * If you write code that depends on this, you do so at your own risk.
       
    22  * This code and its internal interfaces are subject to change or
       
    23  * deletion without notice.</b>
       
    24  */
       
    25 public class FSInfo {
       
    26 
       
    27     /** Get the FSInfo instance for this context.
       
    28      *  @param context the context
       
    29      *  @return the Paths instance for this context
       
    30      */
       
    31     public static FSInfo instance(Context context) {
       
    32         FSInfo instance = context.get(FSInfo.class);
       
    33         if (instance == null)
       
    34             instance = new FSInfo();
       
    35         return instance;
       
    36     }
       
    37 
       
    38     protected FSInfo() {
       
    39     }
       
    40 
       
    41     protected FSInfo(Context context) {
       
    42         context.put(FSInfo.class, this);
       
    43     }
       
    44 
       
    45     public File getCanonicalFile(File file) {
       
    46         try {
       
    47             return file.getCanonicalFile();
       
    48         } catch (IOException e) {
       
    49             return file.getAbsoluteFile();
       
    50         }
       
    51     }
       
    52 
       
    53     public boolean exists(File file) {
       
    54         return file.exists();
       
    55     }
       
    56 
       
    57     public boolean isDirectory(File file) {
       
    58         return file.isDirectory();
       
    59     }
       
    60 
       
    61     public boolean isFile(File file) {
       
    62         return file.isFile();
       
    63     }
       
    64 
       
    65     public List<File> getJarClassPath(File file) throws IOException {
       
    66         String parent = file.getParent();
       
    67         try (JarFile jarFile = new JarFile(file)) {
       
    68             Manifest man = jarFile.getManifest();
       
    69             if (man == null)
       
    70                 return Collections.emptyList();
       
    71 
       
    72             Attributes attr = man.getMainAttributes();
       
    73             if (attr == null)
       
    74                 return Collections.emptyList();
       
    75 
       
    76             String path = attr.getValue(Attributes.Name.CLASS_PATH);
       
    77             if (path == null)
       
    78                 return Collections.emptyList();
       
    79 
       
    80             List<File> list = new ArrayList<>();
       
    81 
       
    82             for (StringTokenizer st = new StringTokenizer(path);
       
    83                  st.hasMoreTokens(); ) {
       
    84                 String elt = st.nextToken();
       
    85                 try {
       
    86                     File f = parent == null ? new File(elt): new File(file.toURI().resolve(elt));
       
    87                     list.add(f);
       
    88                 } catch (IllegalArgumentException ex) {}
       
    89             }
       
    90 
       
    91             return list;
       
    92         }
       
    93     }
       
    94 }