The reflection classes and methods as well as class loaders etc. need the so called "binary" names of classes to work with.
The question is, how does one get the binary name if one only has the fully qualified name, i.e. the name that one would use in source code.
For example:
package frege;
public static class RT {
....
public static class X { .... }
}
The fully qualified name of the class would be frege.RT.X
. Yet, to get the class object, one needs to write:
Class.forName("frege.RT$X")
and not
Class.forName("frege.RT.X") // fails with ClassNotFoundException
because X
happens to be an inner class of frege.RT
.
A possible, but clumsy, solution would be to replace .
with $
from backwards, one by one, until Class.forName()
doesn't throw ClassNotFoundException
anymore or there are no more .
to replace.
Is there any better/well known/standard solution? I looked in the API docs for Class
, CLassLoader
and java.lang.reflect
but did not find anything usable.
I think its a safe bet that canonical names specify unique classes. As mentioned above javac will not let you create two classes with the same canonical name from withen a single compilation unit. If you have 2 compilations then you can get into trouble regarding which class you load, but at that point I'd be more worried about a library's package name colliding with your package names, which is avoided by all but the malicious.
For this reason, I think its a safe bet to assume you wont run into that scenario. Along those lines, for those who are interested, I implemented the OP's suggestion (flipping
$
s to.
s), and simply throwing aClassNotFoundException
in the event that it doesn't find any classes with that canonical name, or if it finds two or more that have that name.it still annoys me greatly that the "expected" flow is to hit that
catch(NoClass) continue
code, but if you've ever told eclipse or intelliJ to auto-break on any exceptions thrown, you'll know this kind of behavior is par for the course.It now sounds like you want to get the fully qualified name (FQN) from the canonical name. As that is different from working from a simple name I'll add a second answer.
The Sun javac command will not compile a classes if a canonical name conflict would result. However by compiling separately you can still get two different classes with the same canonical name.
An example:
File src1\com\stack\Test.java
File src2\com\stack\Test\Example\Cow\Hoof.java
Then to compile and execute:
Producing the output:
Thus two classes have the same canonical name but different FQNs. Even if two classes have the same FQN and same canonical name, they can still be different if they are loaded via different class loaders.
To resolve your issue I see several ways forward you could take.
First you can specify that you match the class with the least amount of nesting and hence the least number of '$'s in the FQN. UPDATE It turns out Sun javac does the exact opposite of this and matches the class with the most nesting.
Second you can test all possible FQNs and throw an exception if there is more than one.
Third, accept that the only unique mapping is with the FQN then only within a specified class loader and re-work you application appropriately. I find it convenient to use the thread context class loader as a default class loader.
A simple name omits a lot of information and it is possible to have many classes with the same simple name. That may make this impossible. For example:
This produces the output:
As you can see, all three local classes have the same simple name.