All classes from pkg name & inner class reflection

J

Jeffy

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();
}
}
 
B

Bent C Dalager

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
 
T

Thomas Weidenfeller

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top