[Soot-list] (no subject)

RASIKA LIMJE rasika1103 at gmail.com
Wed Mar 9 02:35:14 EST 2011


Respected Sir,

              I tried using “SceneTransformer” and the
“internalTransform” method in it and also used the “-w” option while
running the code. Since I am using NetBeans IDE I passed the “-w”
option as argument in the Run             ->Project Configuration->
Arguments dialog box. However I am getting an Exception of resolving
level.

              Also my second query wasn’t answered last time. When I
am trying to split methods and fields in the existing class into
different classes (suppose 2) such that some fields and methods are in
class 1 and other fields and methods are in class 2, I am not being
able to solve the field and method references. Obviously when split,
the fields and methods will belong to new classes and so wherever they
are used or defined the corresponding changes in their reference needs
to be done with respect to their new classes. How to get the
references?

             Also the calls made to the methods from original class
will be modified since they no longer belong to original class but the
new split classes. So objects for the new classes need to be created
for sure.

                I’ll be extremely obliged if you could assist me with
the above mentioned problems as soon as possible. I’m sending you the
following attachments:

1)      Original class file : Maths.java

2)      Required class files after doing above mentioned changes :
Maths1.java, Maths2.java, Maths3.java

3)      My source code in which I am being able to get the fields and
methods from a class and put those into a different class using
BodyTransformer: step2.java, ClassCopy.java

4)      Code in which I tried using Scenetransformer:
NewStep2.java,Step2ClassCopy.java


P.S. my arguments passed are
-w -process-dir <path of file to be analyzed and transformed>
-main-class <main class name>


Thanking you.
-------------- next part --------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package try1.algo;


import try1.algo.step2.*;
import soot.*;
import soot.jimple.*;
import java.util.*;
import soot.SootMethod;
/**
 *
 * @author Rasika
 */
public class ClassCopy extends BodyTransformer
{
  private static Type type;
    @SuppressWarnings("static-access")

protected void internalTransform(Body body, String phase, Map options)
{
    SootMethod method = body.getMethod();
    System.out.println("Method : " + method.getSignature());
    if(method.getName().compareTo(method.constructorName)!=0)
    {
    //create new method
    SootMethod m1 = new SootMethod(method.getName(),method.getParameterTypes(), method.getReturnType(), method.getModifiers(), method.getExceptions());
    //add method to class
    step2.c1.addMethod(m1);
    JimpleBody m1body = Jimple.v().newBody(m1);
    m1.setActiveBody(m1body);
    m1body.importBodyContentsFrom(body);
    }
}
}
-------------- next part --------------
class Maths
{
   public Integer a = new Integer(10);
   public Integer b = new Integer(15);
   public Integer add()
{
   return a+b;
} 
  public Integer avg()
{
   return (a+b)/2;
}

public static void main(String args[])
{
   Integer res;
   Maths obj1 = new Maths();
   res=obj1.add();
   System.out.println("Addition :"+res);
   res=obj1.avg();
   System.out.println("Average :"+res);
}
}
-------------- next part --------------
class Maths1
{
   public Integer a = new Integer(15);
   public Integer add()
{
   Maths2 obj = new Maths2();
   return a+obj.b;
} 
}
-------------- next part --------------
class Maths2
{
   public Integer b = new Integer(10);
   public Integer avg()
{
   Maths1 obj = new Maths1();
   return (obj.a+b)/2;
}
}
-------------- next part --------------
class Maths3
{
public static void main(String args[])
{
   Integer res;
   Maths1 obj1 = new Maths1();
   res=obj1.add();
   System.out.println("Addition :"+res);
   Maths2 obj2 = new Maths2();
   res=obj2.avg();
   System.out.println("Average :"+res);
}
}
-------------- next part --------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package try1.algo;
import soot.*;
import soot.jimple.*;
import soot.options.Options;
import soot.util.*;
import java.io.*;
import java.util.*;
import soot.G;
import soot.Scene;
import soot.SootClass;
import soot.SootField;
/**
 *
 * @author Rasika
 */
public class NewStep2 {
    public static SootClass c1;
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        Scene.v().loadClassAndSupport("java.lang.Object");
        Scene.v().loadClassAndSupport("java.lang.System");
        c1 = new SootClass("tryclass", Modifier.PUBLIC);
        c1.setSuperclass(Scene.v().getSootClass("java.lang.Object"));
        Scene.v().addClass(c1);
        Scene.v().setSootClassPath("D:\\NetBeansProjects\\Testcodes\\step1classes;C:\\Program Files\\Java\\jre6\\lib\\rt.jar;C:\\Program Files\\Java\\jre6\\lib\\jce.jar");

        /* add a phase to transformer pack by call Pack.add */
        Pack wjtp = PackManager.v().getPack("wjtp");
        wjtp.add(new Transform("wjtp.copy",new Step2ClassCopy()));
        soot.Main.main(args);

        String fileName = SourceLocator.v().getFileNameFor(c1, Options.output_format_class);
        OutputStream streamOut = new JasminOutputStream(
                                    new FileOutputStream(fileName));
        PrintWriter writerOut = new PrintWriter(
                                    new OutputStreamWriter(streamOut));
        JasminClass jasminClass = new soot.jimple.JasminClass(c1);
        jasminClass.print(writerOut);
        writerOut.flush();
        streamOut.close();
  }
}
-------------- next part --------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package try1.algo;

import soot.*;
import soot.jimple.*;
import soot.options.Options;
import soot.util.*;
import java.io.*;
import java.util.*;
import soot.G;
import soot.Scene;
import soot.SootClass;
import soot.SootField;

/**
 *
 * @author Rasika
 */
public class step2 {
    public static SootClass c1;
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        Scene.v().loadClassAndSupport("java.lang.Object");
        Scene.v().loadClassAndSupport("java.lang.System");
        c1 = new SootClass("tryclass", Modifier.PUBLIC);
        c1.setSuperclass(Scene.v().getSootClass("java.lang.Object"));
        Scene.v().addClass(c1);
        Scene.v().setSootClassPath("D:\\NetBeansProjects\\Testcodes\\step1classes;C:\\Program Files\\Java\\jre6\\lib\\rt.jar;C:\\Program Files\\Java\\jre6\\lib\\jce.jar");

        /* add a phase to transformer pack by call Pack.add */
        Pack jtp = PackManager.v().getPack("jtp");
        jtp.add(new Transform("jtp.copy",new ClassCopy()));
        soot.Main.main(args);

        Scene scene1 = G.v().soot_Scene();
        Iterator it = scene1.getApplicationClasses().iterator();
        while (it.hasNext())
        {
          SootClass c = (SootClass)it.next();
          System.out.println("class : " + c.getName());
          Iterator fIt = c.getFields().iterator();
          while (fIt.hasNext())
          {
              SootField f = (SootField)fIt.next();
              System.out.println("Adding field : " + f.getName());
              SootField f1 = new SootField(f.getName(), f.getType());
              c1.addField(f1);
          }
        }

        String fileName = SourceLocator.v().getFileNameFor(c1, Options.output_format_class);
        OutputStream streamOut = new JasminOutputStream(
                                    new FileOutputStream(fileName));
        PrintWriter writerOut = new PrintWriter(
                                    new OutputStreamWriter(streamOut));
        JasminClass jasminClass = new soot.jimple.JasminClass(c1);
        jasminClass.print(writerOut);
        writerOut.flush();
        streamOut.close();
  }
}
-------------- next part --------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package try1.algo;

import try1.algo.step2.*;
import soot.*;
import soot.jimple.*;
import java.util.*;
import soot.SootMethod;

/**
 *
 * @author Rasika
 */
public class Step2ClassCopy extends SceneTransformer{

   protected void internalTransform(String phase, Map options)
   {
        Scene new_scene = G.v().soot_Scene();
        Iterator it = new_scene.getApplicationClasses().iterator();
        while (it.hasNext())
        {
          SootClass c = (SootClass)it.next();
          System.out.println("class : " + c.getName());
          Iterator fIt = c.getFields().iterator();
          while (fIt.hasNext())
          {
              SootField f = (SootField)fIt.next();
              System.out.println("Adding field : " + f.getName());
              SootField f1 = new SootField(f.getName(), f.getType());
              NewStep2.c1.addField(f1);
          }
          Iterator mIt = c.getMethods().iterator();
          while (mIt.hasNext())
          {
              SootMethod method = (SootMethod)mIt.next();
              if(method.getName().compareTo(method.constructorName)!=0)
              {
                SootMethod m1 = new SootMethod(method.getName(),method.getParameterTypes(), method.getReturnType(), method.getModifiers(), method.getExceptions());
                NewStep2.c1.addMethod(m1);
                JimpleBody m1body = Jimple.v().newBody(m1);
                m1.setActiveBody(m1body);
                m1body.importBodyContentsFrom(method.getActiveBody());
              }
          }
        }

   }
}


More information about the Soot-list mailing list