ExceptionsAttr.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package gnu.bytecode;
  2. import java.io.*;
  3. /**
  4. * Represents the contents of a standard "Exceptions" attribute.
  5. * @author Geoff Berry
  6. */
  7. public class ExceptionsAttr extends Attribute
  8. {
  9. // The exception types.
  10. ClassType[] exceptions;
  11. // The exception table.
  12. short[] exception_table;
  13. /** Add a new ExceptionsAttr to a Method. */
  14. public ExceptionsAttr(Method meth)
  15. {
  16. super("Exceptions");
  17. addToFrontOf(meth);
  18. }
  19. /** Set the Exceptions attribute to refer to classes whose indices
  20. in the constant pool of `cl' are given by `indices'. */
  21. public void setExceptions (short[] indices, ClassType cl)
  22. {
  23. exception_table = indices;
  24. exceptions = new ClassType[indices.length];
  25. ConstantPool cp = cl.getConstants ();
  26. for (int i = indices.length - 1; i >= 0; -- i)
  27. exceptions[i] =
  28. (ClassType)((CpoolClass)cp.getPoolEntry(indices[i])).getClassType ();
  29. }
  30. /** Set the Exceptions attribute to refer to the given exception types.
  31. * @param excep_types the types of the exceptions. */
  32. public void setExceptions (ClassType[] excep_types)
  33. {
  34. exceptions = excep_types;
  35. }
  36. public void assignConstants (ClassType cl)
  37. {
  38. super.assignConstants(cl);
  39. ConstantPool cp = cl.getConstants();
  40. int count = exceptions.length;
  41. exception_table = new short[ count ];
  42. for (int i = count - 1; i >= 0; --i)
  43. {
  44. exception_table[i] = (short)cp.addClass(exceptions[i]).index;
  45. }
  46. }
  47. /** The size of this Attribute (in bytes) is 2 (for
  48. number_of_exception) plus 2 * number_of_exceptions. */
  49. public final int getLength()
  50. {
  51. return 2 + 2 * (exceptions == null ? 0 : exceptions.length);
  52. }
  53. /** The types of the exceptions in this attr. */
  54. public final ClassType[] getExceptions()
  55. {
  56. return exceptions;
  57. }
  58. public void write (DataOutputStream dstr) throws java.io.IOException
  59. {
  60. int count = exceptions.length;
  61. dstr.writeShort(count);
  62. for (int i = 0; i < count; i++)
  63. {
  64. dstr.writeShort(exception_table[i]);
  65. }
  66. }
  67. public void print (ClassTypeWriter dst)
  68. {
  69. dst.print("Attribute \"");
  70. dst.print(getName());
  71. dst.print("\", length:");
  72. dst.print(getLength());
  73. dst.print(", count: ");
  74. int count = exceptions.length;
  75. dst.println(count);
  76. for (int i = 0; i < count; i++)
  77. {
  78. int catch_type_index = exception_table[i] & 0xffff;
  79. dst.print(" ");
  80. dst.printOptionalIndex(catch_type_index);
  81. dst.printConstantTersely(catch_type_index, ConstantPool.CLASS);
  82. dst.println();
  83. }
  84. }
  85. }