case-insensitive conversion of a class name String to an object

A

Andrew

I have a class 'some.package.ClassName'. I have the string
"some.package.classname", all in lowercase, with no way of knowing how
to restore the correct case to the class name. I need to instantiate
the 'some.package.ClassName' class.

Is there a case-insensitive way to get a object instance from an all
lowercase String containing the fully-qualified class name?

I note that when I try:

Class c = Class.forName("some.package.classname");

it naturally throws a java.lang.NoClassDefFoundError (because of the
case-sensitivity).

The exact error is: java.lang.NoClassDefFoundError:
some/package/classname (wrong name: some/package/ClassName)

With interest, I note that the "wrong name" value also reported with
the exception contains the correct case.

Where does this "wrong name" value come from? How does it restore the
case correctly? (This is exactly what I want to do - I really want the
'wrong name' value from the java.lang.NoClassDefFoundError exception).

Any suggestions?

Thanks,
Andrew
 
J

John C. Bollinger

Andrew said:
I have a class 'some.package.ClassName'. I have the string
"some.package.classname", all in lowercase, with no way of knowing how
to restore the correct case to the class name. I need to instantiate
the 'some.package.ClassName' class.

Tough luck. Why can't you get the correct class name in the first place?
Is there a case-insensitive way to get a object instance from an all
lowercase String containing the fully-qualified class name?

Not reliably. You could try all the case combinations, but that would
take considerable time and might get the wrong answer if you have two
classes (or packages!) whose names differ only in case.
I note that when I try:

Class c = Class.forName("some.package.classname");

it naturally throws a java.lang.NoClassDefFoundError (because of the
case-sensitivity).

The exact error is: java.lang.NoClassDefFoundError:
some/package/classname (wrong name: some/package/ClassName)

With interest, I note that the "wrong name" value also reported with
the exception contains the correct case.

I wouldn't rely on this for correct behavior, but perhaps you could
parse this message and attempt to use the resulting name as a fall-back.
You cannot, however, be certain that you have the correct class if
you do that. You should use the correct name in the first place.
Where does this "wrong name" value come from? How does it restore the
case correctly? (This is exactly what I want to do - I really want the
'wrong name' value from the java.lang.NoClassDefFoundError exception).

Presumably the ClassLoader is does a case-insensitive search of its
classpath, finds a likely match, and reports it to you. It might not be
the only possible match. It might not even be the most likely match.
Any suggestions?

If you really want to do this then one possibility would be to write a
custom ClassLoader (it's not hard) that catches the
NoClassDefFoundError, attempts to parse the message, and if successful,
attempts to load the class by the discovered name. With that said,
however, let me add one more time: IT IS A BAD IDEA.

The better idea is to use the correct class name from the beginning, and
to not attempt to play shenanigans with class name case sensitivity. If
you are using some other program or tool that lowercases the class names
then fix or abandon that tool instead of trying to accommodate its
broken behavior.


John Bollinger
(e-mail address removed)
 
M

Mike Schilling

Andrew said:
I have a class 'some.package.ClassName'. I have the string
"some.package.classname", all in lowercase, with no way of knowing how
to restore the correct case to the class name. I need to instantiate
the 'some.package.ClassName' class.

Is there a case-insensitive way to get a object instance from an all
lowercase String containing the fully-qualified class name?

I note that when I try:

Class c = Class.forName("some.package.classname");

it naturally throws a java.lang.NoClassDefFoundError (because of the
case-sensitivity).

The exact error is: java.lang.NoClassDefFoundError:
some/package/classname (wrong name: some/package/ClassName)

With interest, I note that the "wrong name" value also reported with
the exception contains the correct case.

Where does this "wrong name" value come from? How does it restore the
case correctly? (This is exactly what I want to do - I really want the
'wrong name' value from the java.lang.NoClassDefFoundError exception).

A guess: it finds a file called some/package/classname.class in a
case-insensitive filesystem, interrogates it, and discovers that it contains
the wrong class. If you knew that you were looking for such a file, you
could do the same, but this wouldn't work in a case-sensitive filesystem or
inside a jar file.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top