PageRenderTime 29ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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