KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > AbstractUnit


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30 package soot;
31
32 import soot.tagkit.*;
33 import soot.*;
34 import soot.util.*;
35 import java.util.*;
36
37 /** Provides default implementations for the methods in Unit. */
38 public abstract class AbstractUnit extends AbstractHost implements Unit
39 {
40
41     /** Returns a deep clone of this object. */
42     public abstract Object JavaDoc clone();
43     
44     /** Returns a list of Boxes containing Values used in this Unit.
45      * The list of boxes is dynamically updated as the structure changes.
46      * Note that they are returned in usual evaluation order.
47      * (this is important for aggregation)
48      */

49     public List getUseBoxes()
50     {
51         return emptyList;
52     }
53
54     /** Returns a list of Boxes containing Values defined in this Unit.
55      * The list of boxes is dynamically updated as the structure changes.
56      */

57     public List getDefBoxes()
58     {
59         return emptyList;
60     }
61
62
63     /** Returns a list of Boxes containing Units defined in this Unit; typically
64      * branch targets.
65      * The list of boxes is dynamically updated as the structure changes.
66      */

67     public List getUnitBoxes()
68     {
69         return emptyList;
70     }
71
72     /** Canonical AbstractUnit.emptyList list. */
73     static final public List emptyList = Collections.EMPTY_LIST;
74
75     /** List of UnitBoxes pointing to this Unit. */
76     List boxesPointingToThis = null;
77
78     /** List of ValueBoxes contained in this Unit. */
79     List valueBoxes = null;
80
81     /** Returns a list of Boxes pointing to this Unit. */
82     public List getBoxesPointingToThis()
83     {
84         if( boxesPointingToThis == null ) return emptyList;
85         return Collections.unmodifiableList( boxesPointingToThis );
86     }
87
88     public void addBoxPointingToThis( UnitBox b ) {
89         if( boxesPointingToThis == null ) boxesPointingToThis = new ArrayList();
90         boxesPointingToThis.add( b );
91     }
92
93     public void removeBoxPointingToThis( UnitBox b ) {
94         if( boxesPointingToThis != null ) boxesPointingToThis.remove( b );
95     }
96
97     public void clearUnitBoxes() {
98         for( Iterator it = getUnitBoxes().iterator(); it.hasNext(); ) {
99             UnitBox ub = (UnitBox) it.next();
100             ub.setUnit(null);
101         }
102     }
103     
104     /** Returns a list of ValueBoxes, either used or defined in this Unit. */
105     public List getUseAndDefBoxes()
106     {
107         List useBoxes = getUseBoxes();
108         List defBoxes = getDefBoxes();
109         if( useBoxes.isEmpty() ) {
110             if( defBoxes.isEmpty() ) {
111                 return emptyList;
112             } else {
113                 return Collections.unmodifiableList(defBoxes);
114             }
115         } else {
116             if( defBoxes.isEmpty() ) {
117                 return Collections.unmodifiableList(useBoxes);
118             } else {
119                 valueBoxes = new ArrayList();
120
121                 valueBoxes.addAll(defBoxes);
122                 valueBoxes.addAll(useBoxes);
123
124                 valueBoxes = Collections.unmodifiableList(valueBoxes);
125
126                 return valueBoxes;
127             }
128         }
129     }
130
131     /** Used to implement the Switchable construct. */
132     public void apply(Switch sw)
133     {
134     }
135
136     public void redirectJumpsToThisTo(Unit newLocation)
137     {
138         List boxesPointing = this.getBoxesPointingToThis();
139
140         Object JavaDoc[] boxes = boxesPointing.toArray();
141         // important to change this to an array to have a static copy
142

143         for(int i = 0; i < boxes.length; i++)
144         {
145             UnitBox box = (UnitBox) boxes[i];
146
147             if(box.getUnit() != this)
148                 throw new RuntimeException JavaDoc("Something weird's happening");
149
150             if(box.isBranchTarget())
151                 box.setUnit(newLocation);
152         }
153
154     }
155 }
156
Popular Tags