Solved

Inspecting a package

Posted on 2002-04-10
3
232 Views
Last Modified: 2010-03-31
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
Comment
Question by:ebuchner
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6932662
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
 
LVL 35

Expert Comment

by:girionis
ID: 6932773
 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
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 200 total points
ID: 6933338
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now