Open pkernevez opened 7 years ago
Unfortunately, no. There isn't support for rendering an AST or sub-tree back to Java code at the moment.
I don't think this would be too difficult to implement though (some sort of "to_java" method would need to be implemented for each AST node type) and I would be happy to review a pull request to add this if you or anyone else is interested to work on it.
Something like https://github.com/c2nes/javalang/compare/master...kinow:add-to-java @c2nes ?
I am working on a legacy system, that uses Java, JAXB, EJB, and for which the code is a bit too complex, and my IDE is not able to automatically refactor the code as I needed. Your library saved me, in this past week, around 5 hours. I spent 3 hours doing some boring refactoring... then 2 hours more writing a script with javalang, then it took less than 15 minutes for running that for two other parts of the code :-)
I had to write my own logic for re-rendering the nodes (I was filtering out some parts of the code). So I thought I would try to submit a pull request for that. And since @pkernevez requirement is so similar to mine, I thought about using this ticket for that.
Let me know if the design chosen works OK, and if so I will submit a PR for a few nodes that I used (package, compilationunit, import, annotation, ...). Won't have time to submit a complete PR, but then we can leave the PR open, or merge and wait for others to contribute the missing nodes?
Anyway, thanks heaps for the library. You have no idea how much boring-working-hours you saved me :-)
Cheers Bruno
Apologies for not responding sooner. I'm glad to hear that javalang has been helpful!
I think the approach I would take with this would be to first update the parser so that each AST node has a reference to the sequence of tokens which back it. I think this would make it much simpler to implement this sort of rendering/formatting support and would also simplify a full implementation of #14.
Once an AST node has a reference to its tokens it would be quite easy to implement a "default" implementation of something like to_java()
. Overriding implementations could then be added for AST node types where specialized formatting rules are desired.
+1
+1
Is there any implementation of transforming AST to java code? Any Hint?
@kinow Do you have any idea of parsing AST node to source code, I want to parse a MethodDeclaration Node to the source code. From the above description of your comment, you seem to have the implementations, can you share with me? Thanks a lot
Hi @ttbuffey , sorry that was a couple of years ago, and my memory is quite bad. I remembered adding it to some project or gist... I found this one that is using Java reflection:
And this is the repository with some Python code, but I did not include a readme at that time, and would need some minutes to read and try the code to remember what I was doing:
https://github.com/kinow/dork-scripts/blob/master/java/javaentity-dto-splitter/main.py
But I am hoping you will find what you need in one of these two?
Cheers Bruno
Hi @kinow, Thanks for taking your precious time to find the code of couple years ago. As I'm using Pyhon, the second link you provided is very helpful to me. Thanks a lot
Best Regards, ttbuffey
Sr, but can you help me to parse code and explore the AST graph? I don't know how to implement this code. Sorry, this is my topic so I misunderstand. Thank you so much.
Hi,
Thank you for you implementation, it's very easy to parse code and explore the AST graph. I implement some validation rules, for exemple, about catch implementation.
After founding a match I would like to render the subtree as code. Is there an API to do that ?
Regards, Philippe