1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import org.apache.bcel.Constants; 23 24 34 public final class LineNumberTable extends Attribute { 35 36 private int line_number_table_length; 37 private LineNumber[] line_number_table; 39 40 44 public LineNumberTable(LineNumberTable c) { 45 this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), c.getConstantPool()); 46 } 47 48 49 55 public LineNumberTable(int name_index, int length, LineNumber[] line_number_table, 56 ConstantPool constant_pool) { 57 super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool); 58 setLineNumberTable(line_number_table); 59 } 60 61 62 70 LineNumberTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool) 71 throws IOException { 72 this(name_index, length, (LineNumber[]) null, constant_pool); 73 line_number_table_length = (file.readUnsignedShort()); 74 line_number_table = new LineNumber[line_number_table_length]; 75 for (int i = 0; i < line_number_table_length; i++) { 76 line_number_table[i] = new LineNumber(file); 77 } 78 } 79 80 81 88 public void accept( Visitor v ) { 89 v.visitLineNumberTable(this); 90 } 91 92 93 99 public final void dump( DataOutputStream file ) throws IOException { 100 super.dump(file); 101 file.writeShort(line_number_table_length); 102 for (int i = 0; i < line_number_table_length; i++) { 103 line_number_table[i].dump(file); 104 } 105 } 106 107 108 111 public final LineNumber[] getLineNumberTable() { 112 return line_number_table; 113 } 114 115 116 119 public final void setLineNumberTable( LineNumber[] line_number_table ) { 120 this.line_number_table = line_number_table; 121 line_number_table_length = (line_number_table == null) ? 0 : line_number_table.length; 122 } 123 124 125 128 public final String toString() { 129 StringBuffer buf = new StringBuffer (); 130 StringBuffer line = new StringBuffer (); 131 String newLine = System.getProperty("line.separator", "\n"); 132 for (int i = 0; i < line_number_table_length; i++) { 133 line.append(line_number_table[i].toString()); 134 if (i < line_number_table_length - 1) { 135 line.append(", "); 136 } 137 if (line.length() > 72) { 138 line.append(newLine); 139 buf.append(line.toString()); 140 line.setLength(0); 141 } 142 } 143 buf.append(line); 144 return buf.toString(); 145 } 146 147 148 154 public int getSourceLine( int pos ) { 155 int l = 0, r = line_number_table_length - 1; 156 if (r < 0) { 157 return -1; 158 } 159 int min_index = -1, min = -1; 160 162 do { 163 int i = (l + r) / 2; 164 int j = line_number_table[i].getStartPC(); 165 if (j == pos) { 166 return line_number_table[i].getLineNumber(); 167 } else if (pos < j) { 168 r = i - 1; 169 } else { 170 l = i + 1; 171 } 172 176 if (j < pos && j > min) { 177 min = j; 178 min_index = i; 179 } 180 } while (l <= r); 181 184 if (min_index < 0) { 185 return -1; 186 } 187 return line_number_table[min_index].getLineNumber(); 188 } 189 190 191 194 public Attribute copy( ConstantPool _constant_pool ) { 195 LineNumberTable c = (LineNumberTable) clone(); 196 c.line_number_table = new LineNumber[line_number_table_length]; 197 for (int i = 0; i < line_number_table_length; i++) { 198 c.line_number_table[i] = line_number_table[i].copy(); 199 } 200 c.constant_pool = _constant_pool; 201 return c; 202 } 203 204 205 public final int getTableLength() { 206 return line_number_table_length; 207 } 208 } 209 | Popular Tags |