udani
udani

Reputation: 1307

Can we add a import statement to a class using Javassist

I want to call a method in different class in a different package using the lines injected through javassist. Is there any other way to call those methods directly without making them public static and call using the fully qualified classname? In here it describes about a importPackage() method. But it looks like it can be used only with classpool object where we try to obtain the required class file.

Upvotes: 2

Views: 2268

Answers (1)

Nicholas
Nicholas

Reputation: 16066

Imports are compile time only. If you want to invoke non-static methods in another class, you will need some means of acquiring the correct object instances to invoke against, such as:

  1. Instantiating them
  2. Some sort of object finder
  3. Implements a shared collection that can be accessed statically that contains the objects.

One of these would need to be injected into your class so you can get the instances, and then you can wire in the actual invocation.

It might be helpful if you provided some code samples, perhaps how the code is now, and how you would like it to be.

Upvotes: 2

Related Questions