KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > finders > SwitchNode


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.toolkits.base.finders;
21
22 import java.util.*;
23 import soot.util.*;
24 import soot.dava.internal.asg.*;
25
26 public class SwitchNode implements Comparable JavaDoc
27 {
28     private LinkedList preds, succs;
29     private AugmentedStmt as;
30     private int score;
31     private TreeSet indexSet;
32     private IterableSet body;
33
34     public SwitchNode( AugmentedStmt as, TreeSet indexSet, IterableSet body)
35     {
36     this.as = as;
37     this.indexSet = indexSet;
38     this.body = body;
39
40     preds = new LinkedList();
41     succs = new LinkedList();
42
43     score = -1;
44     }
45
46     public int get_Score()
47     {
48     if (score == -1) {
49         score = 0;
50
51         if (preds.size() < 2) {
52
53         Iterator sit = succs.iterator();
54         while (sit.hasNext()) {
55             SwitchNode ssn = (SwitchNode) sit.next();
56
57             int curScore = ssn.get_Score();
58             if (score < curScore)
59             score = curScore;
60         }
61
62         score++;
63         }
64     }
65
66     return score;
67     }
68
69     public List get_Preds()
70     {
71     return preds;
72     }
73
74     public List get_Succs()
75     {
76     return succs;
77     }
78
79     public AugmentedStmt get_AugStmt()
80     {
81     return as;
82     }
83
84     public TreeSet get_IndexSet()
85     {
86     return indexSet;
87     }
88
89     public IterableSet get_Body()
90     {
91     return body;
92     }
93
94     public SwitchNode reset()
95     {
96     preds.clear();
97     succs.clear();
98
99     return this;
100     }
101
102     public void setup_Graph( HashMap binding)
103     {
104     Iterator rit = ((AugmentedStmt) as.bsuccs.get(0)).get_Reachers().iterator();
105     while (rit.hasNext()) {
106         SwitchNode pred = (SwitchNode) binding.get( rit.next());
107         
108         if (pred != null) {
109         if (preds.contains( pred) == false)
110             preds.add( pred);
111         
112         if (pred.succs.contains( this) == false)
113             pred.succs.add( this);
114         }
115     }
116     }
117
118
119     /*
120      * Can compare to an Integer, a String, a set of Indices, and another SwitchNode.
121      */

122
123     public int compareTo( Object JavaDoc o)
124     {
125     if (o == this)
126         return 0;
127
128     if (indexSet.last() instanceof String JavaDoc)
129         return 1;
130
131     if (o instanceof String JavaDoc)
132         return -1;
133
134     if (o instanceof Integer JavaDoc)
135         return ((Integer JavaDoc) indexSet.last()).intValue() - ((Integer JavaDoc) o).intValue();
136
137     if (o instanceof TreeSet) {
138         TreeSet other = (TreeSet) o;
139
140         if (other.last() instanceof String JavaDoc)
141         return -1;
142
143         return ((Integer JavaDoc) indexSet.last()).intValue() - ((Integer JavaDoc) other.last()).intValue();
144     }
145     
146     SwitchNode other = (SwitchNode) o;
147
148     if (other.indexSet.last() instanceof String JavaDoc)
149         return -1;
150
151     return ((Integer JavaDoc) indexSet.last()).intValue() - ((Integer JavaDoc) other.indexSet.last()).intValue();
152     }
153 }
154
155
Popular Tags