Wed, 03 Sep 2008 10:46:25 -0700
6743107: clean up use of static caches in file manager
Reviewed-by: mcimadamore
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@106 | 19 | */ |
jjg@106 | 20 | public class FSInfo { |
jjg@106 | 21 | |
jjg@106 | 22 | /** Get the FSInfo instance for this context. |
jjg@106 | 23 | * @param context the context |
jjg@106 | 24 | * @return the Paths instance for this context |
jjg@106 | 25 | */ |
jjg@106 | 26 | public static FSInfo instance(Context context) { |
jjg@106 | 27 | FSInfo instance = context.get(FSInfo.class); |
jjg@106 | 28 | if (instance == null) |
jjg@106 | 29 | instance = new FSInfo(); |
jjg@106 | 30 | return instance; |
jjg@106 | 31 | } |
jjg@106 | 32 | |
jjg@106 | 33 | protected FSInfo() { |
jjg@106 | 34 | } |
jjg@106 | 35 | |
jjg@106 | 36 | protected FSInfo(Context context) { |
jjg@106 | 37 | context.put(FSInfo.class, this); |
jjg@106 | 38 | } |
jjg@106 | 39 | |
jjg@106 | 40 | public File getCanonicalFile(File file) { |
jjg@106 | 41 | try { |
jjg@106 | 42 | return file.getCanonicalFile(); |
jjg@106 | 43 | } catch (IOException e) { |
jjg@106 | 44 | return file.getAbsoluteFile(); |
jjg@106 | 45 | } |
jjg@106 | 46 | } |
jjg@106 | 47 | |
jjg@106 | 48 | public boolean exists(File file) { |
jjg@106 | 49 | return file.exists(); |
jjg@106 | 50 | } |
jjg@106 | 51 | |
jjg@106 | 52 | public boolean isDirectory(File file) { |
jjg@106 | 53 | return file.isDirectory(); |
jjg@106 | 54 | } |
jjg@106 | 55 | |
jjg@106 | 56 | public boolean isFile(File file) { |
jjg@106 | 57 | return file.isFile(); |
jjg@106 | 58 | } |
jjg@106 | 59 | |
jjg@106 | 60 | public List<File> getJarClassPath(File file) throws IOException { |
jjg@106 | 61 | String parent = file.getParent(); |
jjg@106 | 62 | JarFile jarFile = new JarFile(file); |
jjg@106 | 63 | try { |
jjg@106 | 64 | Manifest man = jarFile.getManifest(); |
jjg@106 | 65 | if (man == null) |
jjg@106 | 66 | return Collections.emptyList(); |
jjg@106 | 67 | |
jjg@106 | 68 | Attributes attr = man.getMainAttributes(); |
jjg@106 | 69 | if (attr == null) |
jjg@106 | 70 | return Collections.emptyList(); |
jjg@106 | 71 | |
jjg@106 | 72 | String path = attr.getValue(Attributes.Name.CLASS_PATH); |
jjg@106 | 73 | if (path == null) |
jjg@106 | 74 | return Collections.emptyList(); |
jjg@106 | 75 | |
jjg@106 | 76 | List<File> list = new ArrayList<File>(); |
jjg@106 | 77 | |
jjg@106 | 78 | for (StringTokenizer st = new StringTokenizer(path); st.hasMoreTokens(); ) { |
jjg@106 | 79 | String elt = st.nextToken(); |
jjg@106 | 80 | File f = (parent == null ? new File(elt) : new File(parent, elt)); |
jjg@106 | 81 | list.add(f); |
jjg@106 | 82 | } |
jjg@106 | 83 | |
jjg@106 | 84 | return list; |
jjg@106 | 85 | } finally { |
jjg@106 | 86 | jarFile.close(); |
jjg@106 | 87 | } |
jjg@106 | 88 | } |
jjg@106 | 89 | } |