1 22 package org.aspectj.util; 23 24 import java.io.File ; 25 import java.util.*; 26 27 74 75 public class LineNumberTableMapper { 76 77 private static final char PACKAGE_SEPARATOR = '/'; 78 private static final char FILE_SEPARATOR = ';'; 79 80 87 88 public LineNumberTableMapper(String attrib, String pkg, File root) { 89 int len = attrib.length(); 90 int i = attrib.indexOf(FILE_SEPARATOR); 91 if (i == -1) { 92 files = new File [] { buildFile(root, 93 (pkg == null) 94 ? attrib 95 : pkg + PACKAGE_SEPARATOR + attrib) }; 96 offsets = new int[] { 0 }; 97 } else { 98 List strings = new Vector(); 99 List ints = new Vector(); 100 strings.add(attrib.substring(0, i)); 101 ints.add(new Integer (0)); 102 while (i != -1) { 103 i++; 104 int start = i; 105 i = attrib.indexOf(FILE_SEPARATOR, start); 106 int end = (i == -1) ? len : i; 107 end--; end--; end--; int accumulator = 0; 111 int power = 1; 112 for (char c = attrib.charAt(end); c != '('; end--, c = attrib.charAt(end)) { 113 accumulator += Character.digit(c, 10) * power; 114 power *= 10; 115 } 116 strings.add(attrib.substring(start, end)); 117 ints.add(new Integer (accumulator * 1000)); 118 } 119 int size = strings.size(); 120 files = new File [size]; 121 offsets = new int[size]; 122 for (int j = 0; j < size; j++) { 123 files[j] = buildFile(root, (String ) strings.get(j)); 124 offsets[j] = ((Integer ) ints.get(j)).intValue(); 125 } 126 } 127 } 128 129 139 private static File buildFile(File root, String path) { 140 for (int i = 0, j = path.indexOf(PACKAGE_SEPARATOR); 141 true; 142 i = j + 1, j = path.indexOf(PACKAGE_SEPARATOR, i)) { 143 if (j == -1) { 144 return new File (root, path.substring(i, path.length())); 145 } else { 146 root = new File (root, path.substring(i, j)); 147 } 148 } 149 } 150 151 158 159 public File getCorrespondingFile(int n) { 160 int end = files.length - 1; 161 for (int i = 1; i <= end; i++) { 162 if (n < offsets[i]) return files[i - 1]; 163 } 164 return files[end]; 165 } 166 167 175 public int getCorrespondingLineNumber(int n) { 176 int end = offsets.length - 1; 177 for (int i = 1; i <= end; i++) { 178 if (n < offsets[i]) return n - offsets[i - 1]; 179 } 180 return n - offsets[end]; 181 } 182 183 200 public int getCorrespondingLineNumber(File f, int n) { 201 for (int i = 0, len = files.length; i < len; i++) { 202 if (f.equals(files[i])) { 203 return offsets[i] + n; 204 } 205 } 206 throw new IllegalArgumentException ("No code from " 207 + f 208 + " in this classfile"); 209 } 210 211 214 private File [] files; 215 private int[] offsets; 216 217 220 public String toString() { 221 String s = ""; 222 for (int i = 0; i < files.length - 1; i++) { 223 s += offsets[i] + " " + files[i] + ":"; 224 } 225 s += offsets[files.length - 1] + " " 226 + files[files.length - 1]; 227 return s; 228 } 229 230 public static void main(String [] args) { 231 System.err.println(new LineNumberTableMapper(args[0], "", new File ("."))); 232 } 233 } 234 | Popular Tags |