Why does Class.forName fail with nested classes?

D

Duane Evenson

Why does Class.forName() fail with nested classes?

Class.forName("javax.swing.text.html.HTMLEditorKit.Parser")
produces a ClassNotFoundException exception?

How do I make a nested class string into a Class?
 
R

Roedy Green

Class.forName("javax.swing.text.html.HTMLEditorKit.Parser")
produces a ClassNotFoundException exception?

How do I make a nested class string into a Class?

Here is a strategy to crack the problem. Please report back what you
find about inner and static nested classes.

Allocate some class objects and use Class.getName to number the name
of the class. See what happens when you feed that into Class.forName.

However, an Inner class class object can only exist in the context of
a mother class object. There does not appear to be an instance
version of Class.forName so I don't know if what you want to do is
possible.
 
T

Tom Anderson

Why does Class.forName() fail with nested classes?

Class.forName("javax.swing.text.html.HTMLEditorKit.Parser")
produces a ClassNotFoundException exception?

How do I make a nested class string into a Class?

In practice, that will be javax.swing.text.html.HTMLEditorKit$Parser - any
dots which indicate membership of an enclosing class rather than a package
become dollar signs.

Caveat: i don't believe this is part of a specification, it's just how
javac does it, which means it can't necessarily be relied on in all
versions of java.

tom
 
R

Roger Lindsjö

Duane said:
Why does Class.forName() fail with nested classes?

Class.forName("javax.swing.text.html.HTMLEditorKit.Parser")
produces a ClassNotFoundException exception?

How do I make a nested class string into a Class?

I think hte actual name of the class is
javax.swing.text.html.HTMLEditorKit$Parser, so try
Class.forName("javax.swing.text.html.HTMLEditorKit$Parser")
instead.
 
J

Joshua Cranmer

Tom said:
Caveat: i don't believe this is part of a specification, it's just how
javac does it, which means it can't necessarily be relied on in all
versions of java.

The Javadocs for Class.getName clearly state that it returns the binary
name; finding the binary name in the JLS is a bit difficult, but you
should get to §13.1 "The Form of a Binary":

* The binary name of a top-level type is its canonical name (§6.7).
* The binary name of a member type consists of the binary name of
its immediately enclosing type, followed by $, followed by the simple
name of the member.

(More complex portions elided for brevity).

<http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909>
is the full list, for the interested. The last three I did not know.
 
T

Tom Anderson

f>> versions of java.

The Javadocs for Class.getName clearly state that it returns the binary name;
finding the binary name in the JLS is a bit difficult, but you should get to
§13.1 "The Form of a Binary":

* The binary name of a top-level type is its canonical name (§6.7).
* The binary name of a member type consists of the binary name of its
immediately enclosing type, followed by $, followed by the simple name of the
member.

Aha! I stand corrected, many thanks.

tom
 
T

Tom Anderson

Duane Evenson said :



They asked about nested classes; they didn't specify if it was an inner
class.

And whether it is or not makes no difference to how you classload it.

It may make a difference to how you instantiate it, but ISTR that for
inner classes, the constructor just takes an additional parameter (which
comes first, i think) of a pointer to the enclosing instance. Again,
there's no magic.

tom
 
D

Duane Evenson

I think hte actual name of the class is
javax.swing.text.html.HTMLEditorKit$Parser, so try
Class.forName("javax.swing.text.html.HTMLEditorKit$Parser")
instead.

Thanks everyone, you helped.
For the archives, if anyone has a similar problem and wants to go on and
print the simple class and enclosing class without the package info,
here's my code snippet. I had several classes in an array of classes.

-----------------

// build simple class name array for class list
simpleClasses = new String[classes.size()];
for (int x = 0; x < classes.size(); x++) {
simpleClasses[x] = classes.get(x).getSimpleName();
Class cl = classes.get(x); // add enclosing classes to simple name
while ((cl = cl.getEnclosingClass()) != null) {
simpleClasses[x] = cl.getSimpleName() + "." + simpleClasses[x];
}
}

-----------------
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top