Reflection Question

Discussion in 'Java' started by Tim, Jun 22, 2004.

  1. Tim

    Tim Guest

    Is there a way to use reflection on an interface to find out what
    classes implement it?

    I am trying to create a concrete implementation of an interface using
    Class.newInstance(), but I only have the Class object for the
    interface at runtime.

    I'm guessing that I can only look up the inheritance hierarchy (i.e.
    getSuperClass(), getInterfaces(), etc.) and not down, but I figure
    it's worth asking.

    Any help would be appreciated. Thanks in advance.

    Tim
     
    Tim, Jun 22, 2004
    #1
    1. Advertising

  2. Tim wrote:
    > Is there a way to use reflection on an interface to find out what
    > classes implement it?


    No, because the JVM can not, at any time, know all classes it potentially
    has access to. The classloader mechanism is so flexible that the JVM
    does not know whether a certain class exists (and what its properties
    are) until it has explicitly tried to load that class via its name.
    There are classlaoders that load classes via HTTP from the internet
    (and don't know whether the class exists until they get a response
    from the webserver), you can even write classloaders that generate
    classes on the fly according to whatever rules you might conceive.
    And any of those classes might or might not implement your interface.
     
    Michael Borgwardt, Jun 22, 2004
    #2
    1. Advertising

  3. Tim

    Adam Maass Guest

    "Michael Borgwardt" <> wrote in message
    news:...
    > Tim wrote:
    > > Is there a way to use reflection on an interface to find out what
    > > classes implement it?

    >
    > No, because the JVM can not, at any time, know all classes it potentially
    > has access to. The classloader mechanism is so flexible that the JVM
    > does not know whether a certain class exists (and what its properties
    > are) until it has explicitly tried to load that class via its name.
    > There are classlaoders that load classes via HTTP from the internet
    > (and don't know whether the class exists until they get a response
    > from the webserver), you can even write classloaders that generate
    > classes on the fly according to whatever rules you might conceive.
    > And any of those classes might or might not implement your interface.


    Though, if you know precisely where the VM might load classes from, and have
    read access to those locations to get a listing of classes, you could
    brute-force load classes looking for those that implement your interface.
    Ugly at best and not guaranteed to work in all situations (as under the more
    esoteric situations above).

    -- Adam Maass
     
    Adam Maass, Jun 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary
    Replies:
    2
    Views:
    5,635
    _nuhb_
    Aug 14, 2003
  2. Ian A. Mason
    Replies:
    2
    Views:
    433
    Chris Uppal
    Nov 9, 2004
  3. Daniel Wang

    reflection question

    Daniel Wang, Nov 26, 2004, in forum: Java
    Replies:
    7
    Views:
    2,282
    Filip Larsen
    Nov 28, 2004
  4. isamura

    Question about Reflection

    isamura, Oct 18, 2005, in forum: Java
    Replies:
    3
    Views:
    431
    isamura
    Oct 18, 2005
  5. Replies:
    3
    Views:
    444
Loading...

Share This Page