Using Eclipse AST

Recently I came up with the need to change Java code (add methods, change the signature of some fields and delete methods), and I think that all this can be done using the AST Eclipse SDK.

I know from some research how to parse the source file, but I don't know how to do it. Does anyone know a good tutorial or can someone give me a brief explanation of how to solve these problems?

Many thanks,

Extremecoder


Edit:

Now I started to look more at JCodeModel, and I think it can be much easier to use, but I don’t know if an existing document can be loaded?

If this may work, let me know;)

Thanks again.

+6
java eclipse abstract-syntax-tree
source share
2 answers

I will not post all the source code for this problem here because it is quite long, but I will get people to start.

All the documents 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

Document document = new Document("import java.util.List;\n\nclass X\n{\n\n\tpublic void deleteme()\n\t{\n\t}\n\n}\n"); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); CompilationUnit cu = (CompilationUnit)parser.createAST(null); cu.recordModifications(); 

This will create a compiler for you from the source code you are passing.

Now this is a simple function that outputs all the methods inside the class definitions in what you passed:

 List<AbstractTypeDeclaration> types = cu.types(); for(AbstractTypeDeclaration type : types) { if(type.getNodeType() == ASTNode.TYPE_DECLARATION) { // Class def found List<BodyDeclaration> bodies = type.bodyDeclarations(); for(BodyDeclaration body : bodies) { if(body.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration method = (MethodDeclaration)body; System.out.println("method declaration: "); System.out.println("name: " + method.getName().getFullyQualifiedName()); System.out.println("modifiers: " + method.getModifiers()); System.out.println("return type: " + method.getReturnType2().toString()); } } } } 

That should make you all start.

It will take some time to get used to this (a lot in my case). But it works, and this is the best way I could handle it.

Good luck;)

Extremecoder


Edit:

Before I forget, this is the import I used to get this working (I spent quite a bit of time organizing them):

 org.eclipse.jdt.core_xxxx.jar org.eclipse.core.resources_xxxx.jar org.eclipse.core.jobs_xxxx.jar org.eclipse.core.runtime_xxxx.jar org.eclipse.core.contenttype_xxxx.jar org.eclipse.equinox.common_xxxx.jar org.eclipse.equinox.preferences_xxxx.jar org.eclipse.osgi_xxxx.jar org.eclipse.text_xxxx.jar 

Where xxxx represents the version number.

+4
source share

You can do this with Eclipse by calling APIs that allow you to manage AST.

Or you can apply software transformations to achieve your effect so that it does not depend on the microscopic details of AST.

As an example, you can write the following program conversion:

 add_int_parameter(p:parameter_list, i: identifier): parameters -> parameters " \p " -> " \p , int \i"; 

add an integer parameter with an arbitrary name to the parameter list. This provides the same effect as a whole set of API calls, but it is much more readable because it is in the surface syntax of your language (in this case, Java).

Our DMS Software Reengineering Toolkit can accept such software transformations and apply them to many languages, including Java.

+1
source share

All Articles