KickJava   Java API By Example, From Geeks To Geeks.

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


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

20
21 /*
22  * Modified by the Sable Research Group and others 1997-1999.
23  * See the 'credits' file distributed with Soot for the complete list of
24  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
25  */

26
27
28 package soot.jimple.internal;
29
30 import soot.*;
31 import soot.tagkit.*;
32 import soot.baf.*;
33 import soot.jimple.*;
34 import soot.util.*;
35 import java.util.*;
36 import java.io.*;
37 import soot.grimp.PrecedenceTest;
38
39 public abstract class AbstractInstanceFieldRef implements InstanceFieldRef, ConvertToBaf
40 {
41     protected SootFieldRef fieldRef;
42     ValueBox baseBox;
43
44     protected AbstractInstanceFieldRef(ValueBox baseBox, SootFieldRef fieldRef)
45     {
46         if( fieldRef.isStatic() ) throw new RuntimeException JavaDoc("wrong static-ness");
47         this.baseBox = baseBox;
48         this.fieldRef = fieldRef;
49     }
50
51     public abstract Object JavaDoc clone();
52
53     public String JavaDoc toString()
54     {
55         return baseBox.getValue().toString() + "." + fieldRef.getSignature();
56     }
57     
58     public void toString( UnitPrinter up ) {
59         if( PrecedenceTest.needsBrackets( baseBox, this ) ) up.literal("(");
60         baseBox.toString(up);
61         if( PrecedenceTest.needsBrackets( baseBox, this ) ) up.literal(")");
62         up.literal(".");
63         up.fieldRef(fieldRef);
64     }
65
66     public Value getBase()
67     {
68         return baseBox.getValue();
69     }
70
71     public ValueBox getBaseBox()
72     {
73         return baseBox;
74     }
75
76     public void setBase(Value base)
77     {
78         baseBox.setValue(base);
79     }
80
81     public SootFieldRef getFieldRef()
82     {
83         return fieldRef;
84     }
85     public void setFieldRef(SootFieldRef fieldRef) {
86         this.fieldRef = fieldRef;
87     }
88
89     public SootField getField()
90     {
91         return fieldRef.resolve();
92     }
93
94     public List getUseBoxes()
95     {
96         List useBoxes = new ArrayList();
97
98         useBoxes.addAll(baseBox.getValue().getUseBoxes());
99         useBoxes.add(baseBox);
100
101         return useBoxes;
102     }
103
104     public Type getType()
105     {
106         return fieldRef.type();
107     }
108
109     public void apply(Switch sw)
110     {
111         ((RefSwitch) sw).caseInstanceFieldRef(this);
112     }
113     
114     public boolean equivTo(Object JavaDoc o)
115     {
116         if (o instanceof AbstractInstanceFieldRef)
117         {
118             AbstractInstanceFieldRef fr = (AbstractInstanceFieldRef)o;
119             return fr.getField().equals(getField()) &&
120                 fr.baseBox.getValue().equivTo(baseBox.getValue());
121         }
122         return false;
123     }
124
125     /** Returns a hash code for this object, consistent with structural equality. */
126     public int equivHashCode()
127     {
128         return getField().equivHashCode() * 101 + baseBox.getValue().equivHashCode() + 17;
129     }
130
131     public void convertToBaf(JimpleToBafContext context, List out)
132     {
133         ((ConvertToBaf)getBase()).convertToBaf(context, out);
134     Unit u;
135         out.add(u = Baf.v().newFieldGetInst(fieldRef));
136
137     
138     Unit currentUnit = context.getCurrentUnit();
139     Iterator it = currentUnit.getTags().iterator();
140     while(it.hasNext()) {
141         u.addTag((Tag) it.next());
142     }
143     }
144 }
145
Popular Tags