FSInfo.java revision 2675:4be0e35f385a
18870Srgrimes 217935Speterpackage com.sun.tools.javac.file; 38870Srgrimes 42116Sjkhimport java.io.File; 52116Sjkhimport java.io.IOException; 68870Srgrimesimport java.util.ArrayList; 72116Sjkhimport java.util.Collections; 82116Sjkhimport java.util.List; 98870Srgrimesimport java.util.StringTokenizer; 102116Sjkhimport java.util.jar.Attributes; 112116Sjkhimport java.util.jar.JarFile; 128870Srgrimesimport java.util.jar.Manifest; 138870Srgrimes 142116Sjkhimport com.sun.tools.javac.util.Context; 152116Sjkh 162116Sjkh/** 172116Sjkh * Get meta-info about files. Default direct (non-caching) implementation. 188870Srgrimes * @see CacheFSInfo 198870Srgrimes * 202116Sjkh * <p><b>This is NOT part of any supported API. 212116Sjkh * If you write code that depends on this, you do so at your own risk. 222116Sjkh * This code and its internal interfaces are subject to change or 232116Sjkh * deletion without notice.</b> 242116Sjkh */ 252116Sjkhpublic class FSInfo { 268870Srgrimes 272116Sjkh /** Get the FSInfo instance for this context. 282116Sjkh * @param context the context 292116Sjkh * @return the Paths instance for this context 302116Sjkh */ 318870Srgrimes public static FSInfo instance(Context context) { 322116Sjkh FSInfo instance = context.get(FSInfo.class); 332116Sjkh if (instance == null) 348870Srgrimes instance = new FSInfo(); 352116Sjkh return instance; 362116Sjkh } 378870Srgrimes 382116Sjkh protected FSInfo() { 392116Sjkh } 408870Srgrimes 412116Sjkh protected FSInfo(Context context) { 428870Srgrimes context.put(FSInfo.class, this); 432116Sjkh } 442116Sjkh 452116Sjkh public File getCanonicalFile(File file) { 462122Sjkh try { 472116Sjkh return file.getCanonicalFile(); 482116Sjkh } catch (IOException e) { 492116Sjkh return file.getAbsoluteFile(); 502116Sjkh } 512116Sjkh } 522116Sjkh 532116Sjkh public boolean exists(File file) { 542116Sjkh return file.exists(); 552116Sjkh } 568870Srgrimes 572116Sjkh public boolean isDirectory(File file) { 582116Sjkh return file.isDirectory(); 592116Sjkh } 602116Sjkh 612116Sjkh public boolean isFile(File file) { 622116Sjkh return file.isFile(); 632116Sjkh } 642116Sjkh 652116Sjkh public List<File> getJarClassPath(File file) throws IOException { 662116Sjkh String parent = file.getParent(); 672116Sjkh try (JarFile jarFile = new JarFile(file)) { 682116Sjkh Manifest man = jarFile.getManifest(); 692116Sjkh if (man == null) 702116Sjkh return Collections.emptyList(); 712116Sjkh 722116Sjkh Attributes attr = man.getMainAttributes(); 732116Sjkh if (attr == null) 742116Sjkh return Collections.emptyList(); 752116Sjkh 762116Sjkh String path = attr.getValue(Attributes.Name.CLASS_PATH); 772116Sjkh if (path == null) 782116Sjkh return Collections.emptyList(); 792116Sjkh 802116Sjkh List<File> list = new ArrayList<>(); 812116Sjkh 822116Sjkh for (StringTokenizer st = new StringTokenizer(path); 832116Sjkh st.hasMoreTokens(); ) { 842116Sjkh String elt = st.nextToken(); 852116Sjkh File f = new File(elt); 862116Sjkh if (!f.isAbsolute() && parent != null) 872116Sjkh f = new File(parent,elt).getAbsoluteFile(); 882116Sjkh list.add(f); 892116Sjkh } 902116Sjkh 916953Sbde return list; 922116Sjkh } 938870Srgrimes } 946953Sbde} 956953Sbde