Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Inspecting a package

Hi,

I need to inspect all the classes of a certain package (for example java.util).
I was looking for a piece of code like
.......
Classes [] classes = new Package("java.util").getClasses()
...

but this method does not exist.
Anyone knows how can i do it?
0
ebuchner
Asked:
ebuchner
1 Solution
 
objectsCommented:
No easy way to do that. The class loader only loads classes on demand, so until a class is needed it knows nothing about it.
0
 
girionisCommented:
 Objects is right, it is not easy to do it. What I can think off the top of my head is to write your customized class loader that goes through a given subdirectory (package) recursively and loads all the classes. Then you can use reflection to get the properties and methods of the classes.

 Hope it helps.
0
 
Igor BazarnyCommented:
Hi,

If you are using default classloader, you can use java.class.path, sun.boot.class.path and java.ext.dirs system properties and java.io.File, java.util.zip.* classes to list .class files there.

Something like that (just an idea):

String package = "java.util"
String packageDir = package.replace('.',File.separatorChar);
String packageInZip = package.replace('.', '/')+"/";
ArrayList classes = new ArrayList();

StringBuffer buffer = new StringBuffer(System.getProperty("sun.boot.class.path"));

StringTokenizer extTokens = new StringTokenizer(System.getProperty("java.ext.dirs"), File.pathSeparator);
while(extTokens.hasMoreElements()){
    File root = new File(extTokens.nextToken());
    File[] list = root.list();
    for(int i=0; i<list.length; ++i){
        if( list[i].getName().endsWith(".jar") ){
            buffer.append(File.pathSeparator)
                .append(list[i].getAbsolutePath());
        }
    }
}
buffer.append(File.pathSeparator)
   .append(System.getProperty("java.class.path"));
 
StringTokenizer pathTokens = new StringTokenizer(buffer.toString(), File.pathSeparator);

while(pathTokens.hasMoreElements()){
    File entry = new File(pathTokens.nextElement());
    if( entry.isDirectory() ){
        File dir = new File(entry,packageDir);
        if( dir.isDirectory() ){
            String[] list = dir.list();
            for( int i=0; i<list.length(); ++i){
                if( list[i].endsWith(".class") ){
                    classes.add(list[i]);
                }
            }
        }
    }
    else{
        ZipFile zip = new ZipFile(entry);
        Enumeration zipEntries = zip.entries();
        while(entries.hasMoreElements()){
            String current = ((ZipEntry)entries.nextElement()).getName();
            if(current.startsWith(packageInZip)){
                String relative = current.substring(packageinZip.length());
                if( relative.indexOf("/") < 0 && relative.endsWith(".class")){
                    classes.add(relative);
                }
            }
        }
    }
}

Regards,
Igor Bazarny,
Brianbench MVP for Java 1

 
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now