5

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.

5
  • Fyi Documenting Salesforce.com Apex class files.
    – Keith C
    Jul 4, 2015 at 13:45
  • 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, 2015 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, 2015 at 14:47
  • @KeithC You can query classes now.
    – sfdcfox
    Jul 4, 2015 at 18:43
  • 1
    @sfdcfox Good to know. My comment was purely about the parsing part.
    – Keith C
    Jul 4, 2015 at 18:58

2 Answers 2

2

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.

0
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .