All classes from pkg name & inner class reflection

Discussion in 'Java' started by Jeffy, Sep 10, 2003.

  1. Jeffy

    Jeffy Guest

    Two specific questions:

    First, when given a package name ("java.util"), how can I determine
    that--first of all--the package truly exists in the CLASSPATH, and
    then retrieve all the class names that exist in that package?

    Second, say I have the below Java source code. I need to determine
    the fully-qualified class name for an identifier ("Vector" in this
    case, only the class names, not the variable names), depending on
    where it is found. Are there any parsers existing that can help me
    with this? Is there any way to access the parsing abilities of the
    javac compiler?

    Thank you for any insights :' )

    ---------------
    ---------------
    ---------------
    ---------------
    import java.util.Vector;

    public class MyClass {
    MyClass() {
    Vector vJavaUtil = new Vector();
    System.out.println("cnstr a: " + vJavaUtil.getClass().getName());

    class Vector {
    Vector() {
    }
    }

    Vector vInner = new Vector();
    System.out.println("cnstr b: " + vInner.getClass().getName());
    }

    public void myFunction() {
    Vector vJavaUtil = new Vector();
    System.out.println("mf a: " + vJavaUtil.getClass().getName());

    class Vector {
    Vector() {
    }
    }

    //The inner class Vector
    Vector vInner = new Vector();
    System.out.println("mf b: " + vInner.getClass().getName());
    }

    public static void main(String[] as_cmdLineParams) {
    (new MyClass()).myFunction();
    }
    }
     
    Jeffy, Sep 10, 2003
    #1
    1. Advertising

  2. In article <>,
    Jeffy <> wrote:
    >Two specific questions:
    >
    >First, when given a package name ("java.util"), how can I determine
    >that--first of all--the package truly exists in the CLASSPATH, and
    >then retrieve all the class names that exist in that package?


    You can't.

    The closest you can get is to write your own class loaders and then
    add features to these that will let you traverse those packages
    accessible through them. This still wouldn't help you with the
    built-in class loaders.

    Cheers
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Sep 10, 2003
    #2
    1. Advertising

  3. (Jeffy) writes:
    > First, when given a package name ("java.util"), how can I determine
    > that--first of all--the package truly exists in the CLASSPATH,


    You can't. There is no "package entity" in a classpath. Packages just
    provide the naming prefix for classes.

    > and
    > then retrieve all the class names that exist in that package?


    In general, you can't. There is no list of classes per package in the
    classpath. A classpath points to locations that are used to search for
    a particular class.

    If you happen to know that all your classes of a particular package are
    in a jar, and if you happen to know where the jar is located, you can
    get the list of class files from the jar (see java.util[zip|jar]). With
    that, you can construct class names, and then you can load the
    classes. If the jar is in the class path, you can use the default
    class loader, otherwise you can create a URLClassLoader for loading
    from the jar.

    If you happen to know that all your class files are below a certain
    root directory, and you happen to know where that one is located, you
    can list the matching subdirectory contents. And again, if the
    directory is in the class path, you can load the classes via the
    default class loader. Otherwise you can creat a new one and load via
    that loader.

    > Second, say I have the below Java source code. I need to determine
    > the fully-qualified class name for an identifier ("Vector" in this
    > case, only the class names, not the variable names), depending on
    > where it is found.


    I don't understand your question. You are using getClass().getName(),
    and that delivers the class name. What's wrong with that?

    /Thomas
     
    Thomas Weidenfeller, Sep 10, 2003
    #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. Carfield Yim
    Replies:
    1
    Views:
    1,438
    Andrew Thompson
    May 31, 2004
  2. E11
    Replies:
    1
    Views:
    4,786
    Thomas Weidenfeller
    Oct 12, 2005
  3. Carlo v. Dango
    Replies:
    14
    Views:
    1,037
    Alex Martelli
    Oct 19, 2003
  4. Pyenos
    Replies:
    2
    Views:
    389
    Pyenos
    Dec 27, 2006
  5. ryles
    Replies:
    3
    Views:
    537
    Piet van Oostrum
    Jul 26, 2009
Loading...

Share This Page