Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » JDT HELP
JDT HELP [message #259624] Mon, 20 April 2009 06:02 Go to next message
Kap Pak is currently offline Kap PakFriend
Messages: 52
Registered: July 2009
Member
Hello Guys,

I am stuck in my project and need an urgent help. I have to parse my
Java project(the java packages in the project as well as the java source
files inside the packages) and build a tree from it and show the tree in
an ecclipse view. What am doing is this:

I have some information from a server and i have to compare those results
to the name of the nodes in my project(the ast nodes from the java source
files) and if i find a match, then i must show them(nodes) as a tree in a
view.

My problem now is how i should parse the Java source files. I don't know
how to do it. I have read this tutorial:

http://www.eclipse.org/articles/article.php?file=Article-Jav aCodeManipulation_AST/index.html

but it doesn't give me much idea about the starting point and also got
stuck there. Am asking if someone can help me to do it.

At the moment i have this class:

public class TestingBindings {

protected CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit); // set source
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit)parser.createAST(null /* IProgressMonitor
*/); // parse
}

public ICompilationUnit findProject(){
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// "yourplugins" is a project name
IProject project = root.getProject("yourplugins");
if (project.exists() && !project.isOpen())
try {
project.open(null /* IProgressMonitor */);
} catch (CoreException e1) {
e1.printStackTrace();
}

IJavaProject javaProject = JavaCore.create(project);
IType lwType = null;
try {
//"yplugins" is a package name
"Activator" is a class name
lwType = javaProject.findType("yplugins.Activator");
} catch (JavaModelException e) {

e.printStackTrace();
}
ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit();

return lwCompilationUnit;

}

}


After i created this class, i am just stuck i don't know how to go further
and process the projects and packages and the source files.

IMPORTANT:
I want to be able to parse a project and inside the project all the
packages and inside the packages all the classes

So please help me to understand what i should do and how i should do it. I
have spent much weeks on the web looking for answers but unable to find a
tutorial for a beginner like me. I search the JDT documentation but
couldn't figure out what to use since there are so many methods and also
is not suitable for beginner like me. Please help.

Thanks for all your help.
Re: JDT HELP [message #259629 is a reply to message #259624] Mon, 20 April 2009 07:25 Go to previous messageGo to next message
Romain Dervaux is currently offline Romain DervauxFriend
Messages: 40
Registered: July 2009
Member
Eddy Freeman a écrit :
> Hello Guys,
> I am stuck in my project and need an urgent help. I have to parse my
> Java project(the java packages in the project as well as the java source
> files inside the packages) and build a tree from it and show the tree in
> an ecclipse view. What am doing is this:
> I have some information from a server and i have to compare those
> results to the name of the nodes in my project(the ast nodes from the
> java source files) and if i find a match, then i must show them(nodes)
> as a tree in a view.
>
> My problem now is how i should parse the Java source files. I don't know
> how to do it. I have read this tutorial:
>
> http://www.eclipse.org/articles/article.php?file=Article-Jav aCodeManipulation_AST/index.html
>
>
> but it doesn't give me much idea about the starting point and also got
> stuck there. Am asking if someone can help me to do it.
>
> At the moment i have this class:
>
> public class TestingBindings {
>
> protected CompilationUnit parse(ICompilationUnit unit) {
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setKind(ASTParser.K_COMPILATION_UNIT);
> parser.setSource(unit); // set source
> parser.setResolveBindings(true); // we need bindings later on
> return (CompilationUnit)parser.createAST(null /*
> IProgressMonitor */); // parse
> }
>
> public ICompilationUnit findProject(){
> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
> // "yourplugins" is a project name
> IProject project = root.getProject("yourplugins");
> if (project.exists() && !project.isOpen())
> try {
> project.open(null /* IProgressMonitor */);
> } catch (CoreException e1) {
> e1.printStackTrace();
> }
>
> IJavaProject javaProject = JavaCore.create(project);
> IType lwType = null;
> try {
> //"yplugins" is a package name
> "Activator" is a class name
> lwType = javaProject.findType("yplugins.Activator");
> } catch (JavaModelException e) {
>
> e.printStackTrace();
> }
> ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit();
>
> return lwCompilationUnit;
>
> }
>
> }
>
>
> After i created this class, i am just stuck i don't know how to go
> further and process the projects and packages and the source files.
>
> IMPORTANT:
> I want to be able to parse a project and inside the project all the
> packages and inside the packages all the classes
>
> So please help me to understand what i should do and how i should do it.
> I have spent much weeks on the web looking for answers but unable to
> find a tutorial for a beginner like me. I search the JDT documentation
> but couldn't figure out what to use since there are so many methods and
> also is not suitable for beginner like me. Please help.
>
> Thanks for all your help.
>

Hi

Give a look at the class : org.eclipse.jdt.core.dom.ASTVisitor.

Snippet :

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(cu);
CompilationUnit parsedCompilationUnit = (CompilationUnit)
parser.createAST(null);
parsedCompilationUnit.accept(jdtVisitor);

where jdtVisitor is an instance of a class which extends ASTVisitor.

Hope it helps.
Re: JDT HELP [message #259662 is a reply to message #259624] Tue, 21 April 2009 15:33 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Eddy,

> I am stuck in my project and need an urgent help. I have to parse my
> Java project(the java packages in the project as well as the java source
> files inside the packages) and build a tree from it and show the tree in
> an ecclipse view. What am doing is this:

Assuming this question is about the AST side not the view side.

> [...]

> My problem now is how i should parse the Java source files. I don't know
> how to do it.
> [...]

With "parse the Java source files" you probably mean: "call the ASTParser
to obtain an AST", right?

> but it doesn't give me much idea about the starting point and also got
> stuck there. Am asking if someone can help me to do it.
>
> At the moment i have this class:

So you have a method to find an ICompilationUnit and another one that
converts an ICompilationUnit (source) into a CompilationUnit (parsed AST).

That sounds like you have the solution at hand. So, what is the missing
link here? org.eclipse.jdt.core.dom.CompilationUnit is the root of the
parsed AST. If you have that you need no more parsing.


> protected CompilationUnit parse(ICompilationUnit unit) {
> [...]
> }
>
> public ICompilationUnit findProject(){
> [...]
> }
>
> }
>
>
> After i created this class, i am just stuck i don't know how to go further
> and process the projects and packages and the source files.

If your question is really about parsing, the answer would be
CompilationUnit unit = parse(findProject());

best
Stephan
Previous Topic:Removing an ASTNode without loosing the formatting
Next Topic:Need Help
Goto Forum:
  


Current Time: Wed Jul 17 23:27:14 GMT 2024

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

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

Back to the top