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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 581
f2fdd52e4e87
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial