Modify The Body Of A Method

87 views
Skip to first unread message

Adeel Iqbal

unread,
Jan 14, 2013, 1:14:26 AM1/14/13
to javap...@googlegroups.com
hi,
i am new to JavaParser.
part of my research is to modify the contents of a specific method.
for example suppose the following method

    public void mymethod()
    {
        long l1=System.currentTimeMillis();
        for(int j=1;j<imax;j++)
            a[j]=b[j]+c[j];
        long l2=System.currentTimeMillis();
        System.out.println("Time Consumed: "+(l2-l1));
    }

and i have to modify the loop (for / while etc) and convert it into two loops like

    public void mymethod()
    {
        long l1=System.currentTimeMillis();

        for(int j=1;j<imax;j++)
             a[j]=b[j]+c[j];

        for(int j=imax/2;j<imax;j++)
             a[j]=b[j]+c[j];

        long l2=System.currentTimeMillis();
        System.out.println("Time Consumed: "+(l2-l1));
    }


this is an example.
i have got the body of the method in a string using the n.getBody() method. now the question is "whether to use my own code to edit the body (available in a string) and prepare the complete string. or there is some direct way in JavaParser to understand and modify the contents of the body".


hope it would be clear now. thanks in advance !

Matías Costa

unread,
Jan 14, 2013, 3:49:04 PM1/14/13
to javap...@googlegroups.com
Have you read http://code.google.com/p/javaparser/wiki/UsingThisParser ?

I would extend a visitor and override 

public void visit(ForStmt n, A arg) {

There you can analize the for and do whatever transformation you need. Do not forget the <for(A a: x) > statement that sometimes is equivalent to the <for> you are interested.


2013/1/14 Adeel Iqbal <adee...@gmail.com>

--
 
 

Adeel Iqbal

unread,
Jan 16, 2013, 6:47:23 AM1/16/13
to javap...@googlegroups.com
how to extend a class using javaparser.
e.g. i have a class name MyClass and i have to extend it from Threads class.
i.e.

public class MyClass { ----
}


and i want the final output as


public class MyClass extends Threads { ---
}
Reply all
Reply to author
Forward
0 new messages