/src/kilim/analysis/Handler.java

http://github.com/kilim/kilim · Java · 99 lines · 56 code · 16 blank · 27 comment · 9 complexity · 39b3e504c8ca0ab66c91c484698a49fc MD5 · raw file

  1. /* Copyright (c) 2006, Sriram Srinivasan
  2. *
  3. * You may distribute this software under the terms of the license
  4. * specified in the file "License"
  5. */
  6. package kilim.analysis;
  7. import static kilim.Constants.THROWABLE_CLASS;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11. /**
  12. * Representation for a catch handler.
  13. */
  14. public class Handler {
  15. /**
  16. * Source offset in method's instruction list
  17. */
  18. public int from;
  19. /**
  20. * End offset in method's instruction list
  21. */
  22. public int to;
  23. /**
  24. * Exception type
  25. */
  26. public String type;
  27. /**
  28. * catch handler's entry point
  29. */
  30. public BasicBlock catchBB;
  31. /** the position of the corresponding entry in the unwoven exception table */
  32. public int pos;
  33. public Handler(int aFrom, int aTo, Handler h) {
  34. this(aFrom,aTo,h.type,h.catchBB,h.pos);
  35. }
  36. public Handler(int aFrom,int aTo,String aType,BasicBlock aCatchBB,int pos) {
  37. from = aFrom;
  38. to = aTo;
  39. if (aType == null) {
  40. // try/finally is compiled with a covering catch handler with
  41. // type null. It is the same as catching Throwable.
  42. aType = THROWABLE_CLASS;
  43. }
  44. type = aType;
  45. catchBB = aCatchBB;
  46. this.pos = pos;
  47. }
  48. private int comparePos(Handler h) {
  49. return from < h.from ? -1 : (from == h.from) ? 0 : 1;
  50. }
  51. public static ArrayList<Handler> consolidate(ArrayList<Handler> list) {
  52. ArrayList<Handler> newList = new ArrayList<Handler>(list.size());
  53. outer:
  54. for (Handler c : list) {
  55. for (Handler h : newList) {
  56. // Two options here. Either h is contiguous with c or it isn't. Contiguous
  57. // means that it has to be the same type and the same catchBB and
  58. // from == to+1
  59. if (c.type.equals(h.type) & c.catchBB==h.catchBB) {
  60. if (h.from==c.to+1) { h.from = c.from; continue outer; }
  61. else if (c.from==h.to+1) { h.to = c.to; continue outer; }
  62. }
  63. }
  64. newList.add(c);
  65. }
  66. Collections.sort(newList,resort);
  67. return newList;
  68. }
  69. private static Resort resort = new Resort();
  70. private static class Resort implements Comparator<Handler> {
  71. public int compare(Handler o1,Handler o2) {
  72. return o1.pos < o2.pos ? -1 : o1.pos==o2.pos ? 0:1;
  73. }
  74. }
  75. /** return a Comparator that orders the handlers by start position */
  76. public static Comparator<Handler> startComparator() { return comp; }
  77. private static Comp comp = new Comp();
  78. private static class Comp implements Comparator<Handler> {
  79. public int compare(Handler o1,Handler o2) {
  80. return o1.comparePos(o2);
  81. }
  82. }
  83. }