Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Analyzing Compilation Unit through a java application
Analyzing Compilation Unit through a java application [message #918129] Thu, 20 September 2012 19:21 Go to next message
Waths B. is currently offline Waths B.
Messages: 6
Registered: January 2012
Junior Member
Hi,

I want to read a java source file to collect all the terms it contains and there bindings to create a vocabulary for them.
E.g.
Reading following line in a source file:
private void term(String term)
So I should collect 4 words (1 - private, 1 - void, 2 - term, 1 - String) and there AST Node bindings.

So I wrote the following code for that:
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = new Path(ReadTextInput());
IFile file = workspace.getRoot().getFile(path);
JParser parser = new JParser(JavaCore.createCompilationUnitFrom(file));
CompilationUnit compUnit = parser.getCompilationUnit();
TermVisitor visitor = new TermVisitor();
compUnit.accept(visitor);


But when I'm running the application it ways that the workspace is closed ( java.lang.IllegalStateException: Workspace is closed.) So I think that is because I haven't start any workspace yet.

But I don't want to run this analysis as an Eclipse application which opens a new workspace in a separate window. I need to do this without opening any other workspace windows.

So now I'm thinking of creating a new workspace dynamically through the program, create a new project in that workspace and add the source file to that project. Then start the workspace and analyze its compilation unit.

It's really helpful if someone can tell me whether this is possible to implement and if not any suggestion to achieve this task is highly appreciated.

Thanks you.
Re: Analyzing Compilation Unit through a java application [message #918177 is a reply to message #918129] Thu, 20 September 2012 20:29 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan Herrmann
Messages: 1020
Registered: July 2009
Senior Member
Please don't confuse Workspace (resources, as an abstraction over folders and files) with Workbench (the UI of the IDE).

What we don't know: how is your code launched: is it a standalone application, or part of an Eclipse plug-in you're developing?
In a running instance of Eclipse getWorkspace() should obviously work.
Re: Analyzing Compilation Unit through a java application [message #918262 is a reply to message #918177] Thu, 20 September 2012 22:16 Go to previous messageGo to next message
Waths B. is currently offline Waths B.
Messages: 6
Registered: January 2012
Junior Member
It's a plug-in project which uses Crystal framework to create AST visitor.
Re: Analyzing Compilation Unit through a java application [message #920835 is a reply to message #918262] Sun, 23 September 2012 14:02 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan Herrmann
Messages: 1020
Registered: July 2009
Senior Member
Waths B. wrote on Fri, 21 September 2012 00:16
It's a plug-in project ...


So it should have access to an open workspace.

Quote:
But I don't want to run this analysis as an Eclipse application...


Well, how is an Eclipse plug-in supposed to work if it doesn't live within an Eclipse application?

Perhaps you are confusing plug-in development (using a development workbench and a runtime workbench) with the way a deployed plug-in works (only one workbench)?

Re: Analyzing Compilation Unit through a java application [message #921924 is a reply to message #920835] Mon, 24 September 2012 14:59 Go to previous messageGo to next message
Waths B. is currently offline Waths B.
Messages: 6
Registered: January 2012
Junior Member
Thanks for the replies Stephan, I really appreciate your effort to help me. I think my question was not clear. Sorry about that.
Anyway I found a way to create the compilation unit from a source file path by using the following sample code:

char[]
source = ...;
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source);
CompilationUnit result = (CompilationUnit) parser.createAST(null);


Even though the bindings of AST nodes are null in this way, it still works for me to create the vocabulary.
Re: Analyzing Compilation Unit through a java application [message #986374 is a reply to message #921924] Tue, 20 November 2012 09:30 Go to previous message
Mitesh Aegis is currently offline Mitesh Aegis
Messages: 5
Registered: November 2012
Junior Member
Hi,

You need more help about that so you are welcome. I am developer.

Thansk!


More info:- www.programmingpeopleinc.com
Previous Topic:Custom editor for ".class"
Next Topic:Ctrl Shift G not working
Goto Forum:
  


Current Time: Mon Oct 20 11:30:05 GMT 2014

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

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