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 ExceptionsAttribute extends AttributeInfo { 26 29 public static final String tag = "Exceptions"; 30 31 ExceptionsAttribute(ConstPool cp, int n, DataInputStream in) 32 throws IOException 33 { 34 super(cp, n, in); 35 } 36 37 43 private ExceptionsAttribute(ConstPool cp, ExceptionsAttribute src, 44 Map classnames) { 45 super(cp, tag); 46 copyFrom(src, classnames); 47 } 48 49 54 public ExceptionsAttribute(ConstPool cp) { 55 super(cp, tag); 56 byte[] data = new byte[2]; 57 data[0] = data[1] = 0; this.info = data; 59 } 60 61 69 public AttributeInfo copy(ConstPool newCp, Map classnames) { 70 return new ExceptionsAttribute(newCp, this, classnames); 71 } 72 73 81 private void copyFrom(ExceptionsAttribute srcAttr, Map classnames) { 82 ConstPool srcCp = srcAttr.constPool; 83 ConstPool destCp = this.constPool; 84 byte[] src = srcAttr.info; 85 int num = src.length; 86 byte[] dest = new byte[num]; 87 dest[0] = src[0]; 88 dest[1] = src[1]; for (int i = 2; i < num; i += 2) { 90 int index = ByteArray.readU16bit(src, i); 91 ByteArray.write16bit(srcCp.copy(index, destCp, classnames), 92 dest, i); 93 } 94 95 this.info = dest; 96 } 97 98 101 public int[] getExceptionIndexes() { 102 byte[] blist = info; 103 int n = blist.length; 104 if (n <= 2) 105 return null; 106 107 int[] elist = new int[n / 2 - 1]; 108 int k = 0; 109 for (int j = 2; j < n; j += 2) 110 elist[k++] = ((blist[j] & 0xff) << 8) | (blist[j + 1] & 0xff); 111 112 return elist; 113 } 114 115 118 public String [] getExceptions() { 119 byte[] blist = info; 120 int n = blist.length; 121 if (n <= 2) 122 return null; 123 124 String [] elist = new String [n / 2 - 1]; 125 int k = 0; 126 for (int j = 2; j < n; j += 2) { 127 int index = ((blist[j] & 0xff) << 8) | (blist[j + 1] & 0xff); 128 elist[k++] = constPool.getClassInfo(index); 129 } 130 131 return elist; 132 } 133 134 137 public void setExceptionIndexes(int[] elist) { 138 int n = elist.length; 139 byte[] blist = new byte[n * 2 + 2]; 140 ByteArray.write16bit(n, blist, 0); 141 for (int i = 0; i < n; ++i) 142 ByteArray.write16bit(elist[i], blist, i * 2 + 2); 143 144 info = blist; 145 } 146 147 150 public void setExceptions(String [] elist) { 151 int n = elist.length; 152 byte[] blist = new byte[n * 2 + 2]; 153 ByteArray.write16bit(n, blist, 0); 154 for (int i = 0; i < n; ++i) 155 ByteArray.write16bit(constPool.addClassInfo(elist[i]), 156 blist, i * 2 + 2); 157 158 info = blist; 159 } 160 161 164 public int tableLength() { return info.length / 2 - 1; } 165 166 169 public int getException(int nth) { 170 int index = nth * 2 + 2; return ((info[index] & 0xff) << 8) | (info[index + 1] & 0xff); 172 } 173 } 174 | Popular Tags |