0

Currently I have following method to instantiate objects. I am reading the class name from a file and its in upper case. (I can assume that there won't be many classes like Abc.java, ABC.java or aBc.java and there will be only one class which the name contains a, b, c letters in the given order).

As an example there is a java class named as SchoolPrincipal This class is represented in the file as SCHOOLPRINCIPAL Therefore currently my following method can't find this object due to the uppercase problem and it gives ClassNotFoundException.

I can't rename the java class, it should stays with the standard, and alsoI can't change the word in the file which is in uppercase. Is there a tricky way to solve this problem.

And I have one solution, and want to know the good and bad in this solution, Get all the java classes name and store in a map where the key is all uppercase (key: CLASSTEACHER, Value: ClassTeacher) and when I read the name from file I search it in the map and get the value and pass the className to following method.

public Object getInstance(String packageName, String className)

        String unifiedClassName = packageName + "." + className;
        LOG.info("Creating " + unifiedClassName + " instance...");

        Class<?> clazz = null;
        Constructor<?> ctor = null;
        Object obj = null;

        try {

            clazz = Class.forName(unifiedClassName);
            ctor = clazz.getConstructor();
            obj = ctor.newInstance();

        } catch (InstantiationException e) {
            LOG.error("Error occurred: " + e);
        }
 }
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
  • I think you're approaching your problem from a wrong perspective. Are there that many classes that you can't change their cases by hand? If so, make a simple program that goes through the source folder and generates a new file for you with properly cased file names. – Alex-v-s May 03 '15 at 15:45
  • You can do the `Class.forName` call with all the possible choices. Or loop through the files in that package and find one with the same name. Not sure what you're trying to do, but doesn't sound like a very good concept. – Bubletan May 03 '15 at 15:49
  • @NeoMime The corresponding classes are in one package and there are classes between thousand and five thousands – Isuru Gunawardana May 04 '15 at 01:24
  • @Bubletan , yes thats what I am trying to do. Why it is not a good concept? – Isuru Gunawardana May 04 '15 at 01:28
  • @IsuruGunawardana Well, I mean the reason for doing it this way. I don't know the whole system behind this, but if it's possible, some other way could be better. – Bubletan May 04 '15 at 12:14

1 Answers1

2

Interesting question... I think if you take this approach, the main challenge would be actually getting all available classes in the classpath (IMHO it would still be more efficient than trying all 2^N combinations of names).

Perhaps you're already familiar with classpath scanning, so you omitted it because it was obvious? I was thinking about something like Get all of the Classes in the Classpath, or looking into other 3rd parties that do scanning (e.g. spring or JUnit)

Anyway this would be an interesting thread to follow :)

Community
  • 1
  • 1
Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11