00001 package gov.nasa.arc.ase.jpf.jvm; 00002 00003 import de.fub.bytecode.classfile.ClassParser; 00004 import de.fub.bytecode.classfile.JavaClass; 00005 import de.fub.bytecode.Repository; 00006 import gov.nasa.arc.ase.jpf.*; 00007 import gov.nasa.arc.ase.util.Debug; 00008 import java.io.*; 00009 import java.util.*; 00010 00011 public class Source { 00012 private static Hashtable ht = new Hashtable(); 00013 private static String[] classPath = null; 00014 00015 private List program = new ArrayList(); 00016 private String name; 00017 00018 private Source() { 00019 program = null; 00020 name = null; 00021 } 00022 private Source(String filename, String name) { 00023 this.name = name; 00024 00025 try{ 00026 String line; 00027 00028 BufferedReader in = new BufferedReader(new FileReader(filename)); 00029 line = in.readLine(); 00030 while(line != null){ 00031 program.add(line); 00032 line = in.readLine(); 00033 }; 00034 in.close(); 00035 } catch(IOException e) { 00036 Debug.println(Debug.WARNING, "warning: file " + filename + " not found"); 00037 program = null; 00038 } 00039 } 00040 public void dump2file(BufferedWriter file, int thread, int lineno) 00041 throws IOException { 00042 file.write(thread + " "); 00043 if(name != null) 00044 file.write(name); 00045 else 00046 file.write("<unknown>"); 00047 file.write(" " + lineno + "\n"); 00048 } 00049 public static boolean exists(String filename) { 00050 try{ 00051 new FileReader(filename); 00052 return true; 00053 } catch(FileNotFoundException e) { 00054 return false; 00055 } 00056 } 00057 public String getLine(int thread,int lineno) { 00058 StringBuffer sb = new StringBuffer(); 00059 00060 sb.append("[" + thread + "] "); 00061 00062 if(name != null) 00063 sb.append(name); 00064 else 00065 sb.append("<unknown>"); 00066 00067 sb.append("." + lineno + ": "); 00068 if(program == null) 00069 sb.append("<source file missing>"); 00070 else if(lineno <= 0 || lineno > program.size()) 00071 sb.append("<source line missing>"); 00072 else 00073 sb.append(program.get(lineno-1).toString()); 00074 00075 return sb.toString(); 00076 } 00077 public static Source getSource(String classname) { 00078 String filename = getSourceFileName(classname); 00079 00080 if(ht.containsKey(filename)) 00081 return (Source)ht.get(filename); 00082 else { 00083 Source s = new Source(filename, getSourceName(classname)); 00084 ht.put(filename, s); 00085 00086 return s; 00087 } 00088 } 00089 public static String getSourceFileName(String classname) { 00090 JavaClass clazz; 00091 00092 if(classPath == null) setClassPath(); 00093 00094 if((clazz = Repository.lookupClass(classname)) == null) 00095 try { 00096 clazz = new ClassParser(classname).parse(); 00097 } catch(IOException e) { 00098 Debug.println(Debug.WARNING, "can't load class " + classname); 00099 throw new IllegalArgumentException(classname); 00100 } 00101 00102 String source = clazz.getSourceFileName(); 00103 String filename = clazz.getPackageName().replace('.', '/') + "/" + source; 00104 00105 for(int i = 0; i < classPath.length; i++) 00106 if(exists(classPath[i] + "/" + filename)) 00107 return classPath[i] + "/" + filename; 00108 00109 return "./" + filename; 00110 } 00111 public static String getSourceName(String classname) { 00112 JavaClass clazz; 00113 00114 if(classPath == null) setClassPath(); 00115 00116 if((clazz = Repository.lookupClass(classname)) == null) 00117 try { 00118 clazz = new ClassParser(classname).parse(); 00119 } catch(IOException e) { 00120 Debug.println(Debug.WARNING, "can't load class " + classname); 00121 throw new IllegalArgumentException(classname); 00122 } 00123 00124 return clazz.getSourceFileName(); 00125 } 00126 public void print() { 00127 if(name != null) 00128 Debug.println(Debug.WARNING, name + ":"); 00129 if(program == null) 00130 Debug.println(Debug.WARNING, "<source file missing>"); 00131 else 00132 for (int i = 0;i < program.size();i++) 00133 Debug.println(Debug.WARNING, (i+1) + ": " + program.get(i).toString()); 00134 } 00135 public void print(int thread,int lineno){ 00136 Debug.print(Debug.WARNING, "[" + thread + "] "); 00137 00138 if(name != null) 00139 Debug.print(Debug.WARNING, name); 00140 else 00141 Debug.print(Debug.WARNING, "<unknown>"); 00142 00143 Debug.print(Debug.WARNING, "." + lineno + ": "); 00144 if(program == null) 00145 Debug.println(Debug.WARNING, "<source file missing>"); 00146 else if(lineno <= 0 || lineno > program.size()) 00147 Debug.println(Debug.WARNING, "<source line missing>"); 00148 else 00149 Debug.println(Debug.WARNING, program.get(lineno-1).toString()); 00150 } 00151 public static void printSources() { 00152 for(Enumeration e = ht.elements(); e.hasMoreElements(); ) 00153 ((Source)e.nextElement()).print(); 00154 } 00155 private static void setClassPath() { 00156 StringTokenizer st = new StringTokenizer(System.getProperty("java.class.path"), ":"); 00157 classPath = new String[st.countTokens()]; 00158 for(int i = 0; st.hasMoreTokens(); i++) 00159 classPath[i] = st.nextToken(); 00160 } 00161 }