Recently, I had to parse Groovy class to extract some information. Reflection was not good as it was not easy to get all dependencies and I had to preserve comments too. This is quite easy to do by utilizing Groovydoc internals which are part of the default libraries:
This is definitely not nicest Groovy code, and looks more like Java, but gets the job done. By the way, it should be possible to parse Java too, though I didn't tried to.
import antlr.collections.AST import org.codehaus.groovy.antlr.AntlrASTProcessor import org.codehaus.groovy.antlr.SourceBuffer import org.codehaus.groovy.antlr.UnicodeEscapingReader import org.codehaus.groovy.antlr.parser.GroovyLexer import org.codehaus.groovy.antlr.parser.GroovyRecognizer import org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal import org.codehaus.groovy.tools.groovydoc.SimpleGroovyClassDoc import org.codehaus.groovy.tools.groovydoc.SimpleGroovyClassDocAssembler def reader = new File("/path/to/package/org/Groovy.groovy").newReader() SourceBuffer sourceBuffer = new SourceBuffer() UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer) GroovyLexer lexer = new GroovyLexer(unicodeReader) unicodeReader.setLexer(lexer) GroovyRecognizer parser = GroovyRecognizer.make(lexer) parser.setSourceBuffer(sourceBuffer) parser.compilationUnit() AST ast = parser.getAST() def visitor = new SimpleGroovyClassDocAssembler("/path/to/package", "org/Groovy.groovy", sourceBuffer, [], new Properties(), true) AntlrASTProcessor traverser = new SourceCodeTraversal(visitor) traverser.process(ast) SimpleGroovyClassDoc doc = (visitor.getGroovyClassDocs().values() as List)[0] doc.methods().each { println it.name() println it.commentText() println it.annotations() }
This is definitely not nicest Groovy code, and looks more like Java, but gets the job done. By the way, it should be possible to parse Java too, though I didn't tried to.
Superb! I was read enough this blog, this is very different kinds of post. Thank you for your great work and keep posting...
ReplyDeletePrimavera Training in Chennai
Primavera Course in Chennai
Pega Training in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Power BI Training in Chennai
Excel Training in Chennai
Oracle Training in Chennai
Social Media Marketing Courses in Chennai
Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai
DeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.
Spring Training in Chennai
The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Thank you sharing this kind of noteworthy information. Nice Post.
ReplyDeleteimedicalassistants
Article submission sites
It is very interesting and i like it. Thank you for the informations.
ReplyDeleteCloud Computing Courses in Coimbatore
Cloud Computing Training in Coimbatore
Cloud Computing Courses
Cloud Training
Cloud Certification
Cloud Computing Certification
Nice article I was really impressed by seeing this blog, it was very interesting and it is very useful for me.
ReplyDeleteWeb Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery