• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get classes from a package

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:

If have a Package object, how can I get all its classes.

Thanks,
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would think you'd need to use Reflection in order to do something like that -- but looking at the package, it seems you can do stuff on the class level, but not on the package level.... You can also check out this tutorial.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure it'll work, but I'd try:
Turn the Package into a directory structure eg /com/javaranch/davo
Use ClassLoader.findResources(String name) to find all resources which define this package (there may be several)
For each location, list the resources and filter for class files.

Kind of round-about, but it may work...
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such concept as "all the classes in a package", in much the same way as there is no such concept as "all the subclasses of a class". This is because it is dynamic information.

A quick read of the spec. will reveal that you can use reflection to get all the loaded classes of a particular package.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
A quick read of the spec. will reveal that you can use reflection to get all the loaded classes of a particular package.



Oh, go on, give us a clue how, then...!

(Perhaps my "quick read" was too quick, but I didn't see how to do it.)
 
Rafael Afonso
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
There is no such concept as "all the classes in a package", in much the same way as there is no such concept as "all the subclasses of a class". This is because it is dynamic information.

A quick read of the spec. will reveal that you can use reflection to get all the loaded classes of a particular package.


In ClassLoader there is a protected method called getPackages(). So It is possible get all packages used by ClassLoader. But classes? Well, we back to my question.

Thanks,
[ August 05, 2004: Message edited by: Rafael Afonso ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic