1

I have the need of getting the Class from a String.
The string is just the class his name without the package declaration.

While I could use Class.forName(className); this requires me to give the FQN.

Here is just where I have the problem.
I know its base package : be.chillworld.catalog but this package have subpackages.

Example :

  • be.chillworld.catalog.location
  • be.chillworld.catalog.operation

Easiest solution is to remove all the subpackages so I can do the Class.forName() but there goes the nice structure then.

Anyone have an idea of how to get mine specific class?

chillworld
  • 4,207
  • 3
  • 23
  • 50
  • 1
    If you know that is has to be in one of these subpackages (assuming that this is always the case), why don't you try both? – reto Jul 03 '14 at 07:27
  • it's a bit more subpackages then 2 (I minimalize the problem for asking) but it's an idea – chillworld Jul 03 '14 at 07:29
  • You might want to read http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection – reto Jul 03 '14 at 07:31
  • Did read that before, but then I still have to search in the collection of Classes to se witch is the one I want. Your first comment is in mine eye's a better solution. – chillworld Jul 03 '14 at 07:37

1 Answers1

0

Use Reflections Class.Give base package when initiating class and get your specific classes.

Reflections reflections = new Reflections("com.sss.xxx");

reflection.getSubTypesOf(ddd.class)

Nadendla
  • 712
  • 2
  • 7
  • 17