Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » -> Some words about AST
-> Some words about AST [message #145080] Tue, 24 February 2004 15:45 Go to next message
Eclipse UserFriend
Hi there,

I am fairly new to AST but not to the eclipse/plugin stuff.
I read some books about eclipse+plugin but the coverage of
AST is - well - only a small overview.

The senario I stand infront of: Having an IMethod or an IType,
how can I get an ASTNode for it?
(MethodDeclaration/TypeDeclaration)
Do I have to parse the enclosing CompilationUnit? What can
I do to reducec footprint on that kind of operations?
Any best practice (or antipattern) advices?


Thanks,

Martin (Kersten)
Re: -> Some words about AST [message #145093 is a reply to message #145080] Tue, 24 February 2004 15:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: newsserver_mails.bodden.de

Once upon a time <c1gctg$9ql$1@eclipse.org>,
Martin Kersten <Martin.Kersten@student.uni-magdeburg.de> enriched the world
with the following:

> Hi there,
>
> I am fairly new to AST but not to the eclipse/plugin stuff.
> I read some books about eclipse+plugin but the coverage of
> AST is - well - only a small overview.
An AST is a very general concept so I assume you will not find much
eclipse-specific literature about it.

> The senario I stand infront of: Having an IMethod or an IType,
> how can I get an ASTNode for it?
> (MethodDeclaration/TypeDeclaration)
> Do I have to parse the enclosing CompilationUnit?
That would be the usual approach.

> What can
> I do to reducec footprint on that kind of operations?
> Any best practice (or antipattern) advices?
No ideas, sorry.

Eric

--
------------------------------------------------------------ -----
Eric Bodden
ICQ UIN: 12656220
Website: http://www.bodden.de
PGP key available
Re: -> Some words about AST [message #145099 is a reply to message #145093] Tue, 24 February 2004 16:06 Go to previous messageGo to next message
Eclipse UserFriend
Hi Eric,

> > I am fairly new to AST but not to the eclipse/plugin stuff.
> > I read some books about eclipse+plugin but the coverage of
> > AST is - well - only a small overview.
> An AST is a very general concept so I assume you will not find much
> eclipse-specific literature about it.
Well I don't have problems on Source Trees and the normal compiler
stuff. (Was some of my main subjects). The problem is the handling and
using of Eclipse's AST implementation. How to do guides and stuff.
(BTW: I can't find the code snippets anymore... can someone give me
the link?)

>
> > The senario I stand infront of: Having an IMethod or an IType,
> > how can I get an ASTNode for it?
> > (MethodDeclaration/TypeDeclaration)
> > Do I have to parse the enclosing CompilationUnit?
> That would be the usual approach.
But I dont know how to find the corresponding ASTNode for
the IMethod object. Sounds like an overkill to me.
Any snippets available?


Thanks,

Martin (Kersten)
Re: -> Some words about AST [message #145104 is a reply to message #145099] Tue, 24 February 2004 16:43 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: aks.terma.com

> Any snippets available?

just been there - newbie though:

IMethod method = xxx;
ICompilationUnit icompilationUnit= method.getCompilationUnit();
CompilationUnit compilationUnit = AST.parseCompilationUnit(
icompilationUnit, true, null, null);
MyASTVisitor myASTVisitor = new MyASTVisitor();
compilationUnit.accept(myASTVisitor);

The MyASTVisitor extends ASTVisitor, which in turn can overload
visit(MethodDeclaration node) or visit(TypeDeclaration node)
/aksel
Re: -> Some words about AST [message #145116 is a reply to message #145104] Tue, 24 February 2004 17:24 Go to previous messageGo to next message
Eclipse UserFriend
Hi aksel,

> > Any snippets available?
>
> just been there - newbie though:
>
> IMethod method = xxx;
> ICompilationUnit icompilationUnit= method.getCompilationUnit();
> CompilationUnit compilationUnit = AST.parseCompilationUnit(
> icompilationUnit, true, null, null);
> MyASTVisitor myASTVisitor = new MyASTVisitor();
> compilationUnit.accept(myASTVisitor);
>
> The MyASTVisitor extends ASTVisitor, which in turn can overload
> visit(MethodDeclaration node) or visit(TypeDeclaration node)

I will copy that snippet. I found another usefull way. Using a scanner...

String source=IMethod.getSource();
IScanner scanner=ToolFactory.createScanner(false,false,false,false);
scanner.setSource(source.toCharArray());

boolean isEOF=false;
while(!isEOF) {
int kind=scanner.getNextToken();
switch(kind) {
case ITerminalSymbols.TokenNameXXX:
...
break;
case ITerminalSymbols.TokenNameEOF:
case ITerminalSymbols.TokenNameERROR:
isEOF=true;
break;
}
}

Works well and there is not such overhead involved like parsing
a whole compilation unit. But why the hack didn't they implemented
the scanner using an iteraton pattern. Something like hasNext and
next.

Dont know. Maybe I will write a fascade or does someone knows?


Thanks aksel,

Martin (Kersten)
Re: -> Some words about AST [message #145120 is a reply to message #145080] Tue, 24 February 2004 18:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: akiezun.cuthis.mit.edu.andthis

look at
org.eclipse.jdt.internal.corext.refactoring.structure.ASTNod eSearchUtil
getMethodDeclarationNode
a.
Re: -> Some words about AST [message #145138 is a reply to message #145120] Wed, 25 February 2004 03:01 Go to previous message
Eclipse UserFriend
> look at
> org.eclipse.jdt.internal.corext.refactoring.structure.ASTNod eSearchUtil
> getMethodDeclarationNode

Saddly the underlying NodeFinder is also placed in an internal package. :(
But I had the same idea yesterday to use the search for a source range
covered
by a node (skipping every node not spanning the given range).

I implemented a visitor for that (looks similar to NodeFinder) but if the
NodeFinder
is in a non-internal package I would love to use it instead (removes one
TestCase :)
But I can't use a class of an internal package :(

The idea to look for the name and use getParent is genius :) I search for
the whole
method declaration. But this would work for nearly everything. :)


Thanks, great suggestion!

Martin (Kersten)
Previous Topic:eclpise home, junit home and ant/cvs
Next Topic:Name of Content Comparison Editor
Goto Forum:
  


Current Time: Mon Jul 14 20:15:57 EDT 2025

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

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

Back to the top