1 package alt.jiapi.file; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.DataInputStream ; 5 import java.io.DataOutputStream ; 6 import java.io.IOException ; 7 import java.util.List ; 8 import java.util.LinkedList ; 9 import java.util.Iterator ; 10 11 17 public class LocalVariableTableAttribute extends Attribute { 18 public static final String ATTRIBUTE_NAME = "LocalVariableTable"; 19 private List localVariables = new LinkedList (); 20 21 public LocalVariableTableAttribute(ConstantPool cp) { 22 super(cp.addUtf8Info(ATTRIBUTE_NAME).getEntryIndex()); 23 setConstantPool(cp); 24 } 25 26 LocalVariableTableAttribute(short nameIndex, DataInputStream dis) throws IOException { 27 super(nameIndex); 28 short l = dis.readShort(); 29 for(int i = 0; i < l; i++) { 30 short start_pc = dis.readShort(); 31 short length = dis.readShort(); 32 short name_index = dis.readShort(); 33 short descriptor_index = dis.readShort(); 34 short index = dis.readShort(); 35 36 localVariables.add(new LocalVariable(start_pc, length, name_index, 37 descriptor_index, index)); 38 } 39 } 40 41 public List getLocalVariables() { 42 return localVariables; 43 } 44 45 46 public byte[] getBytes() { 47 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 48 DataOutputStream dos = new DataOutputStream (baos); 49 50 try { 51 dos.writeShort(localVariables.size()); 52 53 Iterator i = localVariables.iterator(); 54 while(i.hasNext()) { 55 LocalVariable lvte=(LocalVariable)i.next(); 56 dos.writeShort(lvte.getStartPc()); 57 dos.writeShort(lvte.getLength()); 58 dos.writeShort(lvte.getNameIndex()); 59 dos.writeShort(lvte.getDescriptorIndex()); 60 dos.writeShort(lvte.getIndex()); 61 } 62 } 63 catch(IOException ioe) { 64 throw new RuntimeException (ioe); 66 } 67 68 return baos.toByteArray(); 69 } 70 71 72 public class LocalVariable { 73 private short start_pc; 74 private short length; 75 private short name_index; 76 private short descriptor_index; 77 private short index; 78 79 LocalVariable(short start_pc, short length, short name_index, 80 short descriptor_index, short index) { 81 this.start_pc = start_pc; 82 this.length = length; 83 this.name_index = name_index; 84 this.descriptor_index = descriptor_index; 85 this.index = index; 86 } 87 88 public short getStartPc() { 89 return start_pc; 90 } 91 92 public short getLength() { 93 return length; 94 } 95 96 public String getName() { 97 return cp.getUtf8(name_index); 98 } 99 100 short getNameIndex() { 101 return name_index; 102 } 103 104 public String getDescriptor() { 105 return cp.getUtf8(descriptor_index); 106 } 107 108 short getDescriptorIndex() { 109 return descriptor_index; 110 } 111 112 public short getIndex() { 113 return index; 114 } 115 } 116 } 117 118 | Popular Tags |