The same question already has answers for fields
and methods
, both suggesting to iterate over the existing members and lower-case comparing the names. But classes
are different in that you can't [easily] iterate over classes in a package.
My java application instantiates objects according to a given input from a 3rd party - non-case-sensitive - application, and it fails when the case is wrong. Since java won't allow classes with same name and different case in one package, I guess there should be a simple way to get a Class
object without having the exact case.
I want to avoid multiple if
/else if
since I add classes to the package all the time.
try
ing all case possibilities is also out of question (since it is O(2^n)).
So, how should I go about it?
Solution:
As suggested in @IReeder's answer (thanks @assylias), I Ignored standard Java conventions and name all your classes lowercase. This way I can always call class.forName with the lower case version of the input.