Class SootUtilities


  • public class SootUtilities
    extends java.lang.Object
    This class consists of static utility methods for use with Soot
    Since:
    Ptolemy II 2.0
    Version:
    $Id$
    Author:
    Stephen Neuendorffer, Christopher Hylands
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Red (cxh)
    • Constructor Summary

      Constructors 
      Constructor Description
      SootUtilities()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertFinalField​(soot.SootClass theClass, soot.SootField theField, soot.Value newValue)
      Make the given field final.
      static void changeTypesInMethods​(soot.SootClass theClass, soot.SootClass oldClass, soot.SootClass newClass)
      Search through all the methods in the given class and change all references to the old class to references to the new class.
      static void changeTypesOfFields​(soot.SootClass theClass, soot.SootClass oldClass, soot.SootClass newClass)
      Search through all the fields in the given class and if the field is of class oldClass, then change it to newClass.
      static soot.jimple.Constant convertArgumentToConstantValue​(java.lang.Object object)
      Return a constant value that represents the same numeric value as the given object.
      static java.lang.Object convertConstantValueToArgument​(soot.Value value)
      Return an object that represents the same numeric value as the given value.
      static soot.SootClass copyClass​(soot.SootClass oldClass, java.lang.String newClassName)
      Copy a class
      static soot.SootField createAndSetFieldFromLocal​(soot.jimple.JimpleBody body, soot.Local local, soot.SootClass theClass, soot.Type type, java.lang.String name)
      Create a new instance field with the given name and type and add it to the given class.
      static soot.SootField createAndSetFieldFromLocal​(soot.jimple.JimpleBody body, soot.Local local, soot.SootClass theClass, soot.Type type, java.lang.String name, soot.Unit insertPoint)
      Create a new instance field with the given name and type and add it to the given class.
      static java.util.List createForLoopBefore​(soot.Body body, soot.Unit insertPoint, java.util.List initializerList, java.util.List bodyList, soot.jimple.Expr conditionalExpr)
      Create statements that correspond to a for loop and return them.
      static soot.Type createIsomorphicType​(soot.Type shapeType, soot.Type elementType)
      Create a type with the same shape as the given shape type, containing elements of the type given by the given element type.
      static soot.Local createRuntimeException​(soot.Body body, soot.Unit unit, java.lang.String string)
      Create a new local variable in the given body, initialized before the given unit that refers to a Runtime exception with the given string message.
      static soot.SootClass createStaticClassForInstance​(soot.SootClass theClass, soot.Body containerBody, soot.jimple.DefinitionStmt newStmt, soot.jimple.InvokeStmt constructorStmt, java.lang.String className)
      Create a new static class that will behave identically to the given instance.
      static boolean derivesFrom​(soot.SootClass theClass, soot.SootClass baseClass)
      Return true if the given class derives from the given base class.
      static void foldClass​(soot.SootClass theClass)
      Merge the given class with its super class.
      static java.lang.Class getClassForType​(soot.Type type)
      Given a Type object, return the java.lang.Class object that the type represents.
      static soot.SootMethod getMatchingMethod​(soot.SootClass theClass, java.lang.String name, java.util.List args)
      Get the method in the given class that has the given name and will accept the given argument list.
      static soot.SootMethod getSootMethodForMethod​(java.lang.reflect.Method method)  
      static boolean inlineCallsOnThisInMethod​(soot.SootMethod method)
      Inline all the method calls whose base is 'this' in the given method.
      static void inlineCallsToMethod​(soot.SootMethod inlineMethod, soot.SootClass theClass)
      Inline all calls to the given method that occur within the given class.
      static boolean isAliasableValue​(soot.Value value)
      Return true if the given value represents something that can be aliased in Java by something else.
      static boolean isSubtypeOf​(soot.Type type1, soot.Type type2)
      Return true if type2 is a subtype of type1.
      static void makeFieldStatic​(soot.SootClass theClass, soot.SootField field)
      Make the given field a static field.
      static java.lang.Object reflectAndInvokeConstructor​(soot.SootMethod sootMethod, soot.Value[] argValues)
      Reflect the given constructor method with the given arguments on the class of the given object.
      static java.lang.Object reflectAndInvokeMethod​(java.lang.Object object, soot.SootMethod sootMethod, soot.Value[] argValues)
      Reflect the given method on the class of the given object.
      static void replaceInvokeExpr​(soot.jimple.JimpleBody body, soot.jimple.Stmt stmt, soot.Value value)
      Replace the invoke expression in the given statement in the given body with the given value.
      static soot.SootMethod resolveSpecialInvokationForInlining​(soot.jimple.SpecialInvokeExpr expr, soot.SootMethod callingMethod)  
      static soot.SootMethod resolveVirtualInvokationForInlining​(soot.SootClass baseClass, soot.SootMethod targetMethod)  
      static soot.SootMethod searchForMethodByName​(soot.SootClass theClass, java.lang.String name)
      Get the method with the given name in the given class (or one of its super classes).
      static void unrollIteratorInstances​(soot.SootClass theClass, soot.SootField field, java.util.List fieldList)
      Anywhere where the iterator of the given field is referenced in the given class, unroll the iterator as if it contained the objects referenced by the given fields.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SootUtilities

        public SootUtilities()
    • Method Detail

      • assertFinalField

        public static void assertFinalField​(soot.SootClass theClass,
                                            soot.SootField theField,
                                            soot.Value newValue)
        Make the given field final. Anywhere where the the given field is used in the given class, inline the reference with the given value. Anywhere where the given field is illegally defined in the given class, inline the definition to throw an exception. This happens unless the given class is the defining class for the given field and the definition occurs within an initializer (for instance fields) or a static initializer (for static fields). Note that this is rather limited, since it is only really useful for constant values. In would be nice to specify a more complex expression to inline, but I'm not sure how to do it.
      • copyClass

        public static soot.SootClass copyClass​(soot.SootClass oldClass,
                                               java.lang.String newClassName)
        Copy a class
      • changeTypesOfFields

        public static void changeTypesOfFields​(soot.SootClass theClass,
                                               soot.SootClass oldClass,
                                               soot.SootClass newClass)
        Search through all the fields in the given class and if the field is of class oldClass, then change it to newClass.
        Parameters:
        theClass - The class containing fields to modify.
        oldClass - The class to replace.
        newClass - The new class.
      • changeTypesInMethods

        public static void changeTypesInMethods​(soot.SootClass theClass,
                                                soot.SootClass oldClass,
                                                soot.SootClass newClass)
        Search through all the methods in the given class and change all references to the old class to references to the new class. This includes field references, type casts, this references, new object instantiations and method invocations.
        Parameters:
        theClass - The class containing methods to modify.
        oldClass - The class to replace.
        newClass - The new class.
      • convertConstantValueToArgument

        public static java.lang.Object convertConstantValueToArgument​(soot.Value value)
        Return an object that represents the same numeric value as the given value.
        Parameters:
        value - A constant value.
      • convertArgumentToConstantValue

        public static soot.jimple.Constant convertArgumentToConstantValue​(java.lang.Object object)
        Return a constant value that represents the same numeric value as the given object.
        Parameters:
        object - An object that is assumed to be either a Token or a primitive Java object.
        Returns:
        A constant value.
      • createAndSetFieldFromLocal

        public static soot.SootField createAndSetFieldFromLocal​(soot.jimple.JimpleBody body,
                                                                soot.Local local,
                                                                soot.SootClass theClass,
                                                                soot.Type type,
                                                                java.lang.String name)
        Create a new instance field with the given name and type and add it to the given class. Add statements to the given body to initialize the field from the given local.
      • createAndSetFieldFromLocal

        public static soot.SootField createAndSetFieldFromLocal​(soot.jimple.JimpleBody body,
                                                                soot.Local local,
                                                                soot.SootClass theClass,
                                                                soot.Type type,
                                                                java.lang.String name,
                                                                soot.Unit insertPoint)
        Create a new instance field with the given name and type and add it to the given class. Add statements to the given body after the given insertion point to initialize the field from the given local.
      • createForLoopBefore

        public static java.util.List createForLoopBefore​(soot.Body body,
                                                         soot.Unit insertPoint,
                                                         java.util.List initializerList,
                                                         java.util.List bodyList,
                                                         soot.jimple.Expr conditionalExpr)
        Create statements that correspond to a for loop and return them. The returned list will incorporate the statements in the given list of initializer and body statements, and execute while the given conditional expression is true.
      • createIsomorphicType

        public static soot.Type createIsomorphicType​(soot.Type shapeType,
                                                     soot.Type elementType)
        Create a type with the same shape as the given shape type, containing elements of the type given by the given element type. That is, if shapeType is a base type (a reference Type, or a native type), then return elementType. If shapeType is an ArrayType, and elementType is a simple type, then return a new array type with the same number of dimensions as shapeType, and element type elementType. If both are array types, then return a new array type with the sum of the number of dimensions, and the element type elementType.
      • createRuntimeException

        public static soot.Local createRuntimeException​(soot.Body body,
                                                        soot.Unit unit,
                                                        java.lang.String string)
        Create a new local variable in the given body, initialized before the given unit that refers to a Runtime exception with the given string message.
      • createStaticClassForInstance

        public static soot.SootClass createStaticClassForInstance​(soot.SootClass theClass,
                                                                  soot.Body containerBody,
                                                                  soot.jimple.DefinitionStmt newStmt,
                                                                  soot.jimple.InvokeStmt constructorStmt,
                                                                  java.lang.String className)
        Create a new static class that will behave identically to the given instance. Replace all references to the given instance with references to the static class.
        Parameters:
        theClass - The context in which method calls on the given instance will be replaced with method calls to the new static class.
        containerBody - The body that contains the definition statement.
        newStmt - The statement where the instance is created. The right hand side of the definition is assumed to be an instance of NewExpr.
        constructorStmt - The statement where the initializer for the instance is called.
        className - The name of the class that will be created.
      • derivesFrom

        public static boolean derivesFrom​(soot.SootClass theClass,
                                          soot.SootClass baseClass)
        Return true if the given class derives from the given base class.
      • isSubtypeOf

        public static boolean isSubtypeOf​(soot.Type type1,
                                          soot.Type type2)
        Return true if type2 is a subtype of type1.
      • foldClass

        public static void foldClass​(soot.SootClass theClass)
        Merge the given class with its super class. All of the methods and fields of the super class will be added to the given class, if they do not already exist. Methods existing in both the given class and the super class will be merged by inlining the super class method.
      • getClassForType

        public static java.lang.Class getClassForType​(soot.Type type)
                                               throws java.lang.ClassNotFoundException
        Given a Type object, return the java.lang.Class object that the type represents.
        Throws:
        java.lang.ClassNotFoundException
      • getMatchingMethod

        public static soot.SootMethod getMatchingMethod​(soot.SootClass theClass,
                                                        java.lang.String name,
                                                        java.util.List args)
        Get the method in the given class that has the given name and will accept the given argument list.
      • getSootMethodForMethod

        public static soot.SootMethod getSootMethodForMethod​(java.lang.reflect.Method method)
      • resolveSpecialInvokationForInlining

        public static soot.SootMethod resolveSpecialInvokationForInlining​(soot.jimple.SpecialInvokeExpr expr,
                                                                          soot.SootMethod callingMethod)
      • resolveVirtualInvokationForInlining

        public static soot.SootMethod resolveVirtualInvokationForInlining​(soot.SootClass baseClass,
                                                                          soot.SootMethod targetMethod)
      • inlineCallsToMethod

        public static void inlineCallsToMethod​(soot.SootMethod inlineMethod,
                                               soot.SootClass theClass)
        Inline all calls to the given method that occur within the given class. Note that this alone will really only increase the size of the affected code, but it turns cross-method optimizations into local optimizations which often allows us to do interesting things afterwards, since we know the values of any parameters to the method.
      • inlineCallsOnThisInMethod

        public static boolean inlineCallsOnThisInMethod​(soot.SootMethod method)
        Inline all the method calls whose base is 'this' in the given method.
        Returns:
        true if any changes were made.
      • isAliasableValue

        public static boolean isAliasableValue​(soot.Value value)
        Return true if the given value represents something that can be aliased in Java by something else. For instance, a local variable can point to the same object as another local variable.
      • makeFieldStatic

        public static void makeFieldStatic​(soot.SootClass theClass,
                                           soot.SootField field)
        Make the given field a static field. Loop through all the methods of the given class and replace instance references to the given field with static references. Note that in general, this is not a safe thing to do unless there is guaranteed to be exactly one instance of the class that defines the given field.
      • reflectAndInvokeMethod

        public static java.lang.Object reflectAndInvokeMethod​(java.lang.Object object,
                                                              soot.SootMethod sootMethod,
                                                              soot.Value[] argValues)
        Reflect the given method on the class of the given object. Invoke the method and return the returned value
      • reflectAndInvokeConstructor

        public static java.lang.Object reflectAndInvokeConstructor​(soot.SootMethod sootMethod,
                                                                   soot.Value[] argValues)
        Reflect the given constructor method with the given arguments on the class of the given object. Invoke the method and return the returned value.
        Parameters:
        sootMethod - The soot initializer method that corresponds to the correct Java constructor.
        argValues - The arguments, which must be constant valued.
      • replaceInvokeExpr

        public static void replaceInvokeExpr​(soot.jimple.JimpleBody body,
                                             soot.jimple.Stmt stmt,
                                             soot.Value value)
        Replace the invoke expression in the given statement in the given body with the given value. If the statement is an invoke statement (without a return value) and the value is not an invoke expression, then blindly replacing the invoke expression is incorrect. This method deals with this corner case by removing the statement from the given body.
      • searchForMethodByName

        public static soot.SootMethod searchForMethodByName​(soot.SootClass theClass,
                                                            java.lang.String name)
        Get the method with the given name in the given class (or one of its super classes). If the method name is ambiguous or there is some other problem, then a RuntimeException is thrown that includes the names of all possible methods for that class at that level.
      • unrollIteratorInstances

        public static void unrollIteratorInstances​(soot.SootClass theClass,
                                                   soot.SootField field,
                                                   java.util.List fieldList)
        Anywhere where the iterator of the given field is referenced in the given class, unroll the iterator as if it contained the objects referenced by the given fields.