1 15 16 package javassist.bytecode; 17 18 import java.io.DataInputStream ; 19 import java.io.IOException ; 20 import java.util.Map ; 21 22 25 public class LineNumberAttribute extends AttributeInfo { 26 29 public static final String tag = "LineNumberTable"; 30 31 LineNumberAttribute(ConstPool cp, int n, DataInputStream in) 32 throws IOException 33 { 34 super(cp, n, in); 35 } 36 37 private LineNumberAttribute(ConstPool cp, byte[] i) { 38 super(cp, tag, i); 39 } 40 41 45 public int tableLength() { 46 return ByteArray.readU16bit(info, 0); 47 } 48 49 56 public int startPc(int i) { 57 return ByteArray.readU16bit(info, i * 4 + 2); 58 } 59 60 67 public int lineNumber(int i) { 68 return ByteArray.readU16bit(info, i * 4 + 4); 69 } 70 71 76 public int toLineNumber(int pc) { 77 int n = tableLength(); 78 int i = 0; 79 for (; i < n; ++i) 80 if (pc < startPc(i)) 81 if (i == 0) 82 return lineNumber(0); 83 else 84 break; 85 86 return lineNumber(i - 1); 87 } 88 89 96 public int toStartPc(int line) { 97 int n = tableLength(); 98 for (int i = 0; i < n; ++i) 99 if (line == lineNumber(i)) 100 return startPc(i); 101 102 return -1; 103 } 104 105 108 static public class Pc { 109 112 public int index; 113 116 public int line; 117 } 118 119 128 public Pc toNearPc(int line) { 129 int n = tableLength(); 130 int nearPc = 0; 131 int distance = 0; 132 if (n > 0) { 133 distance = lineNumber(0) - line; 134 nearPc = startPc(0); 135 } 136 137 for (int i = 1; i < n; ++i) { 138 int d = lineNumber(i) - line; 139 if ((d < 0 && d > distance) 140 || (d >= 0 && (d < distance || distance < 0))) { 141 distance = d; 142 nearPc = startPc(i); 143 } 144 } 145 146 Pc res = new Pc(); 147 res.index = nearPc; 148 res.line = line + distance; 149 return res; 150 } 151 152 158 public AttributeInfo copy(ConstPool newCp, Map classnames) { 159 byte[] src = info; 160 int num = src.length; 161 byte[] dest = new byte[num]; 162 for (int i = 0; i < num; ++i) 163 dest[i] = src[i]; 164 165 LineNumberAttribute attr = new LineNumberAttribute(newCp, dest); 166 return attr; 167 } 168 169 172 void shiftPc(int where, int gapLength, boolean exclusive) { 173 int n = tableLength(); 174 for (int i = 0; i < n; ++i) { 175 int pos = i * 4 + 2; 176 int pc = ByteArray.readU16bit(info, pos); 177 if (pc > where || (exclusive && pc == where)) 178 ByteArray.write16bit(pc + gapLength, info, pos); 179 } 180 } 181 } 182 | Popular Tags |