[Soot-list] Creating a class from existing class

Xiaoyong Zhou zhou.xiaoyong at gmail.com
Wed Mar 9 15:02:39 EST 2011


Hi, Eric:

Thank you for your help. I am trying to duplicate a SootClass like this:
It works for methods with active body, but will ignore the methods without
active body like "init" functions. How can I completely clone a class?
Besides methods and fields, what else need to be copied? Thanks a lot.

    private SootClass duplicateClass(SootClass c, String className){
        gput("duplicating class: " + className);

        //create a new class
        SootClass tmpClass = new SootClass(className, Modifier.PUBLIC);
        tmpClass.setSuperclass(c.getSuperclass());

        //copy methods
        Iterator<SootMethod> mi = c.getMethods().iterator();
        while(mi.hasNext()){
            SootMethod m = mi.next();
            SootMethod newm = new SootMethod(m.getName(),
m.getParameterTypes(), m.getReturnType());
            gput("add method " + m.getName());

            //remove existing class method and create new
            if(tmpClass.declaresMethod(m.getSignature())){
                tmpClass.removeMethod(tmpClass.getMethod(m.getSignature()));
            }

            //copy this body
            if(m.hasActiveBody()){
                JimpleBody newBody = Jimple.v().newBody(newm);
                newBody.importBodyContentsFrom(m.getActiveBody());
            }else{
                JimpleBody newBody = Jimple.v().newBody(newm);

            }
            tmpClass.addMethod(newm);
        }

        Iterator<SootField> fi = c.getFields().iterator();
        while(fi.hasNext()){
            SootField f = fi.next();
            //copy soot method
            SootField newField = new SootField(f.getName(), f.getType(),
f.getModifiers());
            if(!tmpClass.declaresField(newField.getSignature())){
                tmpClass.addField(newField);
            }
        }

        return tmpClass;
    }


On Fri, Mar 4, 2011 at 10:41 AM, Eric Bodden <
bodden at st.informatik.tu-darmstadt.de> wrote:

> Hello.
>
> Soot does not support this very well. You need to create new fields
> that have the same signatures as the original ones. Similar, you need
> to create new methods with the same signatures as the original ones.
> Method *bodies* you can copy using this method:
>
> http://www.sable.mcgill.ca/soot/doc/soot/Body.html#importBodyContentsFrom(soot.Body)
>
> Eric
>
> On 4 March 2011 15:57, Xiaoyong Zhou <zhou.xiaoyong at gmail.com> wrote:
> > Hi, All:
> >
> > I am trying to create a class from an existing class and want to
> duplicate
> > an class and then modify it. Note that I want to keep the previous class.
> > But I did not figure out how to "clone" an class in soot Scene. I was
> > thinking about copy fields by fields and methods by methods, but it does
> not
> > work because I can not add an SootMethod of class A to class B. Need some
> > help. Thanks a lot.
> >
> > --
> > Sincerely yours,
> > Xiaoyong Zhou
> > School of Informatics and Computing
> > Indiana University, Bloomington, IN
> >
> >
> > _______________________________________________
> > Soot-list mailing list
> > Soot-list at sable.mcgill.ca
> > http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
> >
> >
>
>
>
> --
> Dr. Eric Bodden, http://bodden.de/
> Principal Investigator in Secure Services at CASED
> Coordinator of the CASED Advisory Board of Study Affairs
> PostDoc at Software Technology Group, Technische Universität Darmstadt
> Tel: +49 6151 16-5478    Fax: +49 6151 16-5410
> Mailing Address: S2|02 A209, Hochschulstraße 10, 64289 Darmstadt
>



-- 
Sincerely yours,
Xiaoyong Zhou
School of Informatics and Computing
Indiana University, Bloomington, IN
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.cs.mcgill.ca/pipermail/soot-list/attachments/20110309/983f258b/attachment.html 


More information about the Soot-list mailing list