How to get classes from a pacakge?

J

Jerry

Hi All,

If I have a package, how can I get all the classes from the package?

For example,

Package pak = Package.getPackage ("packageName");
//? Class[] classes = pak.getClasses ??// need to get all the classes
from the package.

Thanks.
Jerry
 
B

Bent C Dalager

Hi All,

If I have a package, how can I get all the classes from the package?

For example,

Package pak = Package.getPackage ("packageName");
//? Class[] classes = pak.getClasses ??// need to get all the classes
from the package.

You can only do something like this if you have a lot of information
about how your ClassLoader works, and then you usually have to do it
manually.

Usually (and in the general case), it's not possible to do what you
want.

To demonstrate the impossibility in the general case, consider a
URLClassLoader that is using HTTP to look up class definitions. You
can do this by writing something like
ClassLoader loader = new URLClassLoader("http://what.not.com/my/classes");
and using that loader to load classes. While the HTTP protocol allows
you to retrieve named files easily, it does not specify a protocol for
listing files and so you can have no idea which files exist in the
target location other than by specifically trying to load each
one. Needless to say, this is not a useful way of obtaining a file
list.

Cheers
Bent D
 
D

David Postill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

| "Jerry" wrote:
|
| > If I have a package, how can I get all the classes from the package?
|
| You can't.

You can.

A simple extension to the code I posted a few days ago to c.l.j.help
in the message "How to find a location of a class?" will do just that.

The code at the moment, when run with no command line arguments will print
the name of every class found in the classpath in the form:

e:\apps\j2sdk1.4.2\jre\lib\rt.jar(com/sun/accessibility/internal/resources/accessibility.class)
e:\apps\j2sdk1.4.2\jre\lib\rt.jar(com/sun/accessibility/internal/resources/accessibility_de.class)
e:\apps\j2sdk1.4.2\jre\lib\rt.jar(com/sun/accessibility/internal/resources/accessibility_en.class)

etc.

I'm sure a competent Java programmer could modify the posted code to only return classes with
a specified package name...

Regards,

<davidp />

- --
David Postill

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.3 - not licensed for commercial use: www.pgp.com
Comment: Get key from pgpkeys.mit.edu:11370

iQA/AwUBP8ND6nxp7q1nhFwUEQI+agCfa3+NiqKvZqtJisa3L3W0ARc7HTkAn0Yj
qryxQ24kwGPyuEYAI+oTduxv
=7CEL
-----END PGP SIGNATURE-----
 
J

Jezuch

Uz.ytkownik David Postill napisa?:
| You can't.

You can.

What about dynamic class loading?
--
Ecce Jezuch
"Beginning of this world swelled with mankind
Who built a tool to kill a fool and lead the blind
Invent a rule to keep you down and call it a sin
Then stand behind the wall and watch the games begin" - P. Keenan
 
B

Brad BARCLAY

David said:
| You can't.

You can.

A simple extension to the code I posted a few days ago to c.l.j.help
in the message "How to find a location of a class?" will do just that.

No, you can't. Your code fails as soon as you need to inspect classes
that live in a remote network location (such as through a
URLClassLoader), or in a DBMS.

As such, your solution doesn't work in the general case -- it works in
just one specific case. If ones needs fit into this specific case it's
a good tool to have, but in general, what the OP wants to do can't be done.

Brad BARCLAY
 
J

Jeffrey Palm

Jerry said:
Hi All,

If I have a package, how can I get all the classes from the package?

For example,

Package pak = Package.getPackage ("packageName");
//? Class[] classes = pak.getClasses ??// need to get all the classes
from the package.

Thanks.
Jerry

Here's a little code for it:

public List allClasses() throws Exception {
createClassLoader();
List classes = Util.newList();
for (Iterator it = paths.iterator(); it.hasNext();) {
final File path = (File)it.next();
if (path.isDirectory()) {
Collection classFiles = IO.findFiles(path, ".class");
for (Iterator jt = classFiles.iterator(); jt.hasNext();) {
File classFile = (File)jt.next();
String className = classFile.getName();
int iclass = className.indexOf(".class");
className = className.substring(0, iclass);
for (File trav = classFile.getParentFile();
trav != null && !trav.equals(path);
trav = trav.getParentFile()) {
className = trav.getName() + "." + className;
}
Class c = klass(path, className);
maybeAdd(classes, c);
}
} else if (isJarFile(path)) {
try {
JarFile jarFile = new JarFile(path);
for (Enumeration e = jarFile.entries();
e.hasMoreElements();) {
JarEntry jarEntry = (JarEntry)e.nextElement();
String jarEntryName = jarEntry.getName();
if (jarEntryName.endsWith(".class")) {
String className = jarEntryName.replace('/', '.');
int iclass = className.indexOf(".class");
className = className.substring(0, iclass);
Class c = klass(path, className);
maybeAdd(classes, c);
}
}
} catch (Exception e) {
handle(e,"Trouble with jar " + path);
}
}
}
return classes;
}

private void createClassLoader() {
//
// If already created just return
//
if (loader != null) {
return;
}
if (useSystemClasspath) {
paths.addAll(separate(System.getProperty("sun.boot.class.path")));
}
if (useJavaClasspath) {
paths.addAll(separate(System.getProperty("java.class.path")));
}
loader = new URLClassLoader(urls(this.paths), null);
}
 
T

Tor Iver Wilhelmsen

If I have a package, how can I get all the classes from the package?

For each element of System.getProperty("java.class.path") do:

if it's a directory, try File.list() on the directory plus package
as a path, look for .class files.

If it's a jar/zip, open it with java.util.ZipFile and navigate to
the package and list the entries, again looking for .class entries.

You may also need to examine the bootclasspath.

But I guess someone already wrote such code so take a Google.
 
D

David Postill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

GMT, Brad BARCLAY

| David Postill wrote:
|
| > | You can't.
| >
| > You can.
| >
| > A simple extension to the code I posted a few days ago to c.l.j.help
| > in the message "How to find a location of a class?" will do just that.
|
| No, you can't. Your code fails as soon as you need to inspect classes
| that live in a remote network location (such as through a
| URLClassLoader), or in a DBMS.

Surely code can be written to handle these cases too?

| As such, your solution doesn't work in the general case -- it works in
| just one specific case. If ones needs fit into this specific case it's
| a good tool to have, but in general, what the OP wants to do can't be done.

Agreed that there is no built-in general solution ...

I will give the above two cases some thought. I /hate/ to be beaten by technical
problems ... ;)

<davidp />

- --
David Postill

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.3 - not licensed for commercial use: www.pgp.com
Comment: Get key from pgpkeys.mit.edu:11370

iQA/AwUBP8OdJHxp7q1nhFwUEQJWjQCfUnKUbqU52hfYN7OytIbK4RWYTX4AoJeO
FV6v20pUdR2YjTO2RG3gnHFm
=3CKR
-----END PGP SIGNATURE-----
 
D

David Postill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

| (e-mail address removed) (Jerry) writes:
|
| > If I have a package, how can I get all the classes from the package?
|
| For each element of System.getProperty("java.class.path") do:
|
| if it's a directory, try File.list() on the directory plus package
| as a path, look for .class files.
|
| If it's a jar/zip, open it with java.util.ZipFile and navigate to
| the package and list the entries, again looking for .class entries.
|
| You may also need to examine the bootclasspath.
|
| But I guess someone already wrote such code so take a Google.

I did (write such code), but take a look at Bard's posting in this thread ... :(

<davidp />

- --
David Postill

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.3 - not licensed for commercial use: www.pgp.com
Comment: Get key from pgpkeys.mit.edu:11370

iQA/AwUBP8OdjXxp7q1nhFwUEQJW0gCgl0UK2o26GpS/j/s3HNbULjWVI78AoN1H
tineXFzjmf+DzMxW5go6Ocxc
=zF+s
-----END PGP SIGNATURE-----
 
M

Michael Borgwardt

David said:
| No, you can't. Your code fails as soon as you need to inspect classes
| that live in a remote network location (such as through a
| URLClassLoader), or in a DBMS.

Surely code can be written to handle these cases too?

No, because there is not necessarily a way to get an enumeration of all classes that
such a classloader will produce. Your suggestions assumes a lot of knowledge about
how the standard classloader works and uses it to bypass it and get such an enumeration.

But the flexibility of the classloader mechanism makes it fundamentally impossible to
find all classes that belong to a package. One could easily write a classloader that
will produce a class for any fully qualified name in existence.
 
B

Brad BARCLAY

David said:
Agreed that there is no built-in general solution ...

I will give the above two cases some thought. I /hate/ to be beaten by technical
problems ... ;)

That's the attitude! Unfortunately, you're bound to fail :p. I've
spent over 5 years with this problem in the back of my head, so far to
no avail.

But if you do come up with a solution, you have an open invite to join
my Open Source jSyncManager Project (http://www.jsyncmanager.org) to
implement it -- it would save us from having to keep property files for
both of our applications plug-in classtypes :).

Brad BARCLAY
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top