4

I have

public class SecondClass{
        MainClass main;
        public SecondClass(MainClass main){
                this.main=main;
        }
        ....
}

And in MainClass (.class file) have aMethod

    public class MainClass(){
        public void aMethod(){
                //I want to insert 
                //SecondClass sc = new SecondClass(this);
        }
}

How can I do this with Apache BCEL? Thank so much!

Toan PV
  • 201
  • 1
  • 7

1 Answers1

0

'this' is passed as the first item on the stack. so the way you can store it into a local variable is with the jvm instructions ALOAD and ASTORE.

For example the following code generates the corresponding jvm instructions.

public void test()
{
    Test var1 = this;
    Test var2 = this;
}

   ALOAD 0      
   ASTORE 1     
   ALOAD  0  
   ASTORE 2           
   RETURN