Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reflection: Instantiate All Classes in a Package?

Reply
Thread Tools

Reflection: Instantiate All Classes in a Package?

 
 
Jason Cavett
Guest
Posts: n/a
 
      03-23-2007
I was wondering...

Is it possible, by using Java reflection, to instantiate all classes
existing in a package without knowing the names of those classes. I'm
envisioning something like this (pseudocode):

Start my class
Class goes to package x
Package x contanis all the classes I need to instantiate
Using a (loop/something?) I loop through all the classes and
instantiate each one

To clarify what I'm trying to do, I am creatnig an error checking
application that relies on a series of rules that groups of developers
will code in Java (yeah, I realize I could have it in XML or something
that makes more sense, but I am the third or fourth developer to work
on this project). All the (compiled) classes are placed in a specific
package, but I never will know exactly which classes are there. I was
hoping for some way to discover and initialize them at runtime. (They
all implement the same interface, so that part is easy.)

Is this even possible? Is there a better solution? Thanks for any
clarification.

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-23-2007
Jason Cavett wrote:
> I was wondering...
>
> Is it possible, by using Java reflection, to instantiate all classes
> existing in a package without knowing the names of those classes. I'm
> envisioning something like this (pseudocode):
>
> Start my class
> Class goes to package x
> Package x contanis all the classes I need to instantiate
> Using a (loop/something?) I loop through all the classes and
> instantiate each one
>
> To clarify what I'm trying to do, I am creatnig an error checking
> application that relies on a series of rules that groups of developers
> will code in Java (yeah, I realize I could have it in XML or something
> that makes more sense, but I am the third or fourth developer to work
> on this project). All the (compiled) classes are placed in a specific
> package, but I never will know exactly which classes are there. I was
> hoping for some way to discover and initialize them at runtime. (They
> all implement the same interface, so that part is easy.)
>
> Is this even possible? Is there a better solution? Thanks for any
> clarification.
>


Using Java reflection, the answer is NO. For more information, check the
message thread "Getting a list of classes in a package"; there is a nice
long discussion on class/package/reflection.

P.S. it generally helps to search recent posts on Usenet before asking a
question.
 
Reply With Quote
 
 
 
 
Jason Cavett
Guest
Posts: n/a
 
      03-23-2007
On Mar 23, 5:23 pm, Joshua Cranmer <(E-Mail Removed)> wrote:
> Jason Cavett wrote:
> > I was wondering...

>
> > Is it possible, by using Java reflection, to instantiate all classes
> > existing in a package without knowing the names of those classes. I'm
> > envisioning something like this (pseudocode):

>
> > Start my class
> > Class goes to package x
> > Package x contanis all the classes I need to instantiate
> > Using a (loop/something?) I loop through all the classes and
> > instantiate each one

>
> > To clarify what I'm trying to do, I am creatnig an error checking
> > application that relies on a series of rules that groups of developers
> > will code in Java (yeah, I realize I could have it in XML or something
> > that makes more sense, but I am the third or fourth developer to work
> > on this project). All the (compiled) classes are placed in a specific
> > package, but I never will know exactly which classes are there. I was
> > hoping for some way to discover and initialize them at runtime. (They
> > all implement the same interface, so that part is easy.)

>
> > Is this even possible? Is there a better solution? Thanks for any
> > clarification.

>
> Using Java reflection, the answer is NO. For more information, check the
> message thread "Getting a list of classes in a package"; there is a nice
> long discussion on class/package/reflection.
>
> P.S. it generally helps to search recent posts on Usenet before asking a
> question.- Hide quoted text -
>
> - Show quoted text -


My mistake. Thanks for pointing me to that thread.

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-23-2007

"Joshua Cranmer" <(E-Mail Removed)> wrote in message
news:lzXMh.493$Ht2.302@trnddc06...
> Jason Cavett wrote:
>> I was wondering...
>>
>> Is it possible, by using Java reflection, to instantiate all classes
>> existing in a package without knowing the names of those classes.

[...]
>
> Using Java reflection, the answer is NO. For more information, check the
> message thread "Getting a list of classes in a package"; there is a nice
> long discussion on class/package/reflection.


Actually, the answer turned out to be "yes" (though I can't find the
thread where we concluded this), but you would probably never want to
instantiate all the classes existing in a package anyway, because there
are an infinite number of such classes.

OP, instead of instantiating all classes in a package, how about
instantiating all classes located within a specific directory on the local
harddrive?

- Oliver


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-23-2007

"Oliver Wong" <(E-Mail Removed)> wrote in message
news:GmZMh.23233$(E-Mail Removed)...
>
> "Joshua Cranmer" <(E-Mail Removed)> wrote in message
> news:lzXMh.493$Ht2.302@trnddc06...
>> Jason Cavett wrote:
>>> I was wondering...
>>>
>>> Is it possible, by using Java reflection, to instantiate all classes
>>> existing in a package without knowing the names of those classes.

> [...]
>>
>> Using Java reflection, the answer is NO. For more information, check
>> the message thread "Getting a list of classes in a package"; there is a
>> nice long discussion on class/package/reflection.

>
> Actually, the answer turned out to be "yes" (though I can't find the
> thread where we concluded this),


Found it:
http://groups.google.ca/group/comp.l...ca74ec29d74031

- Oliver


 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      03-23-2007
>>> Is it possible, by using Java reflection, to instantiate all classes
>>> existing in a package without knowing the names of those classes.

>[...]
>> Using Java reflection, the answer is NO. For more information, check the
>> message thread "Getting a list of classes in a package"; there is a nice
>> long discussion on class/package/reflection.


Oliver Wong <(E-Mail Removed)> wrote:
> Actually, the answer turned out to be "yes" (though I can't find the
>thread where we concluded this)


Um, no. The answer turned out to be "No, but if you define your problem more
clearly, we can suggest alternatatives that are possible, like enumerating
classes in certain known locations and instantiating those that implement a
given interface."

> OP, instead of instantiating all classes in a package, how about
>instantiating all classes located within a specific directory on the local
>harddrive?


Indeed!
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      03-24-2007
Mark Rafn wrote:

> Oliver Wong <(E-Mail Removed)> wrote:
> > Actually, the answer turned out to be "yes" (though I can't find the
> > thread where we concluded this)

>
> Um, no. The answer turned out to be "No"


No, /that/ conversation concluded -- correctly -- that the answer is "yes".
It is trivial, albeit a little slow, to probe a classloader for all the
possible potential names of classes -- see the link in Oliver's follow-up post
for details and further speculation.

-- chris



 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-24-2007
Chris Uppal wrote:
> Mark Rafn wrote:
>
>> Oliver Wong <(E-Mail Removed)> wrote:
>>> Actually, the answer turned out to be "yes" (though I can't find the
>>> thread where we concluded this)

>> Um, no. The answer turned out to be "No"

>
> No, /that/ conversation concluded -- correctly -- that the answer is "yes".
> It is trivial, albeit a little slow, to probe a classloader for all the
> possible potential names of classes -- see the link in Oliver's follow-up post
> for details and further speculation.
>
> -- chris
>
>
>


When I said that the answer is "no", I meant that purely using
reflection, i.e. stock java.lang.reflect.*/ClassLoader, this could not
be done. Through writing custom ClassLoaders or (my preference), parsing
the class path/bootstrap path/extensions directory, one can obtain the
needed classes.

The OP originally asked for the answer using /reflection/ -- he was more
or less asking if the answer could there was a method along the lines of
getAllClasses(String package).

In summary: Yes, one can get a list of classes in a certain package
through various means, but no, it cannot be done through /pure/
reflection. Happy now?
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      03-25-2007
Joshua Cranmer wrote:

> In summary: Yes, one can get a list of classes in a certain package
> through various means, but no, it cannot be done through /pure/
> reflection. Happy now?


No, It can be done through nothing more than pure reflection (using only
ClassLoader.findClass() as exposed via Class.forName(String, ClassLoader))
Read the thread that Olver referenced. I was going to post code but, what with
the difficulties of handling Unicode in Java, and the absence of standard
combinatorial generators in the library, it's a bit more effort to do properly
than it's worth.

-- chris


 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-25-2007
Chris Uppal wrote:
> Joshua Cranmer wrote:
>
>> In summary: Yes, one can get a list of classes in a certain package
>> through various means, but no, it cannot be done through /pure/
>> reflection. Happy now?

>
> No, It can be done through nothing more than pure reflection (using only
> ClassLoader.findClass() as exposed via Class.forName(String, ClassLoader))
> Read the thread that Olver referenced. I was going to post code but, what with
> the difficulties of handling Unicode in Java, and the absence of standard
> combinatorial generators in the library, it's a bit more effort to do properly
> than it's worth.
>
> -- chris
>
>


Should I append 'feasibly' to my list? To use ClassLoader.findClass()
would require checking more than 64K^64K ~ 10^1M combinations to find
every class and probably on the order of 10^22 for more reasonable
assumptions (100 possible characters, 20-character lengths). It would be
much more efficient to open the Jar-files, find the package directory,
and load the classes.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Can a repeater's ItemTemplate instantiate derived classes? Big Daddy ASP .Net Web Controls 1 10-29-2006 09:23 PM
Can a repeater's ItemTemplate instantiate derived classes? Big Daddy ASP .Net Building Controls 1 10-28-2006 10:39 AM
instantiate all subclasses of a class Daniel Nogradi Python 7 07-16-2006 04:18 PM
Instantiate webform classes .... =?Utf-8?B?TnVubw==?= ASP .Net 3 02-15-2006 02:42 PM



Advertisments