4

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...

  • 1
    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 to return Outer.Inner as the classname. – Phil Hawthorn Jun 4 '13 at 14:47
  • @Phil: Please convert this to an answer so in case nothing else comes I can credit you ;) – Robert Sösemann Jun 4 '13 at 15:21
  • No problems, expanded slightly too – Phil Hawthorn Jun 4 '13 at 16:09
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.