OSDN Git Service

752cc8eca21561eac50642d8e91d543626809ccd
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / jtools / js / tinyvm / ExceptionRecord.java
1 package js.tinyvm;
2
3 import java.io.IOException;
4
5 import js.tinyvm.io.IByteWriter;
6 import js.tinyvm.io.IOUtilities;
7
8 import org.apache.bcel.classfile.CodeException;
9 import org.apache.bcel.classfile.ConstantClass;
10 import org.apache.bcel.classfile.JavaClass;
11
12 public class ExceptionRecord implements WritableData
13 {
14    CodeException iExcep;
15    int iClassIndex;
16
17    public ExceptionRecord (CodeException aExcep, Binary aBinary, JavaClass aCF)
18       throws Exception
19    {
20       iExcep = aExcep;
21       int pCPIndex = aExcep.getCatchType();
22       if (pCPIndex == 0)
23       {
24          // An index of 0 means ANY.
25          iClassIndex = aBinary.getClassIndex("java/lang/Throwable");
26       }
27       else
28       {
29          ConstantClass pCls = (ConstantClass) aCF.getConstantPool()
30             .getConstant(pCPIndex);
31          String pName = pCls.getBytes(aCF.getConstantPool());
32          iClassIndex = aBinary.getClassIndex(pName);
33       }
34       if (iClassIndex == -1)
35       {
36          throw new TinyVMException("Exception not found: " + iExcep);
37       }
38    }
39
40    public int getLength ()
41    {
42       return IOUtilities.adjustedSize(2 + // start
43          2 + // end
44          2 + // handler
45          1, // class index
46          2);
47    }
48
49    public void dump (IByteWriter aOut) throws TinyVMException
50    {
51       int pStart = iExcep.getStartPC();
52       int pEnd = iExcep.getEndPC();
53       int pHandler = iExcep.getHandlerPC();
54       if (pStart > TinyVMConstants.MAX_CODE || pEnd > TinyVMConstants.MAX_CODE
55          || pHandler > TinyVMConstants.MAX_CODE)
56       {
57          throw new TinyVMException("Exception handler with huge PCs");
58       }
59
60       try
61       {
62          aOut.writeU2(pStart);
63          aOut.writeU2(pEnd);
64          aOut.writeU2(pHandler);
65          aOut.writeU1(iClassIndex);
66          IOUtilities.writePadding(aOut, 2);
67       }
68       catch (IOException e)
69       {
70          throw new TinyVMException(e.getMessage(), e);
71       }
72    }
73
74    public boolean equals (Object aOther)
75    {
76       if (!(aOther instanceof ExceptionRecord))
77          return false;
78       return ((ExceptionRecord) aOther).iExcep.equals(iExcep);
79    }
80
81    public int hashCode ()
82    {
83       return iExcep.hashCode();
84    }
85 }
86