1 2 12 package com.versant.core.jdo.tools.enhancer.utils; 13 14 import java.util.*; 15 import java.io.*; 16 17 import com.versant.lib.bcel.classfile.*; 18 import com.versant.lib.bcel.generic.*; 19 import com.versant.lib.bcel.Constants; 20 import com.versant.lib.bcel.*; 21 import com.versant.lib.bcel.Repository; 22 23 28 public class FileInfoUtil { 29 30 private ArrayList list = new ArrayList(); 31 private ArrayList allClassesList; 32 private String fileSeparator; 33 private char charfileSeparator; 34 35 public FileInfoUtil() { 36 String paths = System.getProperty("java.class.path"); 37 String pathSeparator = System.getProperty("path.separator"); 38 fileSeparator = System.getProperty("file.separator"); 39 charfileSeparator = fileSeparator.charAt(0); 40 StringTokenizer to = new StringTokenizer(paths,pathSeparator,false); 41 while (to.hasMoreElements()){ 42 String path = (String )to.nextElement(); 43 if (!path.endsWith(".jar")){ 44 list.add(path); 45 } 46 } 47 } 48 49 public void setPath(String [] paths){ 50 for (int i = 0; i < paths.length; i++) { 51 list.add(paths[i]); 52 } 53 } 54 55 public List getAllFiles(){ 56 Iterator iter = list.iterator(); 57 allClassesList = new ArrayList(); 58 while (iter.hasNext()){ 59 String path = (String )iter.next(); 60 File file = new File(path); 61 addFilesToList(file); 62 } 63 return allClassesList; 64 } 65 66 private void addFilesToList(File file){ 67 if (file.isFile()){ 68 if (file.getName().endsWith(".class")){ 69 allClassesList.add(file); 70 } 71 } else { 72 File [] files = file.listFiles(); 73 for (int i = 0;i < files.length ;i++){ 74 addFilesToList(files[i]); 75 } 76 } 77 } 78 79 87 public File getClassFile(String fullClassName)throws FileNotFoundException{ 88 String paths = System.getProperty("java.class.path"); 89 String partFileName = fullClassName.replace('.',charfileSeparator); 90 String partFile = fileSeparator+partFileName+".class"; 91 Iterator iter = list.iterator(); 92 while (iter.hasNext()){ 93 String testPath = (String )iter.next(); 94 File testFile = new File(testPath+partFile); 95 if (testFile.exists()){ 96 return testFile; 97 } 98 } 99 100 throw new FileNotFoundException("The following class was not found on class path : "+ 101 fullClassName+"\nCommon error is that your class path was not set properly"); 102 } 103 104 112 public File getResourceFile(String resourceFile)throws FileNotFoundException{ 113 114 String partFile = fileSeparator+resourceFile; 115 Iterator iter = list.iterator(); 116 while (iter.hasNext()){ 117 String testPath = (String )iter.next(); 118 File testFile = new File(testPath+partFile); 119 if (testFile.exists()){ 120 return testFile; 121 } 122 } 123 124 125 126 throw new FileNotFoundException("The following resource was not found on class path : "+ 127 resourceFile+"\nCommon error is that your class path was not set properly"); 128 } 129 public boolean classExists(String clazz) throws ClassNotFoundException { 130 JavaClass javaClass = Repository.lookupClass(clazz); 131 if (javaClass != null)return true; 132 return false; 133 134 } 135 136 } 137 | Popular Tags |