Article View: borland.public.jbuilder.java.language
Article #8014Re: Gettging all classes in a package ?
From: Torsten Welches
Date: Thu, 26 Apr 2001 06:39
Date: Thu, 26 Apr 2001 06:39
55 lines
2681 bytes
2681 bytes
Mikael,
there is no API support for that. There is a "Package" class
but that is of no help. ClassLoader would be the place to look,
but there is no out-of-the-box method there either.
Each ClassLoader maintains a private Vector of "his" classes
(doc: "do keep the classes from being GC'ed").
Getting the field with reflection is a bit tricky because it is
private (getField won't work) and inherited (getDeclaredField
won't work on the concrete ClassLoader subclass):
Class c = this.getClass(); //whatever class to start with
ClassLoader cl = c.getClassLoader();
Class theClassLoaderClass = cl.getClass();
while (!(theClassLoaderClass.equals(ClassLoader.class))) {
classLoader = theClassLoaderClass.getSuperclass();
}
Field classesField = theClassLoaderClass.getDeclaredField("classes");
classesField.setAccessible(true); //it's private!
Vector v = (Vector)classesField.get(cl);
You now have all the classes of a given ClassLoader.
(here I assume that no ClassLoader subclass overrides the
addClass method without a super.addClass call <g>)
You would now need to loop over all active ClassLoader to get
the whole picture.
But how do you get all active ClassLoader?? lol!
Well, JBuilder does it, so it should be possible. <g>
Josh...?! ;-)
Torsten
"Mikael Fransson" <mikael.fransson@aspiro.com> wrote:
>im trying to make a method to gett all Classes in a Package
>ex:
>public static Class[] getAllClassesInPackage(String package)
>{
> ...
> return classArray;
>}
>
>Class[] classes=ClassLister.getAllClassesInPackage("java.util");
>for (int i = 0; i < classes.length; i++)
>{
> System.out.println(classes[i].getName());
>}
>
>Any ideas ?
>
>frasse@rings.se
>
>
Technical Details
Message-ID:
<3ae7c298$1_2@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!news-was.dfn.de!news-koe1.dfn.de!RRZ.Uni-Koeln.DE!nntp.CS.Uni-Magdeburg.De!PeerFeed.KWeb.on.ca!dnewsReferences:
<3ae69dfa$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,e2562564d66f3419 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-25 23:46:02 PST From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Subject: Re: Gettging all classes in a package ? Newsgroups: borland.public.jbuilder.java.language X-User-Info: 193.158.55.74 193.158.55.74 References: <3ae69dfa$1_2@dnews> NNTP-Posting-Host: 207.105.83.65 Message-ID: <3ae7c298$1_2@dnews> Date: 25 Apr 2001 23:39:20 -0700 X-Trace: dnews 988267160 207.105.83.65 (25 Apr 2001 23:39:20 -0700) Lines: 56 Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!news-was.dfn.de!news-koe1.dfn.de!RRZ.Uni-Koeln.DE!nntp.CS.Uni-Magdeburg.De!PeerFeed.KWeb.on.ca!dnews