Article View: borland.public.jbuilder.java.language
Article #8015Re: Gettging all classes in a package ?
From: Mikael Fransson
Date: Thu, 26 Apr 2001 11:27
Date: Thu, 26 Apr 2001 11:27
77 lines
3400 bytes
3400 bytes
I think you missed my point...
I'm going to write a class tester for JUnit where the only thing I have is a
package name
and I know that there are a lot of classes with only setters and getters and
ofcorse a constructor.
I have finished the test code for all the setters and getters but now I need
some thing to collect the classes for me.
its not necessary that I get them as Class, if I get the names I can create
the Class object my self.
So I need some more help. !
Any way:Thanks Torsten !
Mikael .....
"Torsten Welches" <torsten.welches@who.cares.netcologne.de> wrote in message
news:3ae7c298$1_2@dnews...
>
> 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:
<3ae8066d$1_2@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-02!supernews.com!bignews.mediaways.net!kinglear.mobilixnet.dk!opentransit.net!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3ae69dfa$1_2@dnews> <3ae7c298$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-26 04:46:58 PST From: "Mikael Fransson" <mikael.fransson@aspiro.com> Newsgroups: borland.public.jbuilder.java.language References: <3ae69dfa$1_2@dnews> <3ae7c298$1_2@dnews> Subject: Re: Gettging all classes in a package ? Date: Thu, 26 Apr 2001 13:27:53 +0200 Lines: 79 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 193.15.113.253 Message-ID: <3ae8066d$1_2@dnews> X-Trace: dnews 988284525 193.15.113.253 (26 Apr 2001 04:28:45 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-02!supernews.com!bignews.mediaways.net!kinglear.mobilixnet.dk!opentransit.net!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!dnews