Get full class name of an inner class instance?

I have a inner class

public class Outer {
   public class Inner {
      ...
   }
}

and need to get that running

System.assertEquals('Outer.Inner', getClassName(new Outer.Inner()));

I found out that you can get the inner class name, but not the outer class:

System.assertEquals('Inner', String.valueOf(classInstance).substring(0, String.valueOf(classInstance).indexOf(':'));

I could do a SOSL query on all ApexClass’ body but that would be somewhat quirky…

Answer

This also assumes that no-one has overridden the toString method on your Inner class to do something other than what you’re expecting to parse.

If you own the Classes, you might consider overriding the toString() method yourself to return Outer.Inner as the classname.

For example:

   public class Outer {
       public override String toString() {
          return 'Outer';
        }
        public class Inner {
           public override String toString(){
             return 'Outer.Inner';
           }
        }
     }

I asked a similar question a while ago here

Attribution
Source : Link , Question Author : Robert Sösemann , Answer Author : Community

Leave a Comment