• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking Package Hierarchy  RSS feed

 
Manu Verma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to check package hierarchy?
Suppose I have to display all classes contains in a Package P1, and P1 contains 2 sub Package P2 and P3. P2 and P3 have also few classes. So how we can check the folder hierarchy?

I have used this -

if (directory.exists())
{
// Get the list of the files contained in the package
String[] files = directory.list();
for (int i = 0; i < files.length; i++)
{
// we are only interested in .class files
if (files[i].endsWith(".class") )
{
// removes the .class extension
classes.add(Class.forName(pckgname + '.' + files[i].substring(0, files[i].length() - 6)));
// checking for SubPackages
}
}
}

But this will display class name of same folder, not checking for subfolder.

So what should I add in above code to check the subfolders and their class files???

Please provide me complete code.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manu Verma:
Please provide me complete code.


That's not how it works, round here. We don't do your job/homework for you, but we do like to help with problems.

Regarding the subdirectory issue, I think this is a classic application for recursion.
You should write a method that deals with all the class files in a given directory, then finds all the subdirectories and calls itself for each of them.

You can do a better job of listing files, too. You should not do a plain listFiles(), but instead should do listFiles(FileFilter). When listing the class files, you would pass a FileFilter that accepted only files with ".class" extension. When listing subdirectories (as suggested above), you should pass a FileFilter that accpets only directories, not files (use isDirectory()).
 
Manu Verma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton.
it helped me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!