Subclassed ClassLoader problem with array types

B

Benji

I'm writing a specialized classloader by extending SecureClassLoader, and
overwriting findClass. (it looks through a specified directory, among
other things. It rewrites classes as they're loaded in, which is why I
can't use a URLClassLoader). It works fine on classes that are in the
directory, classes in the parent classloader, and array types of classes
in the parent classloader. But the one thing that it can't load is
array types from the current classloader. If I call
loadClass("[[LHelloWorld;"), loadClass ends up calling findClass with the
String "[[LHelloWorld;". Shouldn't loadClass be dealing with figuring
out that it's an array type, and not passing me the [[L garbage?

Is there a way to manually define an array Class from another Class?
 
C

Chris Uppal

Benji said:
But the one thing that it can't load is
array types from the current classloader. If I call
loadClass("[[LHelloWorld;"), loadClass ends up calling findClass with the
String "[[LHelloWorld;". Shouldn't loadClass be dealing with figuring
out that it's an array type, and not passing me the [[L garbage?

I suspect that you are calling your classloader's loadClass() method directly ?
If so, then that is your problem. As far as I know, the only proper way to
load a class is via
Class.forName()
or Class.forName(String, boolean, Classloader)
and findClass() and loadClass() are intended only for use (except within the
Classloader hierarchy) by the JVM itself.

If you use one of the legal forms then the request will be routed via the JVM
which will recognise the array syntax, and translate it appropriately, ask your
loader to:

loadClass("HelloWorld", false).

and will then generate the appropriate array class using the value you return.

-- chris
 

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
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top