1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import java.io.*; 59 60 70 public final class LineNumberTable extends Attribute { 71 private int line_number_table_length; 72 private LineNumber[] line_number_table; 74 78 public LineNumberTable(LineNumberTable c) { 79 this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), 80 c.getConstantPool()); 81 } 82 83 89 public LineNumberTable(int name_index, int length, 90 LineNumber[] line_number_table, 91 ConstantPool constant_pool) 92 { 93 super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool); 94 setLineNumberTable(line_number_table); 95 } 96 97 105 LineNumberTable(int name_index, int length, DataInputStream file, 106 ConstantPool constant_pool) throws IOException 107 { 108 this(name_index, length, (LineNumber[])null, constant_pool); 109 line_number_table_length = (file.readUnsignedShort()); 110 line_number_table = new LineNumber[line_number_table_length]; 111 112 for(int i=0; i < line_number_table_length; i++) 113 line_number_table[i] = new LineNumber(file); 114 } 115 122 public void accept(Visitor v) { 123 v.visitLineNumberTable(this); 124 } 125 131 public final void dump(DataOutputStream file) throws IOException 132 { 133 super.dump(file); 134 file.writeShort(line_number_table_length); 135 for(int i=0; i < line_number_table_length; i++) 136 line_number_table[i].dump(file); 137 } 138 139 142 public final LineNumber[] getLineNumberTable() { return line_number_table; } 143 144 147 public final void setLineNumberTable(LineNumber[] line_number_table) { 148 this.line_number_table = line_number_table; 149 150 line_number_table_length = (line_number_table == null)? 0 : 151 line_number_table.length; 152 } 153 154 157 public final String toString() { 158 StringBuffer buf = new StringBuffer (); 159 StringBuffer line = new StringBuffer (); 160 161 for(int i=0; i < line_number_table_length; i++) { 162 line.append(line_number_table[i].toString()); 163 164 if(i < line_number_table_length - 1) 165 line.append(", "); 166 167 if(line.length() > 72) { 168 line.append('\n'); 169 buf.append(line); 170 line.setLength(0); 171 } 172 } 173 174 buf.append(line); 175 176 return buf.toString(); 177 } 178 179 185 public int getSourceLine(int pos) { 186 int l = 0, r = line_number_table_length-1; 187 188 if(r < 0) return -1; 190 191 int min_index = -1, min=-1; 192 193 195 do { 196 int i = (l + r) / 2; 197 int j = line_number_table[i].getStartPC(); 198 199 if(j == pos) 200 return line_number_table[i].getLineNumber(); 201 else if(pos < j) r = i - 1; 203 else l = i + 1; 205 206 210 if(j < pos && j > min) { 211 min = j; 212 min_index = i; 213 } 214 } while(l <= r); 215 216 return line_number_table[min_index].getLineNumber(); 217 } 218 219 222 public Attribute copy(ConstantPool constant_pool) { 223 LineNumberTable c = (LineNumberTable)clone(); 224 225 c.line_number_table = new LineNumber[line_number_table_length]; 226 for(int i=0; i < line_number_table_length; i++) 227 c.line_number_table[i] = line_number_table[i].copy(); 228 229 c.constant_pool = constant_pool; 230 return c; 231 } 232 233 public final int getTableLength() { return line_number_table_length; } 234 } 235 | Popular Tags |