1 19 20 package soot; 21 import java.util.*; 22 import soot.util.*; 23 24 29 30 class AbstractSootFieldRef implements SootFieldRef { 31 public AbstractSootFieldRef( 32 SootClass declaringClass, 33 String 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 ( "Attempt to create SootFieldRef with null class" ); 41 if( name == null ) throw new RuntimeException ( "Attempt to create SootFieldRef with null name" ); 42 if( type == null ) throw new RuntimeException ( "Attempt to create SootFieldRef with null type" ); 43 } 44 45 private final SootClass declaringClass; 46 private final String name; 47 private final Type type; 48 private final boolean isStatic; 49 50 public SootClass declaringClass() { return declaringClass; } 51 public String name() { return name; } 52 public Type type() { return type; } 53 public boolean isStatic() { return isStatic; } 54 55 public String 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 toString() { 66 StringBuffer ret = new StringBuffer (); 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 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 toString() { 118 return getSignature(); 119 } 120 } 121 | Popular Tags |