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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 581
f2fdd52e4e87
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

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

mercurial