Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Soot - a J*va Optimization Framework
   * Copyright (C) 2004 Ondrej Lhotak
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
 package soot;
 import java.util.*;
 
 import soot.util.*;

Representation of a reference to a method as it appears in a class file. Note that the method directly referred to may not actually exist; the actual target of the reference is determined according to the resolution procedure in the Java Virtual Machine Specification, 2nd ed, section 5.4.3.3.
 
 
 class SootMethodRefImpl implements SootMethodRef {
     public SootMethodRefImpl
             SootClass declaringClass,
             String name,
             List parameterTypes,
             Type returnType,
             boolean isStatic) {
         this. = declaringClass;
         this. = name;
         List l = new ArrayList();
         l.addAll(parameterTypes);
         this. = Collections.unmodifiableList(l);
         this. = returnType;
         this. = isStatic;
         ifdeclaringClass == null ) throw new RuntimeException"Attempt to create SootMethodRef with null class" );
         ifname == null ) throw new RuntimeException"Attempt to create SootMethodRef with null name" );
         ifparameterTypes == null ) throw new RuntimeException"Attempt to create SootMethodRef with null parameterTypes" );
         ifreturnType == null ) throw new RuntimeException"Attempt to create SootMethodRef with null returnType" );        
     	if(declaringClass.getName().equals("java.dyn.InvokeDynamic") && !isStatic) {
     		throw new IllegalArgumentException("invokedynamic method references must be static!");
     	}
     }
 
     private final SootClass declaringClass;
     private final String name;
     private final List parameterTypes;
     private final Type returnType;
     private final boolean isStatic;
 
     private NumberedString subsig;
 
     public SootClass declaringClass() { return ; }
     public String name() { return ; }
     public List parameterTypes() { return ; }
     public Type returnType() { return ; }
     public boolean isStatic() { return ; }
 
     public NumberedString getSubSignature() {
         if == null ) {
              = Scene.v().getSubSigNumberer().findOrAdd(
                 SootMethod.getSubSignature ));
         }
         return ;
     }
 
     public String getSignature() {
         return SootMethod.getSignature();
     }
 
     public Type parameterType(int i) {
         return (Type.get(i);
     }
 
     public class ClassResolutionFailedException extends ResolutionFailedException {
         public ClassResolutionFailedException() {
             super("Class "++" doesn't have method "++
                     "("++")"+" : "++
                     "; failed to resolve in superclasses and interfaces" );
         }
        public String toString() {
            StringBuffer ret = new StringBuffer();
            ret.append(super.toString());
            resolve(ret);
            return ret.toString();
        }
    }
    public SootMethod resolve() {
        return resolve(null);
    }
    
    private SootMethod checkStatic(SootMethod ret) {
        ifret.isStatic() != isStatic()) {
            throw new ResolutionFailedException"Resolved "+this+" to "+ret+" which has wrong static-ness" );
        }
        return ret;
    }
    
    private SootMethod resolve(StringBuffer trace) {
    	if(.getName().equals("java.dyn.InvokeDynamic")) {
    		throw new IllegalStateException("Cannot resolve invokedynamic method references at compile time!");
    	}
        SootClass cl = ;
        while(true) {
            if(trace != nulltrace.append(
                    "Looking in "+cl+" which has methods "+cl.getMethods()+"\n" );
            ifcl.declaresMethodgetSubSignature() ) )
                return checkStatic(cl.getMethodgetSubSignature() ));
            if(Scene.v().allowsPhantomRefs() && cl.isPhantom())
            {
                SootMethod m = new SootMethod(isStatic()?.:0);
                m.setPhantom(true);
                cl.addMethod(m);
                return checkStatic(m);
            }
            ifcl.hasSuperclass() ) cl = cl.getSuperclass();
            else break;
        }
        cl = ;
        while(true) {
            LinkedList<SootClassqueue = new LinkedList<SootClass>();
            queue.addAllcl.getInterfaces() );
            while( !queue.isEmpty() ) {
                SootClass iface = queue.removeFirst();
                if(trace != nulltrace.append(
                        "Looking in "+iface+" which has methods "+iface.getMethods()+"\n" );
                ififace.declaresMethodgetSubSignature() ) )
                    return checkStatic(iface.getMethodgetSubSignature() ));
                queue.addAlliface.getInterfaces() );
            }
            ifcl.hasSuperclass() ) cl = cl.getSuperclass();
            else break;
        }
        
        //when allowing phantom refs we also allow for references to non-existing methods;
        //we simply create the methods on the fly; the method body will throw an appropriate
        //error just in case the code *is* actually reached at runtime
        if(Options.v().allow_phantom_refs()) {
        	SootMethod m = new SootMethod();
        	JimpleBody body = Jimple.v().newBody(m);
			//exc = new Error
			RefType runtimeExceptionType = RefType.v("java.lang.Error");
			NewExpr newExpr = Jimple.v().newNewExpr(runtimeExceptionType);
			LocalGenerator lg = new LocalGenerator(body);
			Local exceptionLocal = lg.generateLocal(runtimeExceptionType);
			AssignStmt assignStmt = Jimple.v().newAssignStmt(exceptionLocalnewExpr);
			body.getUnits().add(assignStmt);
			//exc.<init>(message)
			SootMethodRef cref = runtimeExceptionType.getSootClass().getMethod("<init>", Collections.singletonList(RefType.v("java.lang.String"))).makeRef();
			SpecialInvokeExpr constructorInvokeExpr = Jimple.v().newSpecialInvokeExpr(exceptionLocalcref, StringConstant.v("Unresolved compilation error: Method "+getSignature()+" does not exist!"));
			InvokeStmt initStmt = Jimple.v().newInvokeStmt(constructorInvokeExpr);
			body.getUnits().insertAfter(initStmtassignStmt);
			//throw exc
			body.getUnits().insertAfter(Jimple.v().newThrowStmt(exceptionLocal), initStmt);
			return m
        } else iftrace == null ) {
        	throw new ClassResolutionFailedException();
        }
        return null;
    }
    
    public String toString() {
        return getSignature();
    }
New to GrepCode? Check out our FAQ X