KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > AbstractSootFieldRef


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Ondrej Lhotak
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;
21 import java.util.*;
22 import soot.util.*;
23
24 /** Representation of a reference to a field as it appears in a class file.
25  * Note that the field directly referred to may not actually exist; the
26  * actual target of the reference is determined according to the resolution
27  * procedure in the Java Virtual Machine Specification, 2nd ed, section 5.4.3.2.
28  */

29
30 class AbstractSootFieldRef implements SootFieldRef {
31     public AbstractSootFieldRef(
32             SootClass declaringClass,
33             String JavaDoc name,
34             Type type,
35             boolean isStatic) {
36         this.declaringClass = declaringClass;
37         this.name = name;
38         this.type = type;
39         this.isStatic = isStatic;
40         if( declaringClass == null ) throw new RuntimeException JavaDoc( "Attempt to create SootFieldRef with null class" );
41         if( name == null ) throw new RuntimeException JavaDoc( "Attempt to create SootFieldRef with null name" );
42         if( type == null ) throw new RuntimeException JavaDoc( "Attempt to create SootFieldRef with null type" );
43     }
44
45     private final SootClass declaringClass;
46     private final String JavaDoc name;
47     private final Type type;
48     private final boolean isStatic;
49
50     public SootClass declaringClass() { return declaringClass; }
51     public String JavaDoc name() { return name; }
52     public Type type() { return type; }
53     public boolean isStatic() { return isStatic; }
54
55     public String JavaDoc getSignature() {
56         return SootField.getSignature(declaringClass, name, type);
57     }
58
59     public class FieldResolutionFailedException extends ResolutionFailedException {
60         public FieldResolutionFailedException() {
61             super("Class "+declaringClass+" doesn't have field "+name+
62                     " : "+type+
63                     "; failed to resolve in superclasses and interfaces" );
64         }
65         public String JavaDoc toString() {
66             StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
67             ret.append(super.toString());
68             resolve(ret);
69             return ret.toString();
70         }
71     }
72
73     public SootField resolve() {
74         return resolve(null);
75     }
76     private SootField checkStatic(SootField ret) {
77         if( ret.isStatic() != isStatic()) {
78             throw new ResolutionFailedException( "Resolved "+this+" to "+ret+" which has wrong static-ness" );
79         }
80         return ret;
81     }
82     private SootField resolve(StringBuffer JavaDoc trace) {
83         SootField ret = null;
84         SootClass cl = declaringClass;
85         while(true) {
86             if(trace != null) trace.append(
87                     "Looking in "+cl+" which has fields "+cl.getFields()+"\n" );
88             if( cl.declaresField(name, type) ) {
89                 return checkStatic(cl.getField(name, type));
90             }
91
92             if(Scene.v().allowsPhantomRefs() && cl.isPhantom())
93             {
94                 SootField f = new SootField(name, type, isStatic()?Modifier.STATIC:0);
95                 f.setPhantom(true);
96                 cl.addField(f);
97                 return f;
98             } else {
99                 LinkedList queue = new LinkedList();
100                 queue.addAll( cl.getInterfaces() );
101                 while( !queue.isEmpty() ) {
102                     SootClass iface = (SootClass) queue.removeFirst();
103                     if(trace != null) trace.append(
104                             "Looking in "+iface+" which has fields "+iface.getFields()+"\n" );
105                     if( iface.declaresField(name, type) ) {
106                         return checkStatic(iface.getField( name, type ));
107                     }
108                     queue.addAll( iface.getInterfaces() );
109                 }
110                 if( cl.hasSuperclass() ) cl = cl.getSuperclass();
111                 else break;
112             }
113         }
114         if( trace == null ) throw new FieldResolutionFailedException();
115         return null;
116     }
117     public String JavaDoc toString() {
118         return getSignature();
119     }
120 }
121
Popular Tags