Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Parsing .java files in unit tests?
Parsing .java files in unit tests? [message #517751] Mon, 01 March 2010 17:28 Go to next message
Eclipse UserFriend
Hi,

I`m developing an eclipse plugin that has to read in .java files from an opened project and performs actions on it using jdt.

In the source code the parsing looks something like this:


private static ASTNode parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);

parser.setKind(ASTParser.K_COMPILATION_UNIT);

parser.setSource(unit);
parser.setResolveBindings(true);

return parser.createAST(null); // parse
}

now i need to write unit tests. I read in a ".java" file (text file) and create a char[] from it and put it in ASTParser.setSource(char[] source) .

But how do I correctly set up ASTParser.setUnitName(String unitName) and ASTParser.setProject(IJavaProject project) to get the correct bindings? Especially I don't know how to set up the needed IJavaProject.

Thanks for your help!
phil
Re: Parsing .java files in unit tests? [message #517911 is a reply to message #517751] Tue, 02 March 2010 09:03 Go to previous messageGo to next message
Eclipse UserFriend
What version of Eclipse are you using ?
Re: Parsing .java files in unit tests? [message #517913 is a reply to message #517751] Tue, 02 March 2010 09:06 Go to previous messageGo to next message
Eclipse UserFriend
thanks for your reply!

I solved my problem by using an approach similar to that one:
http://www.koders.com/java/fid32F1214F9795BE048B6E2FF5FEE0B2 91486F198F.aspx?s=IPackageFragmentRoot#L20

best regards
phil

[Updated on: Tue, 02 March 2010 09:06] by Moderator

Re: Parsing .java files in unit tests? [message #517987 is a reply to message #517913] Tue, 02 March 2010 11:33 Go to previous message
Eclipse UserFriend
My point was that if you use an ICompilationUnit for setSource(..), you don't actually need to set up a java project as the project in which the compilation unit is defined will be used.
If you want to use setSource(char[]), then in latest I-build (3.6 stream), you can use the setEnvironment(..) method to set up an environment to resolve bindings without the need of a java project.

Glad to know that you fixed it with a different approach.

Olivier
Previous Topic:Continuosly window limits paints when resizing
Next Topic:Validation and exclusion groups
Goto Forum:
  


Current Time: Mon Mar 17 02:02:20 EDT 2025

Powered by FUDForum. Page generated in 0.03638 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top