references on all Java classes within the same package

Discussion in 'Java' started by Peter Parker, Oct 5, 2006.

  1. Peter Parker

    Peter Parker Guest

    Is it possible to get object references on all Java classes within the same
    package without knowing their names? For example, I want to obtain the names
    (hence, object references via Java reflection) of all the classes that are
    contained inside package a.b.c knowing nothing else but the fact that they
    are in that same package.

    import a.b.c.*;

    Is there a way to accomplish this? Thanks
     
    Peter Parker, Oct 5, 2006
    #1
    1. Advertising

  2. Peter Parker wrote:
    > Is it possible to get object references on all Java classes within the same
    > package without knowing their names?


    No. There are specific ways in specific cases, under limited
    circumstances, but no general solution.

    >..For example, I want ..


    What you want is not important, what you want to achieve
    is somewhat more so. What do you want to *achieve*?

    Andrew T.
     
    Andrew Thompson, Oct 5, 2006
    #2
    1. Advertising

  3. There is none. You can get the packages known to the classloader, but
    that is pretty much it. The packages do not have an accessor like
    getClassesInPackage(). The 'Package' does not belong to reflection.

    I did this to check it out...

    Package[] arr = Package.getPackages();
    for(Package item:arr){
    System.out.println(item);
    }

    And I got a list of all packages that the classloder loaded, here is
    the partial output:

    package java.io, Java Platform API Specification, version 1.5
    package javax.swing.undo, Java Platform API Specification, version 1.5
    package java.lang, Java Platform API Specification, version 1.5
    package java.net, Java Platform API Specification, version 1.5
    package javax.sound.sampled, Java Platform API Specification, version
    1.5
    package sun.text, Java Platform API Specification, version 1.5
    package java.text, Java Platform API Specification, version 1.5
    package org.w3c.dom.events, Java Platform API Specification, version
    1.5

    ....
    -cheers,
    Manish
     
    Manish Pandit, Oct 5, 2006
    #3
  4. Peter Parker

    Ravi Guest

    Yes and No. You can use the current classpath to open all Jars and all
    files in directories that match your classpath. For Jars you need to
    use the APIs available like JarFile or JarURLConnection or
    JarInputStream class. The NO is because not all classes are created
    from filesystems. A class generated from Rhino or Groovy or CGLib could
    easily exist in a package and have no relationship to a filesystem at
    all.

    To solve this problem you will want to override a variety of
    classloaders, which would also include the system class loader. This
    requires a special JVM startup command line switch. Once you have the
    system class loader you can detect when other class loaders are
    created. When they are you need to modify their byte code to intercept
    calls to loadClass or defineClass, just to know the names of the
    classes they load. The whole system could get really messy, but for the
    most part after doing all that, you could get all classes in a package.
    Why?

    Peter Parker wrote:
    > Is it possible to get object references on all Java classes within the same
    > package without knowing their names? For example, I want to obtain the names
    > (hence, object references via Java reflection) of all the classes that are
    > contained inside package a.b.c knowing nothing else but the fact that they
    > are in that same package.
    >
    > import a.b.c.*;
    >
    > Is there a way to accomplish this? Thanks
     
    Ravi, Oct 5, 2006
    #4
    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. D Sandher
    Replies:
    3
    Views:
    319
    Tom McGlynn
    Sep 24, 2003
  2. Carfield Yim
    Replies:
    1
    Views:
    1,518
    Andrew Thompson
    May 31, 2004
  3. della
    Replies:
    6
    Views:
    340
    Peter Otten
    Nov 28, 2008
  4. Jack Bates
    Replies:
    3
    Views:
    318
    Steven D'Aprano
    Aug 6, 2011
  5. Krishna Chaitanya
    Replies:
    5
    Views:
    185
    Gunnar Hjalmarsson
    Apr 3, 2009
Loading...

Share This Page