[Soot-list] Creating a class from existing class
Xiaoyong Zhou
zhou.xiaoyong at gmail.com
Thu Mar 10 09:35:25 EST 2011
I think <init> has a active body, but when I try to print the class like
this:
private void printClass(SootClass c) throws FileNotFoundException,
IOException{
gput("write class " + c.getName());
String fileName = SourceLocator.v().getFileNameFor(c,
Options.output_format_class);
OutputStream streamOut = new JasminOutputStream(new
FileOutputStream(fileName));
PrintWriter writerOut = new PrintWriter(new
OutputStreamWriter(streamOut));
JasminClass jasminClass = new soot.jimple.JasminClass(c);
jasminClass.print(writerOut);
writerOut.flush();
streamOut.close();
}
I got an error telling me that <init> has no active body. I don't know why
is that. Thanks.
On Wed, Mar 9, 2011 at 3:02 PM, Xiaoyong Zhou <zhou.xiaoyong at gmail.com>wrote:
> 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
>
>
--
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/20110310/ed816e89/attachment.html
More information about the Soot-list
mailing list