I want to create something like a Java doc but for apex. So it would parse through classes, pages, components, etc and pull out things like soql and sosl statements, classes it relies upon, objects it relies on, etc.

Is this possible? I looked around and so far I can't seem to find a way to get the body (actual code) from any of these items in a class.

Any thoughts would be appreciated.

  • That's a good alternative but I was hoping to have it 100% in salesforce and not need to download the source files and such. Thanks for the alternative though
    – Grady D
    Jul 4 '15 at 13:49
  • 1
    Most parsers are written on top of tools such as Antlr which don't exist in Apex. And if you intend to parse multiple files you are at risk of hitting the CPU governor limit. Staying in Apex makes the job much harder and it's hard to start with. Also AFAIK Salesforce have not published a grammar for Apex or SOQL. You may be underestimating how big the job would be.
    – Keith C
    Jul 4 '15 at 14:47
  • @KeithC You can query classes now.
    – sfdcfox
    Jul 4 '15 at 18:43
  • @sfdcfox Good to know. My comment was purely about the parsing part.
    – Keith C
    Jul 4 '15 at 18:58

You'll want a batch process to parse the classes, because of the potential of hitting CPU limits. You can start your batch off like this:

public ApexClass[] start(Database.BatchableContext context) {
    return [SELECT Body FROM ApexClass];

You can't retrieve the body from protected classes (those that are managed and not global). Parsing Apex Code would probably be non-trivial, but it certainly is possible.


How about Apex Metadata API? You could use it to retrieve all the components and classes' bodies. Then the only thing left is to parse it and get what you're interested in. I would go with some remote service and use flex/bison, or perhaps some more primitive approach would fit 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.