src/share/classes/com/sun/tools/javac/file/FSInfo.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/FSInfo.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,94 @@
     1.4 +
     1.5 +package com.sun.tools.javac.file;
     1.6 +
     1.7 +import java.io.File;
     1.8 +import java.io.IOException;
     1.9 +import java.util.ArrayList;
    1.10 +import java.util.Collections;
    1.11 +import java.util.List;
    1.12 +import java.util.StringTokenizer;
    1.13 +import java.util.jar.Attributes;
    1.14 +import java.util.jar.JarFile;
    1.15 +import java.util.jar.Manifest;
    1.16 +
    1.17 +import com.sun.tools.javac.util.Context;
    1.18 +
    1.19 +/**
    1.20 + * Get meta-info about files. Default direct (non-caching) implementation.
    1.21 + * @see CacheFSInfo
    1.22 + *
    1.23 + * <p><b>This is NOT part of any supported API.
    1.24 + * If you write code that depends on this, you do so at your own risk.
    1.25 + * This code and its internal interfaces are subject to change or
    1.26 + * deletion without notice.</b>
    1.27 + */
    1.28 +public class FSInfo {
    1.29 +
    1.30 +    /** Get the FSInfo instance for this context.
    1.31 +     *  @param context the context
    1.32 +     *  @return the Paths instance for this context
    1.33 +     */
    1.34 +    public static FSInfo instance(Context context) {
    1.35 +        FSInfo instance = context.get(FSInfo.class);
    1.36 +        if (instance == null)
    1.37 +            instance = new FSInfo();
    1.38 +        return instance;
    1.39 +    }
    1.40 +
    1.41 +    protected FSInfo() {
    1.42 +    }
    1.43 +
    1.44 +    protected FSInfo(Context context) {
    1.45 +        context.put(FSInfo.class, this);
    1.46 +    }
    1.47 +
    1.48 +    public File getCanonicalFile(File file) {
    1.49 +        try {
    1.50 +            return file.getCanonicalFile();
    1.51 +        } catch (IOException e) {
    1.52 +            return file.getAbsoluteFile();
    1.53 +        }
    1.54 +    }
    1.55 +
    1.56 +    public boolean exists(File file) {
    1.57 +        return file.exists();
    1.58 +    }
    1.59 +
    1.60 +    public boolean isDirectory(File file) {
    1.61 +        return file.isDirectory();
    1.62 +    }
    1.63 +
    1.64 +    public boolean isFile(File file) {
    1.65 +        return file.isFile();
    1.66 +    }
    1.67 +
    1.68 +    public List<File> getJarClassPath(File file) throws IOException {
    1.69 +        String parent = file.getParent();
    1.70 +        JarFile jarFile = new JarFile(file);
    1.71 +        try {
    1.72 +            Manifest man = jarFile.getManifest();
    1.73 +            if (man == null)
    1.74 +                return Collections.emptyList();
    1.75 +
    1.76 +            Attributes attr = man.getMainAttributes();
    1.77 +            if (attr == null)
    1.78 +                return Collections.emptyList();
    1.79 +
    1.80 +            String path = attr.getValue(Attributes.Name.CLASS_PATH);
    1.81 +            if (path == null)
    1.82 +                return Collections.emptyList();
    1.83 +
    1.84 +            List<File> list = new ArrayList<File>();
    1.85 +
    1.86 +            for (StringTokenizer st = new StringTokenizer(path); st.hasMoreTokens(); ) {
    1.87 +                String elt = st.nextToken();
    1.88 +                File f = (parent == null ? new File(elt) : new File(parent, elt));
    1.89 +                list.add(f);
    1.90 +            }
    1.91 +
    1.92 +            return list;
    1.93 +        } finally {
    1.94 +            jarFile.close();
    1.95 +        }
    1.96 +    }
    1.97 +}

mercurial