KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > internal > AbstractNewArrayExpr


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam
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 package soot.jimple.internal;
28
29 import soot.tagkit.*;
30 import soot.*;
31 import soot.jimple.*;
32 import soot.baf.*;
33 import soot.jimple.*;
34 import soot.util.*;
35 import java.util.*;
36
37 public abstract class AbstractNewArrayExpr implements NewArrayExpr, ConvertToBaf
38 {
39     Type baseType;
40     ValueBox sizeBox;
41
42     protected AbstractNewArrayExpr(Type type, ValueBox sizeBox)
43     {
44       this.baseType = type; this.sizeBox = sizeBox;
45     }
46
47     public boolean equivTo(Object JavaDoc o)
48     {
49         if (o instanceof AbstractNewArrayExpr)
50         {
51             AbstractNewArrayExpr ae = (AbstractNewArrayExpr)o;
52             return sizeBox.getValue().equivTo(ae.sizeBox.getValue()) &&
53                 baseType.equals(ae.baseType);
54         }
55         return false;
56     }
57
58     /** Returns a hash code for this object, consistent with structural equality. */
59     public int equivHashCode()
60     {
61         return sizeBox.getValue().equivHashCode() * 101 + baseType.hashCode() * 17;
62     }
63
64     public abstract Object JavaDoc clone();
65     
66     public String JavaDoc toString()
67     {
68         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
69
70         buffer.append(Jimple.v().NEWARRAY + " (" + getBaseTypeString() + ")");
71         buffer.append("[" + sizeBox.getValue().toString() + "]");
72
73         return buffer.toString();
74     }
75     
76     public void toString(UnitPrinter up) {
77         up.literal(Jimple.v().NEWARRAY);
78         up.literal(" ");
79         up.literal("(");
80         up.type(baseType);
81         up.literal(")");
82         up.literal("[");
83         sizeBox.toString(up);
84         up.literal("]");
85     }
86     
87     private String JavaDoc getBaseTypeString()
88     {
89     return baseType.toString();
90     }
91
92     public Type getBaseType()
93     {
94         return baseType;
95     }
96
97     public void setBaseType(Type type)
98     {
99         baseType = type;
100     }
101
102     public ValueBox getSizeBox()
103     {
104         return sizeBox;
105     }
106
107     public Value getSize()
108     {
109         return sizeBox.getValue();
110     }
111
112     public void setSize(Value size)
113     {
114         sizeBox.setValue(size);
115     }
116
117     public List getUseBoxes()
118     {
119         List useBoxes = new ArrayList();
120
121         useBoxes.addAll(sizeBox.getValue().getUseBoxes());
122         useBoxes.add(sizeBox);
123
124         return useBoxes;
125     }
126
127
128     public Type getType()
129     {
130         if(baseType instanceof ArrayType)
131             return ArrayType.v(((ArrayType) baseType).baseType, ((ArrayType) baseType).numDimensions + 1);
132         else
133             return ArrayType.v(baseType, 1);
134     }
135
136     public void apply(Switch sw)
137     {
138         ((ExprSwitch) sw).caseNewArrayExpr(this);
139     }
140
141     public void convertToBaf(JimpleToBafContext context, List out)
142     {
143        ((ConvertToBaf)(getSize())).convertToBaf(context, out);
144        
145
146        Unit u;
147        out.add(u = Baf.v().newNewArrayInst(getBaseType()));
148     
149     Unit currentUnit = context.getCurrentUnit();
150
151     Iterator it = currentUnit.getTags().iterator();
152     while(it.hasNext()) {
153         u.addTag((Tag) it.next());
154     }
155     
156     }
157 }
158
Popular Tags