I have recently come into the need of modifying some Java code (adding methods, changing the signatures of some fields and removing methods) and I think that all of this can be accomplished though the use of the Eclipse SDK's AST.
I know from some research how to parse in a source file but I don't know how to do the things mentioned above. Does anyone know a good tutorial or could someone give me a brief explanation on how to resolve these issues?
Thanks a lot,
ExtremeCoder
Edit:
I have now started to look more into the JCodeModel and I think this could be much easier to use but I do not know if an exististing document can be loaded into it?
If this could work let me know ;)
Thanks again.
I will not post the whole source code to this problem here because it is quite long but I will get people started.
All the docs that you will need are here: http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/package-summary.html
That will create a compilation unit for you from the source code that you pass in.
Now this is a simple function that prints out all of the methods inside the class definitions in what you have passed:
This should get you all started.
It does take some time to get used to this (a lot in my case). But it does work and is the best method I could get my hands on.
Good luck ;)
ExtremeCoder
Edit:
Before I forget, these are the imports that I used to get this working (I took quite a bit of time to get these organized):
Where xxxx represents a version number.
You can do this with Eclipse by calling APIs that let you manipulate the ASTs.
Or you can apply program transformations to achieve your effect in way that doesn't depend on the microscopic details of the AST.
As an example you might write the following program transformation:
to add an integer parameter with an arbitrary name to a parameter list. This achieves the same effect as a whole set of API calls but it is a lot more readable because it is in the surface syntax of your language (in this case, Java).
Our DMS Software Reengineering Toolkit can accept such program transformations and apply them to many languages, including Java.