相关网页地址:https://github.com/Sable/soot/wiki/Adding-profiling-instructions-to-applications
这篇博文讲 SootClass, SootMethod and Unit classes 这几个类的具体使用方法。
1.首先,我们先要可以创造一个类来把方法放进去,下面是创造一个类的必要步骤:
(1)加载java.lang.Object,和库类。又由于这个类需要其他的类支持,又要加载Scene.v().loadClassAndSupport(“java.lang.System”);
The Scene is the container for all of the SootClasses in a program, and provides various utility methods. There is a singleton Scene object, accessible by calling Scene.v().
(2)加载SootClass,创建类
Create the HelloWorld’ SootClass, and set its super class as`java.lang.Object”.
sClass = new SootClass(“HelloWorld”, Modifier.PUBLIC);
This code creates a SootClass object for a public class named HelloWorld.
sClass.setSuperclass(Scene.v().getSootClass(“java.lang.Object”));
This sets the superclass of the newly-created class to the SootClass object for java.lang.Object. Note the use of the utility method getSootClass on the Scene.
Scene.v().addClass(sClass);
This adds the newly-created HelloWorld class to the Scene. All classes should belong to the Scene once they are created.
(3)在创建的Class中创建方法Main!
Adding methods to SootClasses
Create a main() method for HelloWorld with an empty body.
Now that we have a SootClass, we need to add methods to it.
**method = new SootMethod(“main”,
Arrays.asList(new Type[] {ArrayType.v(RefType.v(“java.lang.String”), 1)}),
VoidType.v(), Modifier.PUBLIC | Modifier.STATIC);**
We create a new public static method, main, declare that it takes an array of java.lang.String objects, and that it returns void.
The constructor for SootMethod takes a list, so we call the Java utility method Arrays.asList to create a list from the one-element array which we generate on the fly with new Type[] … . In the list, we put an array type, corresponding to a one-dimensional ArrayType of java.lang.String objects. The call to RefType fetches the type corresponding to the java.lang.String class.
Types Each SootClass represents a Java object. We can instantiate the class, giving an object with a given type. The two notions - type and class - are closely related, but distinct. To get the type for the java.lang.String class, by name, we call RefType.v(“java.lang.String”). Given a SootClass object sc, we could also call sc.getType() to get the corresponding type.
sClass.addMethod(method);
(4