1 16 17 package org.cojen.classfile; 18 19 import java.io.DataInput ; 20 import java.io.DataOutput ; 21 import java.io.IOException ; 22 import org.cojen.classfile.constant.ConstantClassInfo; 23 24 30 public class ExceptionHandler implements LocationRange { 31 private Location mStart; 32 private Location mEnd; 33 private Location mCatch; 34 private ConstantClassInfo mCatchType; 35 36 42 public ExceptionHandler(Location startLocation, 43 Location endLocation, 44 Location catchLocation, 45 ConstantClassInfo catchType) { 46 mStart = startLocation; 47 mEnd = endLocation; 48 mCatch = catchLocation; 49 mCatchType = catchType; 50 } 51 52 public Location getStartLocation() { 53 return mStart; 54 } 55 56 public Location getEndLocation() { 57 return mEnd; 58 } 59 60 public Location getCatchLocation() { 61 return mCatch; 62 } 63 64 67 public ConstantClassInfo getCatchType() { 68 return mCatchType; 69 } 70 71 public void writeTo(DataOutput dout) throws IOException { 72 int start_pc = getStartLocation().getLocation(); 73 int end_pc = getEndLocation().getLocation(); 74 int handler_pc = getCatchLocation().getLocation(); 75 int catch_type; 76 ConstantClassInfo catchType = getCatchType(); 77 if (catchType == null) { 78 catch_type = 0; 79 } 80 else { 81 catch_type = catchType.getIndex(); 82 } 83 84 check("exception start PC", start_pc); 85 check("exception end PC", end_pc); 86 check("exception handler PC", handler_pc); 87 88 dout.writeShort(start_pc); 89 dout.writeShort(end_pc); 90 dout.writeShort(handler_pc); 91 dout.writeShort(catch_type); 92 } 93 94 private void check(String type, int addr) throws IllegalStateException { 95 if (addr < 0 || addr > 65535) { 96 throw new IllegalStateException ("Value for " + type + " out of " + 97 "valid range: " + addr); 98 99 } 100 } 101 102 public static ExceptionHandler readFrom(ConstantPool cp, 103 DataInput din) throws IOException { 104 int start_pc = din.readUnsignedShort(); 105 int end_pc = din.readUnsignedShort(); 106 int handler_pc = din.readUnsignedShort(); 107 int catch_type = din.readUnsignedShort(); 108 109 ConstantClassInfo catchTypeConstant; 110 if (catch_type == 0) { 111 catchTypeConstant = null; 112 } else { 113 catchTypeConstant = (ConstantClassInfo)cp.getConstant(catch_type); 114 } 115 116 return new ExceptionHandler(new FixedLocation(start_pc), 117 new FixedLocation(end_pc), 118 new FixedLocation(handler_pc), 119 catchTypeConstant); 120 } 121 } 122 | Popular Tags |