Question

I have an existing Java application containing source files that I'd like to modify programatically. I need to be able to do analysis on the source files and based on that analysis execute source transforms. I also need to be able to understand the relationships between different source files. I also would like to create this process as a standalone Java application.

Doing a little research I found the following tools: Code Model & Eclipse JDT. I'm sure there are others. I have excluded Code Model because it (to my knowledge) only allows the generation of new code and will not work with existing code.

The Eclipse JDT library seemed to fit all my needs. I was able to parse source files easily. I could configure binding such that information across types were preserved. The issue that I'm having now is getting the ASTRewrite feature to work. It seems (from the examples and documentation I've read) that it only works from within an eclipse plugin. When I try to use the rewriter I get the following error:

java.lang.IllegalArgumentException: This API can only be used if the AST is created from a compilation unit or class file 

So can I use the ASTRewrite feature from a standalone application or do I need to start looking at a different tool?

Was it helpful?

Solution 2

I did not complete the project but I put together a proof-of-concept tool that used:

  1. TattleTale for class dependencies.
  2. JavaParser for the analysis of the code.

I was particularly impressed with JavaParser and TattleTale did what I needed it to do. JavaParser is certainly capable of changing the code on the fly.

Obviously you will need both the jars and the sources as TattleTale works on the jars and JavaParser works with the source.

OTHER TIPS

Since you haven't provided any code I can't give you a definitive answer, but ASTRewrite does work even if it is used outside the Eclipse "ecosystem". Most likely you are using the wrong method to apply the changes on the AST. In order to make it work, do the following things

Read the whole java file into a string and create a org.eclipse.jface.text.Document from it

final String source = FileUtils.readFileToString(javaSRC);
org.eclipse.jface.text.Document document = new org.eclipse.jface.text.Document(source);

Now you can create an instance of the ASTParser and ASTRewriter and setting the source of the parser the document you just created.

ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(document.get().toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
ASTRewrite rewriter = ASTRewrite.create(cu.getAST());   

cu.recordModifications();

After you have finished with all the modifications, call the proper rewrite method which is : public TextEdit rewriteAST(IDocument document,Map options)

After this you can apply the changes and (if you wish) write back the changes to the file as well.

TextEdit edits = rewriter.rewriteAST(document,null);
edits.apply(document);
FileUtils.write(javaSRC, document.get());

Btw: I have tested this code with the following jars:

org.eclipse.osgi_3.8.2.v20130124-134944.jar

org.eclipse.jdt.core_3.8.3.v20130121-145325.jar

org.eclipse.equinox.preferences_3.5.1.v20121031-182809.jar

org.eclipse.equinox.common_3.6.100.v20120522-1841.jar

org.eclipse.core.runtime_3.8.0.v20120912-155025.jar

org.eclipse.core.resources_3.8.1.v20121114-124432.jar

org.eclipse.core.jobs_3.5.300.v20120912-155018.jar

org.eclipse.core.contenttype_3.4.200.v20120523-2004.jar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top