Re: Get imported packages using BCEL

"Nam T. Nguyen" <[email protected]> Fri, 03 Mar 2006 07:27:23 -0800
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
Pico Florin wrote:
> Hi!
>     It is possible to use BCEL API to obtain the imported packages(classes) from a java binary code? 
>    Thank you in advance,
>           Florin

Hi Pico

It is possible to do so.
1. Get the current package name.
2. Iterate through all Classref's
3. If the current package is different from those Classref's, and they 
are not java.lang, they are the imported packages.
4. Optionally, you can put a threshold number, say 3, of imported 
classes to convert their import statements to one import package 
statement. eg: from import abc.C1; import abc.C2; import abc.C3 to 
import abc.*;

Well, this may not produce 100% correct code. For example: If you have 
code like this:

java.util.Collections.sort(myList);

You would expect java.util.Collections not showing up as imported 
package. But it will be shown as imported package here.

By the way, by "import"ed package, I mean those declared with "import" 
keyword.

Cheers
Nam